Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare attribute values using Xpath

Tags:

xpath

Given the following document structure, how could i check if two attribute values match using Xpath?

<document lang="en">
<element lang="en"></element>
<element lang="sv"></element>
<element lang="fr"></element>
</document>

What i am looking for is something like:

//document[@lang="[//element[@lang]"]
like image 623
user2075124 Avatar asked Sep 18 '14 13:09

user2075124


People also ask

What is attribute and value in XPath?

XPath Tutorial from basic to advance level. This attribute can be easily retrieved and checked by using the @attribute-name of the element. @name − get the value of attribute "name". <td><xsl:value-of select = "@rollno"/></td> Attribute can be used to compared using operators.

What is attribute in XPath?

Definition of XPath attribute. For finding an XPath node in an XML document, use the XPath Attribute expression location path. We can use XPath to generate attribute expressions to locate nodes in an XML document.

What is XPath with examples?

XPath is a major element in the XSLT standard. XPath can be used to navigate through elements and attributes in an XML document. XPath is a syntax for defining parts of an XML document. XPath uses path expressions to navigate in XML documents. XPath contains a library of standard functions.

What is XPath parsing?

It defines a language to find information in an XML file. It is used to traverse elements and attributes of an XML document. XPath provides various types of expressions which can be used to enquire relevant information from the XML document.


2 Answers

This will return all <document> nodes having lang attribute value match any child node <element>'s lang attribute :

//document[@lang = element/@lang]
like image 131
har07 Avatar answered Sep 28 '22 18:09

har07


Specifically to your example, you can use:

//document[@lang=child::element/@lang]

If you are just checking whether a match exists, you can wrap it in a boolean:

boolean(//document[@lang=child::element/@lang])

If you want to select the matched element, you can check by ancestor:

//element[@lang=ancestor::document[1]/@lang]

If you want to match any nodes that have matching attributes elsewhere, you can do something like this:

//node()[@lang=following::node()/@lang]

That should match the first node that has a match elsewhere in the document.

like image 34
JoeLinux Avatar answered Sep 28 '22 18:09

JoeLinux