Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Peek Into the Future of XSLT 2.0 : Page 5

XSLT 2.0 provides powerful new features that XSLT 1.0 was missing. Although XSLT 2.0 is still under development, Mike Kay's Saxon 7 implementation lets you experiment with the new capabilities now.


advertisement
Putting It All Together
It's not fair to take a peek at all these new toys if you can't play with them a bit. So here are some interesting examples that illustrate the new powers in XLST 2.0 and XPath 2.0. Listing 3 shows the source document used for the sample transformations. It's essentially the same as Listing 1, but I've removed the schema to simplify adding an element to the file without worrying about validation.

Now, take a look at Listing 4. I've employed a few new XSLT 2.0 operations, so I'll break down the code a bit. The highlighted XSLT 2.0 features appear in bold text.

The first interesting chunk of code concerns an old favorite of Perl programmers, regular expressions. I've decided to take Bert, the maker of all these fine products, out of the loop, so to speak, and hand the products over to a fellow named Tom. I do this not because I don't like Bert, but because I want to show how regular expressions have made their way into XSLT 2.0. The following chunk of source XML shows a <product> element:

<product> <name>Bert&apos;s Coffee</name> <quantity>1</quantity> <quantity>3</quantity> <quantity>3.4</quantity> <quantity>8</quantity> <selldate>2003-01-21</selldate> </product>

The object is to transform the <name> element in the input document...

<name>Bert&apos;s Coffee</name>

...into the following HTML in the output:



<h1>Tom's Coffee</h1>

Trying Out a Regular Expression
In XSLT 1.0 the only way to replace a string of text is to use a recursive template, such as the one found here.

There's nothing wrong with using such templates, of course, but it would be nice to have a simple function for it. Now, with the help of regular expressions, there is. Rather than calling a complex template function, you can write simple code like this:

<h1> <xsl:value-of select="replace(., '(Bert)', 'Tom')"/> </h1>

The replace function takes three arguments. The first argument is the source string, the second is the part of the string that you want to replace, written as a regular expression, and the third is the replacement string or expression. The function replaces each instance of the target expression found in the source string with the replacement expression. I used simple literals for my regular expressions, but if you know regular expressions you can really do some serious string manipulations here.

Click here for more information on how to use regular expressions.

There are some differences between the way you use regular expressions in Perl and the way you use them in XML Schema. To explore regular expressions specifically as they pertain to XML Schema see http://www.xfront.org/xml-schema/.

Restricting Sequences

Another thing I wanted to do was effortlessly output the value of each <quantity> element within each <product> element. Again, this kind of thing is pretty easily accomplished in XSLT 1.0 once you've learned the language, but now it's even easier because of one simple keyword named except. This keyword lets you restrict certain values from a specific sequence you are trying to generate. For example, to include all the following siblings of the name element, except the last one, you can write:

select="(following-sibling::*) except following-sibling::*[5]"

The result outputs all the quantity elements to HTML:

Sale: 1<br> Sale: 3<br> Sale: 3.4<br> Sale: 8<br>

Note that in XSLT 1.0 you can accomplish the same thing with this select statement:

select="following-sibling::*[not(position()=5)]"



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap