1. Home
  2. Computing & Technology
  3. Web Design / HTML
XSLT: More Transformations
Examining other XSLT Elements for Transforming XML
 Join the Discussion
"Does anyone use this stuff or is it all just mumbo jumbo?"
MAOS1
 
 Related Resources
• XSLT - Advanced
• XSLT - The Basics
• XSLT Processors
 
 Elsewhere on the Web
• XML in a Nutshell
• XSLT.com
 

By Jennifer Kyrnin

There are many elements in XSLT. Not all XSTL processors support all of them, but if you understand what they can do, you have the tools needed to transform XML to almost anything.

xsl:apply-imports
When you want to wrap the effects of one template in another template, you can import those results into the second template using the xsl:apply-imports element.

What does this mean?
If you have a template that matches all the name elements in an XML document, and writes them all as bold HTML. If you then decide that you want all the name elements to also be the color red, you could add it to the first template, but a faster way would be to import the template into your new match:

<xsl:template match="name">
  <b><xsl:value-of select="."/></b>
</xsl:template>

<xsl:template match="name">
  <font color="#ff0000"><xsl:apply-imports/></font>
</xsl:template>

xsl:comment
Because XML comments and HTML comments take the same form, you can use an XSLT element to create a comment within your transformed XML. Any time you use the actual XML comment tags in an XSLT document, they will be ignored in the transformation. If you want your comments to show up in your resulting document, you need to use this element:

<xsl:comment>this is my comment</xsl:comment>
Which will transform as:
<!-- this is my comment -->

xsl:variable
As with most programming languages, XSLT includes variables to store information. Once you've named a variable and defined it, you can use it in your XSLT document.

<xsl:variable name="var_true" select="1"/>
<xsl:variable name="var_false" select="0"/>

xsl:if
Use the xsl:if element to apply tests to your transformations. You must include the test attribute. This attribute includes a boolean expression. If the expression evaluates as true, the information inside the element will be included in the resulting document.

<xsl:if test="$var_true">
This is a true statement, and will be displayed
</xls:if>
<xsl:if test="$var_false">
This is a false statement, and won't be displayed.
</xsl:if>

XSLT has many different elements to help you transform your XML documents. While these are not the complete set of XSLT elements, combined with the elements we learned in XSLT - Advanced, these will help you convert many different types of XML documents to HTML.

Previous Features

Explore Web Design / HTML

More from About.com

  1. Home
  2. Computing & Technology
  3. Web Design / HTML
  4. About.com Web Design A to Z
  5. Web Design Articles R-Z
  6. Web Design/HTML Articles X
  7. XSLT: More Transformations

©2008 About.com, a part of The New York Times Company.

All rights reserved.