1 / 13

More on XSLT

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

ifama
Download Presentation

More on XSLT

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. More on XSLT

  2. 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

  3. 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>

  4. 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>

  5. 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>

  6. 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

  7. 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() &lt; 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>

  8. 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

  9. 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() &lt; 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>

  10. 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

  11. 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() &lt; count(/people/person)"><xsl:text>, </xsl:text></xsl:if> </xsl:template> </xsl:transform>

  12. 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

  13. 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() &lt; count(/people/person)"><xsl:text>, </xsl:text> </xsl:if> </xsl:template> </xsl:transform>

More Related