A wildcard is a special character used in programming to include everything in your selection. Up to this point we have been specifying elements by their name, but with the use of the wildcard * we can actually select more than one element at a time.
This lesson will teach you how to use XPath’s wildcard, * .
We will be using our lemonade2.xml file, which you can download.
XML Code, lemonade2.xml:
XPath – Match Anything with *
The wildcard is another great way to save time. Often, you will want to select every child element of certain element, which would normally mean a great deal of typing. However, the wildcard can select every child element at once because it automatically matches everything possible! If you play cards, you can think of the wildcard as being the joker of the deck.
If we wanted to select each child element of chips, then we would normally have to do something like…
XPath Expression:
inventory/snack/chips/price
inventory/snack/chips/amount
inventory/snack/chips/calories
However, the asterisk character * matches everything and those three expressions could be replaced with one that selects all the children of chips using a wilcard.
Wildcard XPath Expression:
inventory/snack/chips/*
XPath – Advanced Wildcard Usage
Imagine that we wanted to select every child element of each of our products: lemonade, pop and chips. Using normal XPath expressions, it would take a huge amount of expressions, which we have labored to show you.
XPath Expression:
inventory/drink/lemonade/price
inventory/drink/lemonade/amount
inventory/drink/pop/price
inventory/drink/pop/amount
inventory/snack/chips/price
inventory/snack/chips/amount
inventory/snack/chips/calories
However, with the wildcard we could instead decide to select every child of inventory: drink and snack. Then select every child of those two elements: lemonade, pop and chips. And finally we could select each child of those three elements: price x 3, amount x 3 and calories x 1.
Wildcard XPath Expression:
inventory/*/*/*
This may be a little confusing to look at, so we have broken it down for you.
We specified the root element inventory
We then selected every child of inventory with a wildcard: inventory/*
The elements currently selected were drink and snack
We selected all the children of snack and chips with another wildcard: inventory/*/*
The elements currently selected were lemonade, pop and chips
We selected all the children of lemonade, pop and chips with another wildcard: inventory/*/*/*
Xml Tutorials
XPath – Wildcard *
XPath – Parent ..
This lesson will teach you how to select the parent of an element in your XPath expressions.
We will be using our lemonade2.xml file, which you can download.
XML Code, lemonade2.xml:
XPath – Selecting a Parent with ..
Normally in an XPath expression, you have a string of elements separated by a slash. However, if you put two periods “..” where an element would normally go you can select the parent element. The element to the left of the double period will have its parent selected.
For example, if we wanted to select all of the product elements, lemonade, pop, and chips we would have to do a few expressions.
Normal XPath Expression:
inventory/drink/lemonade
inventory/drink/pop
inventory/snack/chips
However, we could instead select the parent element of amount to get the same three product elements. This is because lemonade, pop and chips all have a child amount.
Parent XPath Expression:
amount/..
This expression uses a relative path location and selects all of the amount elements in our XML document. It then uses the parent sequence “..” to select the parent element of each amount element. In our XML document, there are three amount elements, and their parents are lemonade, pop and chips!