Forum Moderators: open

Message Too Old, No Replies

Subroutines and Loops

Repeat When Necessary

         

cmarshall

8:23 pm on Mar 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



One thing that I forgot to mention in my last missive was a basic counted loop. It was the
[url=http://www.w3schools.com/xsl/el_for-each.asp]<xsl:for-each>[/url]
element, and simply loops through each iteration of the given element.

To wit:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:wt="http://www.example.com" exclude-result-prefixes="wt">
<xsl:output method="html" version="4.01"/>
<xsl:output doctype-system="http://www.w3c.org/tr/html4/strict.dtd"/>
<xsl:output doctype-public="-//W3c//DTD HTML 4.01//EN"/>
<xsl:template match="/">
<html>
<head>
<title>XML Test</title>
</head>
<body>
<xsl:for-each select="wt:weetest/wt:elementwrapper">
<div>
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of disable-output-escaping="no" select="wt:data_uri"/>
</xsl:attribute>
<xsl:value-of select="wt:data_name"/>
</xsl:element>
</div>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Unlike other languages, XSL only has one loop statement. As it was developed explicitly for parsing files, this is appropriate. I'm sure that some of the extensions have added more loops.

I also needed to figure out how XSL does subroutines. It does this through the highly intuitive process of declaring

<xsl:template>
elements, and then calling them with the
[url=http://www.w3schools.com/xsl/el_call-template.asp]<xsl:call-template>[/url]
element.

You pass parameters to the "subroutine" by the equally as intuitive

[url=http://www.w3schools.com/xsl/el_with-param.asp]<xsl:with-param>[/url]
element, and access it inside of the "subroutine" with two more elements: the
[url=http://www.w3schools.com/xsl/el_param.asp]<xsl:param>[/url]
element and the
[url=http://www.w3schools.com/xsl/el_value-of.asp]<xsl:value-of>[/url]
element. (The
<xsl:value-of>
element is actually a very important multipurpose element that you use in just about everything).

To wit:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:wt="http://www.example.com" exclude-result-prefixes="wt">
<xsl:output method="html" version="4.01"/>
<xsl:output doctype-system="http://www.w3c.org/tr/html4/strict.dtd"/>
<xsl:output doctype-public="-//W3c//DTD HTML 4.01//EN"/>
<xsl:template match="/">
<html>
<head>
<title>XML Test</title>
</head>
<body>
<xsl:for-each select="wt:weetest/wt:elementwrapper">
<xsl:call-template name="render_one">
<xsl:with-param name="passed_in" select='"&lt;h1>FROM ABOVE&lt;/h1>"'/>
</xsl:call-template>

</xsl:for-each>
</body>
</html>
</xsl:template>
<xsl:template name="render_one">
<xsl:param name="passed_in"/>
<xsl:value-of select="$passed_in" disable-output-escaping="yes"/>
<div>
<xsl:choose>
<xsl:when test="wt:data_uri!= ''">
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of disable-output-escaping="yes" select="wt:data_uri"/>
</xsl:attribute>
<xsl:value-of select="wt:data_name"/>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="wt:data_name"/>
</xsl:otherwise>
</xsl:choose>
</div>
</xsl:template>

</xsl:stylesheet>