XPath - Wildcard *
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.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 - 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
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
Wildcard XPath Expression:
inventory/*/*/*
- 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/*/*/*
0 comments:
Post a Comment