Home > OS >  How to get node name/display value using XPATH
How to get node name/display value using XPATH

Time:07-20

This is my xml

 <Governing_Law_TermName displayValue="NZ Law">
   <Governing_LawText>This is the text within</Governing_LawText>
</Governing_Law_TermName>

I want to extract the term 'NZ Law' but it's not the value within a node, but defined as its displayValue.

I tried the following but did not work:

name(//Governing_Law_TermName)

CodePudding user response:

In your XPath expression, the sub-expression //Governing_Law_TermName identifies the element, and the name() function will return the name of that element i.e. Governing_Law_TermName, which is obviously not what you want.

What you actually want is to traverse from the Governing_Law_TermName element to its displayValue attribute. Follow the path along the attribute axis like so:

//Governing_Law_TermName/attribute::displayValue

... or in its more commonly used shorthand form:

//Governing_Law_TermName/@displayValue

Note that in the terminology used for XPath, the Governing_Law_TermName is a "node", and so is the displayValue attribute, as is the Governing_LawText element and also the text This is the text within. Text nodes, elements, and attributes (among others) are all different kinds of node.

  • Related