简体   繁体   中英

Retrieve XPath result in Saxonica with XML tag

I am trying to query an XML file using miscellaneous xpaths with Saxonica API from net.sf.saxon but it seems that every time the query operations return results without xml tags - only the content. Is there a way to do this (straight-forward or work-around)?

To be more explicit:

For the xml file

<books>
    <book lang="en">
        <nrpages>140</nrpages>
        <author>J.R.R.Tolkien</author>
    </book>
</books>

and the xpath

//book

I would like to retrieve

<book lang="en">
    <nrpages>140</nrpages>
    <author>J.R.R.Tolkien</author>
</book>

instead of

140
J.R.R.Tolkien

What I've tried:

XPathFactory factory = new XPathFactoryImpl();
XPathExpression compiledXPath = factory.newXPath().compile(xPathExpression);
TinyNodeImpl nodeItem = (TinyNodeImpl) compiledXPath.evaluate(new InputSource(filename), XPathConstants.NODE);
nodeItem.atomize(); // brings only the content
nodeItem.getStrinValue(); // brings only the content

The XPath expression returns a node; what you do with the node is then up to the calling application code. If you call node.getStringValue() , you will get the string value as defined in the XPath spec (that is, the same as calling fn:string() on the node within XPath). Similarly, the atomize() method follows the XPath spec for atomization (equivalent to fn:data() applied to the node.)

If you want the node to be serialized as lexical XML, there are various ways of achieving it. If you were to use Saxon's s9api interface instead of the JAXP interface, I would recommend XdmNode.toString() . Using the JAXP interface and then casting to internal Saxon classes gives you the worst of both worlds: you get all the problems of JAXP (eg weak typing, no XPath 2.0 support) with none of the benefits (portability across implementations). But if you prefer to do it this way, then the simplest way to serialize Saxon nodes is probably the static method QueryResult.serialize(NodeInfo) . The 3-argument version of the method gives you full control over serialization properties such as indentation and adding an XML declaration.

With XPath 3.1 you can also invoke serialization within the XPath expression itself by calling fn:serialize() ; this would avoid having to use any Saxon-specific classes and methods in the Java code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM