XSTL syntax summary

Posted by Monik, 02 November 2010.
XSLT Semantic Web XML
Based on: http://www.w3schools.com/xsl/xsl_templates.asp.
  • <xsl:template match="/"> matches whole document
  • <xsl:value-of> extract value from a node, e.g. <xsl:value-of select="catalog/cd/title"/>, but it will get only first occurence!
  • <xsl:for-each> allows loops:
    <xsl:for-each select="catalog/cd">
    ...
    <xsl:value-of select="title"/>
    ...
    </xsl:for-each>
  • filtering out the data from previous example:<xsl:for-each select="catalog/cd[artist='Bob Dylan']">
    (artist is at the same level as title)
  • sorting - put <xsl:sort select="artist"/> element inside for-each element
  • selecting - embrace the content with <xsl:if test="price > 10"> element inside for-each element
  • selecting multiple things (taki switch):
    <xsl:choose>
    <xsl:when test="price > 10">
    ...

    </xsl:when>
    <xsl:otherwise>

    ...
    </xsl:otherwise>
    </xsl:choose>
  • <xsl:apply-templates> element - applies a template to the current element or to the current element's child nodes (xsl:apply-templates select="title"/> - apply the XSLT to the children named title.. if we define several "xsl:apply-templates", at the same time we specify the order in which the children will be processed)

Comments


Comments: