Home > front end >  XPath notebook: XError:Focus for / is absent; code:XPDY0002
XPath notebook: XError:Focus for / is absent; code:XPDY0002

Time:12-21

I have a simple question for you. I want to count how many nodes contained in the heavy XML file.

For example: (In this case, Xpath expression should give me count of bridge node which is 1)

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

This is my Xpath expression:

count(//full_info/bridge)

But this command continuously giving that error:

XError:Focus for / is absent; code:XPDY0002

How to solve this problem? please help me

CodePudding user response:

In general, traditional XPath is not a good tool to deal with huge (GBs of input) XML documents so you might want to look into more advanced techniques like XSLT 3 with streaming where Saxon 10 EE or SaxonCS allow running

<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>

and would not build a complete in memory tree but rather stream through and count nodes.

The other option would be to look into BaseX or eXist-db or other XML database systems, there you would need to put that huge XML into a db first but then hopefully the XPath or XQuery to count nodes doesn't cause out of memory problems.

  • Related