02.06.2013 Views

XML Demystified

XML Demystified

XML Demystified

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 6 XSLT<br />

instructed by the value of the select attribute. The select attribute customers/customer<br />

refers to the customer element, which is the child of the customers element.<br />

The element says, “For each occurrence of the customer element<br />

within the customers element.” You can achieve the same result by using the<br />

following code. The match attribute states, “Begin at the root (entry point of the<br />

source document) and go to the customers element.”<br />

<br />

...<br />

<br />

...<br />

<br />

...<br />

<br />

<br />

The element extracts text from the source document and transforms it<br />

into the result document. This is the XPath. In the example we show you in the previous<br />

section, we’re extracting the customer ID and the customer’s first and last names.<br />

The instruction extracts the customer ID. The @<br />

symbol indicates that the customer ID is an attribute of the customer element and<br />

not a child element.<br />

The &#160; instruction extracts the customer’s<br />

first name which is contained in the firstname element of the source document.<br />

You’re probably wondering if &#160; is a typo. It isn’t.<br />

This is a whitespace that’s inserted into the result document, so there’s a space<br />

between the customer’s first and last names. 160 is the ISO-8859-1 character value<br />

for a nonbreaking whitespace character, which is the equivalent of &nbsp; in<br />

HTML. (ISO is the International Standards Organization that among other things<br />

establishes values for various nonprintable characters such as a space.)<br />

We’re retrieving all customers from the source document in our example. In a<br />

real-world application, you might need to retrieve a particular customer. You can do<br />

this by modifying the attribute value of .<br />

Suppose you want only customer 286. Here’s what you need to write:<br />

<br />

Likewise, you can specify values for <strong>XML</strong> elements, such as the first name of a<br />

customer as we show here:<br />

<br />

89

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!