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]"]
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.
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.
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.
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.
This will return all <document>
nodes having lang
attribute value match any child node <element>
's lang
attribute :
//document[@lang = element/@lang]
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With