How do I declare a sequence in XSLT?

I need to declare a fixed sequence of numbers. How to do it?

For example this (I'm assuming here):

<xsl:element name="xsl:param">
    <xsl:attribute name="name">MySequence</xsl:attribute>
    <xsl:sequence>(1,2,3,4)</xsl:sequence>
</xsl:element>

      

or

<xsl:element name="xsl:param">
    <xsl:attribute name="name">MySequence</xsl:attribute>
    <xsl:sequence>1,2,3,4</xsl:sequence>
</xsl:element>

      

or what?

thank

+3


source to share


2 answers


If you are using XSLT 2.0 you can simply create the sequence directly in select

, for example:

<xsl:param name="MySequence" select="('1','2','3','4')"/>

      

XSLT validation ...

XSLT 2.0



<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>
    <xsl:strip-space elements="*"/>

    <xsl:param name="seq" select="('23453','74365','98','653')"/>

    <xsl:template match="/">
        <xsl:for-each select="$seq">
            <xsl:value-of select="concat('Item ',position(),': ',.,'&#xA;')"/>
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>

      

applies to any XML input:

Item 1: 23453
Item 2: 74365
Item 3: 98
Item 4: 653

      

+5


source


To create a sequence in the XSLT 2.0 sense, you use select

eg.

<xsl:sequence select="1 to 4" />

      

But if you are adding a value to an element, you may prefer value-of

<xsl:value-of select="1 to 4" separator="," />

      

Given this snippet in the question, it will generate output XML



<xsl:param name="MySequence">1,2,3,4</xsl:param>

      

Which makes the value of the generated parameter a comma separated string. If you really want the value to be param

a sequence in the generated XSLT, you need to generate the attribute select

instead of using the content of the element

<xsl:element name="xsl:param">
  <xsl:attribute name="name" select="'MySequence'"/>
  <xsl:attribute name="select">
    <xsl:text>(</xsl:text>
    <xsl:value-of select="1 to 4" separator=","/>
    <xsl:text>)</xsl:text>
  </xsl:attribute>
</xsl:element>

      

Receiving output

<xsl:param name="MySequence" select="(1,2,3,4)" />

      

+1


source







All Articles