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


Transformation Without Change - Page 8

October 28, 2002

The stylesheet has added some code to the beginning of the translated <body> element, but hasn't yet done anything with the original contents of the <body> element. That's the job of the next line, <xsl:apply-templates />. This line says "Work through all of the contents of the <body> element, and perform any other transformations you need to on any tags you find". In our next example, we'll create some more template transformations for some other elements, but for now we don't want to change anything else at all. We want the rest of the <body> element contents to be passed through unchanged.

Even though we want the content to remain unchanged, we still need to specify a transformation for the other tags in our page – <html>, <head>, <title>, <style>, <b>, <h1>, <h2>, <ul> and <li>. If we don't specify a transformation for these tags, they will be ignored, leaving us with a mass of unformatted text and no tags at all. This isn't nice. We use the Identity Transformation to pass these tags through unchanged.

The Identity Transformation is specified in those curious few lines right in the middle of headerfooter_1.xsl:

<xsl:template match="node()|@*">

<xsl:copy>

<xsl:apply-templates select="node()|@*"/>

</xsl:copy>

</xsl:template>

The Identity Transformation is a transformation that leaves everything just the way it found it. It matches every part of our source XML document for which we haven't given a specific rule, and passes it through unchanged – let's look at how this works now.

How Does the Identity Transformation Work?

Very often when writing a stylesheet, we find that we only want to make small changes to the input document. If this is the case, then it's easiest to pass through almost everything unchanged, and just match the few elements that need alteration.

We do this using the Identity Transformation introduced above:

<xsl:template match="node()|@*">

<xsl:copy>

<xsl:apply-templates select="node()|@*" />

</xsl:copy>

</xsl:template>

This template matches both node() (any node in the input document) and @* (any attribute), so it matches everything. When it matches, it uses <xsl:copy> to create an identical copy of the item it has matched, and then uses <xsl:apply-templates> to process the contents of the item if there are any.

If this was the only template in a stylesheet, it would produce an output document functionally the same as the input one. The identity transformation isn't appropriate when the output document needs to be considerably different from the input document.


Normally, as we have done in Example 1 above, we would use the identity template alongside others that add, remove, or alter a few nodes. These other templates, such as the template matching <body> that we defined above, will be used rather than the identity template for the nodes that they match. Each element or attribute in the source XML document can only be matched by one template (as we'll see later in this chapter, the most specific template available is used in XSLT 1.0); this is why we had to specifically copy the <body> element to our output document rather than relying on the identity template to do it for us.

Adding the Footer

Lastly, we want to add some footer text after the unchanged <body> element contents. This is performed by the end section of our <body> element template:

<hr/>

Copyright 2002 DinosaurOrg.

</body>

</xsl:template>

After the body content has been passed through unchanged, these lines add some footer HTML to the page and then close the <body> tag. We then close our body <xsl:template> element, to tell the XSLT processor we have finished dealing with the <body> element.

Finally, the last line of headerfooter_1.xsl tidies up the stylesheet with the line </xsl:stylesheet>. We're done!

You Too Can Transform Your <body> - Page 7
Practical XML for the Web
Specifying Stylesheets for Different Browsers - Page 9


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