Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Removing Content with XSLT - Page 15

November 4, 2002

You may have noticed that we haven't provided an <xsl:template> for our new <discoverer> element. Additionally, we haven't included the identity transformation in this stylesheet, so as a result our <discoverer> element is ignored. Don't worry, we won't forget the dinosaur discoverers completely – we'll come back to them in Chapter 6 and make sure they get the credit they deserve.

This does show how easy it is to exclude content with XSLT – one application for this would be to strip out confidential company information when an internal document is republished on an extranet. (However, this would have to be done with server-side XSLT, not client-side, otherwise a simple View Source would show the hidden information.)

Our decision to exclude the identity transformation from this example is because the output of this stylesheet is very different from the source XML file. As we saw earlier, the identity transformation is useful when the output is very similar to the source and we wish to leave most things unchanged.

We have added a new template:

<xsl:template match="text()" />

XSLT has a built-in rule for text, specifying that any text in the source XML is passed through unchanged by default. In this example we don't want this to happen – we want to suppress text, and only display it when we specifically match it with value-of rules. If we didn't include this text() template, the names of our explorers (which are text nodes in the XML tree) would be passed through and displayed, even though we have not given a rule for their parent (the <discoverer> tag).

The resulting web page from these transformations is essentially the same as the one shown in Example 1 earlier, only this time we started with simple, reusable XML.

XPath – We've Used It a Lot Already, Without Even Noticing

So far, we've used XPath several times without really explaining what it is. In the template above, where we use:

<xsl:value-of select="description/text()" />


…we are using an XPath statement to reference the text child of the description element that is a child of the current node. Again, in the template:

<xsl:template match="body">

…we are using XPath to reference the element identified by the name "body".

XPath is used inside the match and select attributes of many XSLT elements, to choose nodes in the input document, and to alter them. XPath consists of two parts – the path-like document navigation parts shown above, and a set of JavaScript-like functions like substring. These functions will only work in XSLT 1.0, so we will cover some examples of them in the next chapter.

To select attributes with XPath, we just use the attribute name with an "@" in front of it. For example, to output the href attribute of an <a> element we were currently on, we use:

<xsl:value-of select="@href" />

We can also write our XPath so that we match only elements that have a certain attribute. For example, to match only those <dinosaur> elements that have a name attribute, we'd use:

<xsl:template match="dinosaur[@name]" />

It is possible to use conditional statements within XPath as well – for example, to match only the dinosaur with name "Triceratops", we'd use:

<xsl:template match="dinosaur[@name='Triceratops']" />

Conditions can be combined with "and" and "or", for example:

<xsl:template match="dinosaur[@name='Brontosaurus' or @name= 'Triceratops']" >

…will match the <dinosaur> elements with the name "Brontosaurus" or "Triceratops".

We can also match elements that have a specific parent – for example:

<xsl:template match="description/b" >

…would only match <b> tags that were directly inside the <description>, while:

<xsl:template match="description//b" >

would match <b> tags that were within the description, directly or not. For example, in the following description, the <b> would be matched by the description//b XPath, but not by description/b.

<description>A Brontosaurus is <i><b>big</b> and scary.</i></description>

XSL-WD doesn't support the full XPath specification, and so this is the limit of what's possible using XSL-WD inside IE 5.0 and IE 5.5. There are a lot more possibilities available using XSLT 1.0, such as accessing the parent and the siblings of the current node, and a set of functions that includes substring, count, and contains that we'll talk about more in Chapter 6.


XPath is a large subject, and we have barely touched upon it here. However, not too much of it is necessary for everyday use. We will explain more of XPath as we go through the examples below and in the next chapter. A complete XPath reference can be found at http://www.zvon.org/xxl/XSLTreference/Output/index.html, while the XPath specification is available at http://www.w3.org/TR/xpath.

Styling the XML with XSLT - Page 14
Practical XML for the Web
Practical XML for the Web - Page 16


Up to => Home / Authoring / Languages / XML / PracticalXML




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers