Search code examples
xpathxpathdocument

Nodes sequence with options values


Here's a question about repeated nodes and missing values.

Given the xml below is there a way in XPath of returning (value2, null, value5) rather than (value2, value5)? I'm using an expression that looks like:

/nodes/node[*]/value2/text()

To retrieve the values. I need to know there's a value missing and at which indexes this occurs. I'm a complete newby to XPath and cannot figure out how to do this.

<nodes>
  <node>
    <value1>value1</value1>
    <value2>value2</value2>
  </node>
  <node>
    <value1>value3</value1>
  </node>
  <node>
    <value1>value4</value1>
    <value2>value5</value2>
  </node>
</nodes>

Kind regards,

mipper


Solution

  • Given the xml below is there a way in XPath of returning (value2, null, value5)

    In XPath, no. XPath can only select/return existing nodes. It does not know null.

    In XSLT or any other XML aware programming language the problem can be solved by iteration.

    <xsl:for-each select="/nodes/node">
      <xsl:value-of select="*[2]" />
      <xsl:text>, </xsl:text>
    </xsl:for-each>