0

Need your help. I have some XML file with data before the transformation.

<?xml version="1.0" encoding="UTF-8"?>
<Envelope>
  <filter>
    <type>book</type>
    <pageLimit>200</pageLimit>
    <brand>1</brand>
  </filter>
</Envelope>

And I have an XSLT file. But during transformation, I need to add new nodes to this XSLT file. And the funny thing is that I need to generate this node for example 10 times with numeric ascending (but I don't want to write this stuff by hands).

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes" omit-xml-declaration="yes"/>
    <xsl:template match="/">
        <Envelope>
          <filter>
            <type>
              <xsl:value-of select="/Envelope/filter/type"/>
            </type>
            <pageLimit>
              <xsl:value-of select="/Envelope//filter/pageLimit"/>
            </pageLimit>
            <brand>
              <xsl:value-of select="/Envelope/filter/brand"/>
            </brand>
          </filter>
          
        </Envelope>
    </xsl:template>
</xsl:stylesheet>

In output XML file should look somthing like this:

<Envelope>
   <filter>
      <type>book</type>
      <pageLimit>200</pageLimit>
      <brand>1</brand>
   </filter>
   <item>1</item>
   <item>2</item>
   <item>3</item>
   <item>4</item>
   <item>5</item>
   <item>6</item>
   <item>7</item>
   <item>8</item>
   <item>9</item>
   <item>10</item>   
</Envelope>

I think it is possible to use some variables at XSLT to generate this stuff. But I don't know how to do that. Thanks so much for your help =)

  • 1
    Is that really XSLT 2.0 with XPath 2.0? Then of course use ``. – Martin Honnen Jun 15 '22 at 19:17
  • oh god, it has such a simple solution... Now I feel so dummy ] Thank you so much for help ] – Bc. MD. Peter Trahnyuk Jun 15 '22 at 19:25
  • This should be an answer rather than just a comment, so the question is listed as having an answer. – Conal Tuohy Jun 16 '22 at 03:49
  • I have morphed the suggestion from the comment into an answer so that the question can me marked as solved by accepting the answer. @ConalTuohy, sorry about starting out with a comment, but lots of people somehow show up with XSLT code saying `version="2.0"` but later tell you that your suggestion exploiting some XSLT/XPath 2 feature does not work for them. So that is why I first asked in a comment. – Martin Honnen Jun 16 '22 at 09:09

1 Answers1

1

Assuming your are really using an XSLT 2 or later processor with XPath 2 or later it is as easy as knowing that 1 to 10 constructs the sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 and that way to implement

<xsl:for-each select="1 to 10">
  <item>
    <xsl:value-of select="."/>
  </item>
</xsl:for-each>
Martin Honnen
  • 160,499
  • 6
  • 90
  • 110