XPath - Vertical Bar | (Pipe)
XPath makes use of the character "|", which we will be referring to as pipe from here on out. The pipe character is a way of combining two or more expressions into one. Depending on where you are using XPath, this ability to combine multiple expressions into one may be useful.
This lesson will teach you how to use XPath's combine feature, "|".
We will be using our lemonade2.xml file, which you can download.
XML Code, lemonade2.xml:
<inventory>
<drink>
<lemonade supplier="mother" id="1">
<price>$2.50</price>
<amount>20</amount>
</lemonade>
<pop supplier="store" id="2">
<price>$1.50</price>
<amount>10</amount>
</pop>
</drink>
<snack>
<chips supplier="store" id="3">
<price>$4.50</price>
<amount>60</amount>
<calories>180</calories>
</chips>
</snack>
</inventory>
XPath - Combining Two Expressions with |
When you need to select multiple things with one expression, chances are you will have to use the pipe character. In our lemonade2.xml document, we might want to select the children of chips and the children of pop. To do this, we would use the pipe character "|".
Combining XPath Expression:
inventory/snack/chips/* | inventory/drink/pop/*
This expression combines two expressions into one and will select every element that matches either expression.
XPath - Combining Multiple Expressions with |
Although you probably won't use this feature very often, it is possible to combine as many expressions as you want with the pipe character. Here is an example that selects all of the children elements of chips, using |.
Combining XPath Expression:
chips/price | chips/amount | chips/calories
Download Tizag.com's XML Book
If you would rather download the PDF of this tutorial, check out our
XML eBook from the Tizag.com store. Found Something Wrong in this Lesson?Report a Bug or Comment on This Lesson - Your input is what keeps Tizag improving with time! |