Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get node name/display value using XPATH

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

name(//Governing_Law_TermName)

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading