Library tutorials & articles

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>

Comments

  1. 19 Jan 2006 at 14:16

    Beutiful article. Takes a user from a beginner level to an intermediate level very nicely. Certainly a treat for beginers and an expert alike.

  2. 04 Apr 2004 at 22:28

    Good article for starters

  3. 01 Jan 1999 at 00:00

    This thread is for discussions of Extensible Markup Language (XML) Tutorial.

Leave a comment

Sign in or Join us (it's free).

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

Related discussion

Related podcasts

  • LINQ to XML

    Scott's been poking around with LINQ to XML and reports his findings to Carl about life with XDocuments and XElements. They also talk about the bridge classes that link (no pun intended) System.Xml and System.Xml.Linq.

Want to stay in touch with what's going on? Follow us on twitter!