简体   繁体   English

使用 XML 标记在 Saxonica 中检索 XPath 结果

[英]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.我正在尝试使用杂项 xpath 和来自net.sf.saxon 的Saxonica API 查询 XML 文件,但似乎每次查询操作都返回没有 xml 标签的结果 - 只有内容。 Is there a way to do this (straight-forward or work-around)?有没有办法做到这一点(直接或变通)?

To be more explicit:更明确地说:

For the xml file对于 xml 文件

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

and the xpath和 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; XPath 表达式返回一个节点; 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).如果您调用node.getStringValue() ,您将获得 XPath 规范中定义的字符串值(即,与在 XPath 内的节点上调用fn:string()相同)。 Similarly, the atomize() method follows the XPath spec for atomization (equivalent to fn:data() applied to the node.)类似地, atomize atomize()方法遵循原子化的 XPath 规范(相当于应用于节点的fn:data() 。)

If you want the node to be serialized as lexical XML, there are various ways of achieving it.如果您希望将节点序列化为词法 XML,则有多种实现方式。 If you were to use Saxon's s9api interface instead of the JAXP interface, I would recommend XdmNode.toString() .如果您要使用 Saxon 的 s9api 接口而不是 JAXP 接口,我会推荐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).使用 JAXP 接口然后转换为内部 Saxon 类会给您带来两全其美的结果:您会遇到 JAXP 的所有问题(例如弱类型、没有 XPath 2.0 支持)而没有任何好处(跨实现的可移植性)。 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) .但是,如果您更喜欢这样做,那么序列化 Saxon 节点的最简单方法可能是静态方法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.该方法的 3 参数版本使您可以完全控制序列化属性,例如缩进和添加 XML 声明。

With XPath 3.1 you can also invoke serialization within the XPath expression itself by calling fn:serialize() ;使用 XPath 3.1,您还可以通过调用fn:serialize()在 XPath 表达式本身内调用序列fn:serialize() this would avoid having to use any Saxon-specific classes and methods in the Java code.这将避免在 Java 代码中使用任何特定于 Saxon 的类和方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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