What is Xpath? Examples of Xpath? Contd.

What is Xpath? Examples of Xpath? Contd.

Xpath expressions can be Location Path (set of tree nodes), Boolean (true or false), Numeric (number) or a String (unicode string). Let’s see some very basic examples of Xpath.

If the path starts with a single slash / , it represents absolute path to the required element.

Example: To select the root element “Main” from an XML document.
<Main>
<Sub1>           </Sub1>
<Sub2>           </Sub2>
<Sub3>            </Sub3>
</Main>
Syntax:  /Main
Results:
<Main>
<Sub1> </Sub1>
<Sub2> </Sub2>
<Sub3> </Sub3>
</Main>

Another example

Example : Select only Sub1 element which is children of the root element Main
<Main>
<Sub1>           </Sub1>
<Sub2>           </Sub2>
<Sub3>            </Sub3>
</Main>
Syntax:  /Main/Sub1
Results:
<Sub1>           </Sub1>

If the path starts with double slashes // , then all elements in the document which fulfill following criteria will be selected.

Example : Select all elements with Sub2 which are children of the root element Main
<Main>
<Sub1>           </Sub1>
<Sub2>           </Sub2>
<Sub3>            </Sub3>
<Sub2>            </Sub2>
</Main>
Syntax:  //Sub2
Results:
<Sub2> </Sub2>
<Sub2> </Sub2>

If the path syntax contains * , then it selects all elements by preceding path. 

Example : Select all elements enclosed by element Sub3
<Main>
<Sub1>           </Sub1>
<Sub2>           </Sub2>
<Sub3>
<Sub30>            </Sub30>
<Sub31>            </Sub31>
</Sub3>
          <Sub2>            </Sub2>  
</Main>
Syntax:  /Main/Sub3/*
Results:
<Sub30> </Sub30>
<Sub31> </Sub31>

If the path syntax contains | which is also called as pipe, which is used for combining paths. 

Example : Select/Combine element Sub1 and Sub2
<Main>
          <Sub1>           </Sub1>
<Sub2>           </Sub2>
<Sub3>
<Sub30>            </Sub30>
<Sub31>            </Sub31>
</Sub3>
<Sub2>            </Sub2>
</Main>
Syntax: //Sub1 | //Sub2
Results:
<Sub1> </Sub1>
<Sub2> </Sub2>
<Sub2> </Sub2>

If the path syntax contains @ , then it selects attributes from the given path.

Example: Select all attributes specified by @href
<Main>
<documents><a href=”abc.doc”>DOC</a></documents>
<documents><a href=”abc.ppt”>PPT</a></documents>
<documents><a href=”abc.xlsx”>XLSX</a></documents>
<documents><a id=”id”>ID</a></documents>
</Main>
Syntax: //@href
Results:
href=”abc.doc”
href=”abc.ppt”
href=”abc.xlsx”

To test xpath, use the below link –

https://www.uccollabing.com/free-online-xpath-tester-evaluator-tool/

There are many Xpath examples which you can search over web and learn more on xpath syntax. Some helpful links below:
Some tutorials and examples
http://www.w3schools.com/xml/xpath_intro.asp
http://saxon.sourceforge.net/saxon6.5.3/expressions.html
https://msdn.microsoft.com/en-us/library/ms256086(v=vs.110).aspx
Xpath Quick Reference : 
https://www.mulberrytech.com/quickref/XSLT_1quickref-v2.pdf
http://scraping.pro/res/xpath-cheat/xpath_css_dom_recipes.pdf
http://scraping.pro/res/xpath-cheat/xpath_css_dom_ref.pdf

In the next post, we would learn more on Xpath and see how we can use Xpath in UCCX (Cisco Unified Contact Center Express).

Hope this helps!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top