About Me

Friday 13 April 2012

XPath - Wildcard *

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.

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:

<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
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.
  1. We specified the root element inventory
  2. We then selected every child of inventory with a wildcard: inventory/*
  3. The elements currently selected were drink and snack
  4. We selected all the children of snack and chips with another wildcard: inventory/*/*
  5. The elements currently selected were lemonade, pop and chips
  6. We selected all the children of lemonade, pop and chips with another wildcard: inventory/*/*/*

0 comments:

Post a Comment