Applying xpath on XML where namespaces are with children with XOM

☆樱花仙子☆ 提交于 2019-12-13 07:19:30

问题


I'm using XOM library to parse xmls. I have the following XML

<ns4:parent xmlns:ns4="http://sample.xml.com/ns4">
    <ns4:child1 xmlns:ns1="http://sample.xml.com/ns1" xmlns:xsi="http://sample.xml.com/xsi">
        <ns1:child2>divaStatus</ns1:child2>
        <xsi:child>something</xsi:child>
    </ns4:child1>
</ns4:parent>

And I want to apply a xpath like ns4:parent/ns4:child1/ns1:child2. So my code is like below

Document doc = new Builder().build(inStream);  //inStream is containing the xml
XPathContext xc = XPathContext.makeNamespaceContext(doc.getRootElement());
doc.query("ns4:parent/ns4:child1/ns1:child2", xc);

And I'm getting XPathException here.

 Exception in thread "main" nu.xom.XPathException: XPath error: XPath expression uses unbound namespace prefix ns1.

I can understand that since im making the namespace context by Root Element only, its not getting the namespaces of its children. So one work around might be traversing through all the children and collecting their namespaces and add it into the XpathContext object. But my xml can be of 10 to 20k lines. So Im afraid that how efficient the traversal approach will be.

Looking forward for any better suggestion


回答1:


This is easy (or as easy as it can be, given the design of XPath and XML Namespaces). You just need to add any namespaces you want to use to the context manually. For instance, in this case,

XPathContext xc = new XPathContext();
xc.addNamespace("ns4", "http://sample.xml.com/ns4");
xc.addNamespace("ns1", "http://sample.xml.com/ns1");
doc.query("ns4:parent/ns4:child1/ns1:child2", xc);

Note that you do not have to use the same prefixes in the XPath expression that the document uses.




回答2:


Presumably you know the namespace which is associated with each element name. So you could write:

String xpath = "*[local-name()='parent' and namespace-uri()='http://sample.xml.com/ns4']"+
  "*[local-name()='child1' and namespace-uri()='http://sample.xml.com/ns2']"+
  "*[local-name()='child2' and namespace-uri()='http://sample.xml.com/ns1']";

I would expect that to be as efficient as the prefixed version.



来源:https://stackoverflow.com/questions/14934178/applying-xpath-on-xml-where-namespaces-are-with-children-with-xom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!