1

I have an input as follows:

<Record>
<OldList>,Old1,Old2,Old3</OldList>
<NewList>,New1,New2,New3</NewList>
<Record>

I have the output os the XSLT transform to be something like below:

<Records>
  <Record>
    <Old>Old1</Old>
    <New>New1</New>
  </Record>
  <Record>
     <Old>Old2</Old>
     <New>New2</New>
  </Record>
  <Record>
     <Old>Old3</Old>
     <New>New3</New>
   </record>
</Records>

Can you please help me to get the above desired output? I am using XSLT 1.0

Daniel Haley
  • 51,389
  • 6
  • 69
  • 95
  • XSLT 1.0 or 2.0? Or if you don't know that, then what program/library/etc. are you using to process it? – Ian Roberts Dec 12 '14 at 16:18
  • Which processor? You can save a lot of work here if your processor supports the EXSLT str:tokenize() extension function. -- P.S. Do your strings really have a leading comma? – michael.hor257k Dec 12 '14 at 16:33
  • Yes my string as leading comma. No, my processor does not support EXSLT – ayankumar1990 Dec 12 '14 at 16:38
  • 1
    Then you'll need to use a recursive template to do the tokenizing - see an example here: http://stackoverflow.com/questions/23597058/how-to-split-string-in-xml/23598237#23598237 – michael.hor257k Dec 12 '14 at 16:43

1 Answers1

0

try out the following xslt

  <xsl:template match="/*">
    <Records>
      <xsl:call-template name="oldData">
        <xsl:with-param name="oData" select="OldList"/>
        <xsl:with-param name="nData" select="NewList"/>
      </xsl:call-template>
    </Records>
  </xsl:template>
  <xsl:template name="oldData">
    <xsl:param name="oData"/>
    <xsl:param name="nData"/>
    <xsl:variable name="ofirst" select="substring-before($oData,',')"/>
    <xsl:variable name="olast" select="substring-after($oData,',')"/>
    <xsl:variable name="nfirst" select="substring-before($nData,',')"/>
    <xsl:variable name="nlast" select="substring-after($nData,',')"/>
    <xsl:choose>
      <xsl:when test="($ofirst!='') and ($nfirst!='')">
        <record>
          <Old>
            <xsl:value-of select="$ofirst"/>
          </Old>
          <New>
            <xsl:value-of select="$nfirst"/>
          </New>
        </record>
        <xsl:call-template name="oldData">
          <xsl:with-param name="oData" select="substring-after($oData,concat($ofirst,','))"/>
          <xsl:with-param name="nData" select="substring-after($nData,concat($nfirst,','))"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:when test="$olast!='' and $nlast!=''">
        <xsl:call-template name="oldData">
          <xsl:with-param name="oData" select="$olast"/>
          <xsl:with-param name="nData" select="$nlast"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <record>
          <Old>
            <xsl:value-of select="$oData"/>
          </Old>
          <New>
            <xsl:value-of select="$nData"/>
          </New>
        </record>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

Creating mutable variables in XSLT 1.0 is not an option. That can be however achieved by recursion. That is how the problem can be solved

Saurav
  • 592
  • 4
  • 21
  • Perhaps you could add an explanation to your answer? In my opinion, doing so would mean adding a lof of value to your answer. Also, there is no need for an `ext:` prefix and `exclude-result-prefixes` - and you should point out the fact that the OP's XML input is not well-formed. – Mathias Müller Dec 12 '14 at 18:58
  • @Mathias Muller pardon that xmlns. I thought i had copied only the template portion.stylesheet portion was from something that i was trying.Am editing it – Saurav Dec 13 '14 at 04:58