Introduction
I created this page to document and share our findings on using XPath expressions with Yang.
External Resources
https://stackoverflow.com/questions/51639936/what-is-the-xpath-to-access-leaves-within-a-list-yang
https://www.w3schools.com/xml/xpath_syntax.asp
XPath Syntax
In this example I have used XML just to demonstrate how XPaths work.
<bookstore> <categories> <name>web</name> <books> <authors>Toine Siebelink</authors> <authors>David Lang</authors> <lang>en</lang> <price>123456</price> <pub_year>2020</pub_year> <title>My first book</title> </books> </categories> <categories> <name>art</name> <books> <authors>Test</authors> <lang>en</lang> <price>1234</price> <pub_year>2020</pub_year> <title>My 2nd book</title> </books> </categories> </bookstore>
Path Expression | Result |
---|---|
/bookstore/categories[name='web'] | <categories> <name>web</name> <books> <authors>Toine Siebelink</authors> <authors>David Lang</authors> <lang>en</lang> <price>123456</price> <pub_year>2020</pub_year> <title>My first book</title> </books> </categories> |
/bookstore/categories[name='web']/books/title | <title>My first book</title> |