Extensible Markup Language (XML) Tutorial

XSL Transformations

Basic Transformations

The value-of keyword is used to match a pattern and generates an element in the result tree containing the matched text.

<xsl:value-of select="/collection/cd/band" />

Element attributes are accessed using @. The following example matches the attribute year of the title element.

<xsl:value-of select="/collection/cd/title/@year" />

Selection

The if keyword is used for decision making. Conditions are placed in square brackets. The following example tests the paid attribute. If the paid attribute has a value of yes, then Fully Paid Member is displayed.

<xsl:if test=".[membership/@paid='yes']">Fully Paid Member</xsl:if>

Multiple conditions are performed using the choose keyword.

<xsl:choose>
    <xsl:when test=".[membership/@paid='yes']">Fully Paid Member</xsl:when>
    <xsl:when test=".[membership/@paid='no']">Unpaid Member</xsl:when>
    <xsl:otherwise>Unknown Payment Status</xsl:otherwise>
</xsl:choose>

Iteration

Iteration is achieved using the for-each keyword. The following example iterates through each cd in the collection and displays the band.

<xsl:for-each select="/collection/cd">
    <xsl:value-of select="band" />
< /xsl:for-each>m

With iteration, the sort keyword may be used to sort elements selected by the select attribute. The following example displays the bands in ascending order.

<xsl:for-each select="/collection/cd">
    <xsl:sort select="band" order="ascending" />
    <xsl:value-of select="band" />
</xsl:for-each>

You might also like...

Comments

About the author

Gez Lemon United Kingdom

I'm available for contract work. Please visit Juicify for details.

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Engineers are all basically high-functioning autistics who have no idea how normal people do stuff.” - Cory Doctorow