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 =)

4
  • 1
    Is that really XSLT 2.0 with XPath 2.0? Then of course use <xsl:for-each select="1 to 10"><item><xsl:value-of select="."/></item></xsl:for-each>. Commented Jun 15, 2022 at 19:17
  • oh god, it has such a simple solution... Now I feel so dummy ] Thank you so much for help ] Commented Jun 15, 2022 at 19:25
  • This should be an answer rather than just a comment, so the question is listed as having an answer. Commented Jun 16, 2022 at 3: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. Commented Jun 16, 2022 at 9:09

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.