130 likes | 208 Views
More on XSLT. More on XSLT variables. Earlier we saw two ways to associate a value with a variable A variable whose value is the empty string, for example <xsl:variable name="someVariable" /> A variable whose value is specified by the select attribute Examples
E N D
More on XSLT variables • Earlier we saw two ways to associate a value with a variable • A variable whose value is the empty string, for example • <xsl:variable name="someVariable" /> • A variable whose value is specified by the select attribute Examples <xsl:variable name="someVariable" select="./name" /> <xsl:variable name="someVariable" select="'name'" /> • But there is a third way, which allow greater flexibility • It involves using a non-empty xsl:variable element • The content specifies the value of the variable • We can use the full range of XSLT elements to compute the value
Computing a contingent value for an xsl:variable <?xml version="1.0"?> <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" > <xsl:template match="/"> <xsl:variable name="numberOfPeople"> <xsl:choose> <xsl:when test="count(/people/person[age>21]) > 1"> too many old people </xsl:when> <xsl:otherwise> <xsl:value-of select="count(/people/person)"/> </xsl:otherwise> </xsl:choose> </xsl:variable> <body>The number of people is: <xsl:value-of select="$numberOfPeople"/></body> </xsl:template> </xsl:transform>
Computing a cumulative value for an xsl:variable <?xml version="1.0"?> <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" > <xsl:template match="/"> <xsl:variable name="agesOfPeople"> <xsl:for-each select="/people/person"> <xsl:text> </xsl:text> <xsl:value-of select="age"/> </xsl:for-each> </xsl:variable> <body>The ages of the people are: <xsl:value-of select="$agesOfPeople"/></body> </xsl:template> </xsl:transform>
Computing a complex cumulative value for an xsl:variable <?xml version="1.0"?> <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" > <xsl:template match="/"> <xsl:variable name="agesOfPeople"> <xsl:for-each select="/people/person"> <xsl:value-of select="name"/><xsl:text>:</xsl:text><xsl:value-of select="age"/> <xsl:text> </xsl:text> </xsl:for-each> </xsl:variable> <body>The ages of the people are: <xsl:value-of select="$agesOfPeople"/></body> </xsl:template> </xsl:transform>
Aside: the position() function • When a sequence of items is being processed, this function return the position of the current item within the sequence • This is true whether the sequence of items being processed by a • for-each element • or an apply-templates element
Terminating a cumulative value for an xsl:variable • This program does not append a comma to the last item in the sequence <?xml version="1.0"?> <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" > <xsl:template match="/"> <xsl:variable name="agesOfPeople"> <xsl:for-each select="/people/person"> <xsl:value-of select="age"/> <xsl:if test="position() < count(/people/person)"><xsl:text>, </xsl:text> </xsl:if> </xsl:for-each> </xsl:variable> <body>The ages of the people are: <xsl:value-of select="$agesOfPeople"/></body> </xsl:template> </xsl:transform>
Aside: the last() function • When a sequence of items is being processed, this function return the position of the last item within the sequence • This is true whether the sequence of items being processed by • a for-each element • or an apply-templates element
Terminating a cumulative value for an xsl:variable, version 2 • This program uses last() to avoid placing a comma after last item in sequence <?xml version="1.0"?> <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" > <xsl:template match="/"> <xsl:variable name="agesOfPeople"> <xsl:for-each select="/people/person"> <xsl:value-of select="age"/> <xsl:if test="position() < last()"><xsl:text>, </xsl:text></xsl:if> </xsl:for-each> </xsl:variable> <body>The ages of the people are: <xsl:value-of select="$agesOfPeople"/></body> </xsl:template> </xsl:transform>
Aside: templates can be used when computing values for variables • We can use the full range of XSLT elements to compute the value • This includes using the apply-templates element
Using apply-templates to compute the value for a variable <?xml version="1.0"?> <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" > <xsl:template match="/"> <xsl:variable name="agesOfPeople"> <xsl:apply-templates select="/people/person[age > 21]"/> </xsl:variable> <body>The ages of the people over 21 are <xsl:value-of select="$agesOfPeople"/></body> </xsl:template> <xsl:template match="person"> <xsl:value-of select="age"/> <xsl:if test="position() < count(/people/person)"><xsl:text>, </xsl:text></xsl:if> </xsl:template> </xsl:transform>
Aside: More on sorting • We have seen that sort elements can be used inside for-each elements • They can also be used inside apply-templates elements
Sorting inside an apply-templates element <?xml version="1.0"?> <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" > <xsl:template match="/"> <xsl:variable name="agesOfPeople"> <xsl:apply-templates select="/people/person"> <xsl:sort select="name"/> </xsl:apply-templates> </xsl:variable> The ages of the sorted people are <xsl:value-of select="$agesOfPeople" /> </xsl:template> <xsl:template match="person"> <xsl:value-of select="age"/><xsl:if test="position() < count(/people/person)"><xsl:text>, </xsl:text> </xsl:if> </xsl:template> </xsl:transform>