XPath: Difference between revisions
(24 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
=External= | =External= | ||
* https://www.baeldung.com/linux/evaluate-xpath | * https://www.baeldung.com/linux/evaluate-xpath | ||
* https://www.w3schools.com/xml/xpath_intro.asp | |||
* https://www.w3schools.com/xml/xpath_syntax.asp | |||
=Internal= | =Internal= | ||
Line 22: | Line 23: | ||
{{External|https://www.baeldung.com/java-xpath}} | {{External|https://www.baeldung.com/java-xpath}} | ||
=Syntax= | =Syntax= | ||
{{External|https://www.w3schools.com/xml/xpath_syntax.asp}} | |||
==Example== | ==Example== | ||
<syntaxhighlight lang='xml'> | <syntaxhighlight lang='xml'> | ||
Line 49: | Line 51: | ||
</books> | </books> | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== | ==Node Selection== | ||
=== | XPath uses path expressions to select nodes in an XML document. The node is selected by following a path (or steps): | ||
===<tt>nodename</tt>=== | |||
Select the node (or nodes) with the given <code>nodename</code>. If multiple nodes with the same node match, they're all selected. | |||
xml sel -t -v "/books/book/title" ./books.xml | |||
=== | ===<tt>/</tt>=== | ||
Select from the root node. | |||
xml sel -t -v "/books/book/title" ./books.xml | |||
// | |||
== | ===<tt>//</tt>=== | ||
Selects nodes in the document from the current node that match the selection no matter where they are/ | |||
xml sel -t -v "//title" ./books.xml | |||
===<tt>.</tt>=== | |||
Selects the current node | |||
===<tt>..</tt>=== | |||
Selects the parent of the current node | |||
===<tt>@</tt>=== | |||
Selects attributes. | |||
To get the value of a specific attribute, use <code>/@<attribute-name></code> as below: | |||
<syntaxhighlight lang='text'> | |||
/books/book[@id='1']/@category | |||
</syntaxhighlight> | |||
==Predicates== | |||
Predicates are used to find a specific node or a node that contains a specific value. Predicates are always embedded in square brackets. | |||
<syntaxhighlight lang='text'> | |||
element-name[@attribute-name='value'] | |||
element-name[sub-element-expression] | |||
</syntaxhighlight> | |||
==Select an Element based on Two Attributes== | |||
<syntaxhighlight lang='text'> | |||
/box/object[@name='D' and @color='yellow'] | |||
</syntaxhighlight> | |||
Note that <code>and</code> is case sensitive. | |||
===Select an Element based on Value/Attribute of Sub-Element=== | |||
Select the <books>/<book> element whose sub-element <year> is 2003, and display the <title> sub-element of the element. | |||
<syntaxhighlight lang='text'> | |||
/books/book[year=2003]/title | |||
</syntaxhighlight> | |||
Select the <books>/<book> element whose sub-element <title> has a "fr" 'lang' attribute, and display the <author> sub-element of the selected <book> element. | |||
<syntaxhighlight lang='text'> | |||
/books/book/title[@lang='fr']/../author | |||
</syntaxhighlight> |
Latest revision as of 02:21, 5 July 2021
External
- https://www.baeldung.com/linux/evaluate-xpath
- https://www.w3schools.com/xml/xpath_intro.asp
- https://www.w3schools.com/xml/xpath_syntax.asp
Internal
Overview
XPath is a specification language that allows specifying parts of an XML structure. It is part of XSL. An XPath expression can be thought as an address of a part of an XML document.
TODO: https://docs.oracle.com/javase/tutorial/jaxp/xslt/xpath.html
Command Line Tooling
Java Support
Syntax
Example
<books>
<book id="1" category="linux">
<title lang="en">Linux Device Drivers</title>
<year>2003</year>
<author>Jonathan Corbet</author>
<author>Alessandro Rubini</author>
</book>
<book id="2" category="linux">
<title lang="en">Understanding the Linux Kernel</title>
<year>2005</year>
<author>Daniel P. Bovet</author>
<author>Marco Cesati</author>
</book>
<book id="3" category="novel">
<title lang="en">A Game of Thrones</title>
<year>2013</year>
<author>George R. R. Martin</author>
</book>
<book id="4" category="novel">
<title lang="fr">The Little Prince</title>
<year>1990</year>
<author>Antoine de Saint-Exupéry</author>
</book>
</books>
Node Selection
XPath uses path expressions to select nodes in an XML document. The node is selected by following a path (or steps):
nodename
Select the node (or nodes) with the given nodename
. If multiple nodes with the same node match, they're all selected.
xml sel -t -v "/books/book/title" ./books.xml
/
Select from the root node.
xml sel -t -v "/books/book/title" ./books.xml
//
Selects nodes in the document from the current node that match the selection no matter where they are/
xml sel -t -v "//title" ./books.xml
.
Selects the current node
..
Selects the parent of the current node
@
Selects attributes.
To get the value of a specific attribute, use /@<attribute-name>
as below:
/books/book[@id='1']/@category
Predicates
Predicates are used to find a specific node or a node that contains a specific value. Predicates are always embedded in square brackets.
element-name[@attribute-name='value']
element-name[sub-element-expression]
Select an Element based on Two Attributes
/box/object[@name='D' and @color='yellow']
Note that and
is case sensitive.
Select an Element based on Value/Attribute of Sub-Element
Select the <books>/<book> element whose sub-element <year> is 2003, and display the <title> sub-element of the element.
/books/book[year=2003]/title
Select the <books>/<book> element whose sub-element <title> has a "fr" 'lang' attribute, and display the <author> sub-element of the selected <book> element.
/books/book/title[@lang='fr']/../author