XPath notebook: XError:Focus for / is absent; code:XPDY0002

XPath notebook: XError:Focus for / is absent; code:XPDY0002

我有一个简单的问题要问你。我想计算重 XML 文件中包含的节点数。

例如:(在这种情况下,Xpath 表达式应该给我桥接节点的计数,即 1)

<?xml version="1.0" standalone="yes"?>
<full_info>
  <bridge>
    <FFF3>12314</FFF3>
    ...
  </bridge>
</full_info>

这是我的 Xpath 表达式:

count(//full_info/bridge)

但是这个命令不断地给出那个错误:

XError:Focus for / is absent; code:XPDY0002

如何解决这个问题?请帮助我

一般来说,传统的 XPath 不是处理大量(GB 级输入)XML 文档的好工具,因此您可能想研究更高级的技术,例如 XSLT 3 和流式传输,其中 Saxon 10 EE 或SaxonCS 允许 运行

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0">
  
  <xsl:output method="text"/>
  
  <xsl:mode streamable="yes"/>
  
  <xsl:template match="/">
    <xsl:value-of select="count(//full_info/bridge)"/>
  </xsl:template>
  
</xsl:stylesheet>

并且不会构建完整的内存树,而是流式处理和计算节点。

另一种选择是查看 BaseX 或 eXist-db 或其他 XML 数据库系统,在那里您需要先将巨大的 XML 放入数据库,然后希望是 XPath或 XQuery 来计算节点不会导致内存不足问题。