Vaibhav Save
Vaibhav Save

Reputation: 56

add incremental value in xslt tag

 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <s:Header xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" />
  <soapenv:Body>
    <swiftResponseResponse xmlns="http://webservice.sbi.com">
      <swiftResponseReturn>15617TS006140|DC768736|13321.49|04-05-2017 15:13:03|SWIFTINR|NA|FAIL-ACCOUNT UNAVAILABLE</swiftResponseReturn>
    </swiftResponseResponse>
  </soapenv:Body>
</soapenv:Envelope>

and my xsl input is

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://webservice.sbi.com" exclude-result-prefixes="soapenv xsl  xsd xsi xs ">
  <!--<xsl:output method="xml" indent="yes"/>-->
  <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <!--<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>-->


  <xsl:template name="tokenize">
    <xsl:param name="text"/>
    <xsl:param name="separator" />
    <xsl:choose>
        <xsl:when test="not(contains($text, $separator))">
            <swiftResponseReturnValue>
                <xsl:value-of select="$text"/>
            </swiftResponseReturnValue>
        </xsl:when>
        <xsl:otherwise>
            <swiftResponseReturnValue>
                <xsl:value-of select="normalize-space(substring-before($text, $separator))"/>
            </swiftResponseReturnValue>
            <xsl:call-template name="tokenize">
                <xsl:with-param name="text" select="substring-after($text, $separator)"/>
                <xsl:with-param name="separator" select="$separator"/>
            </xsl:call-template>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
  <!--<xsl:template match="soapenv:Envelope/soapenv:Body/xs:swiftResponseResponse/xs:swiftResponseReturn">-->
   <xsl:template match="xs:swiftResponseReturn">
  <swiftResponseReturn>
     <xsl:call-template name="tokenize">
         <xsl:with-param name="text" select="."/>
         <xsl:with-param name="separator" select="'|'"/>
     </xsl:call-template>
  </swiftResponseReturn>
</xsl:template>
</xsl:stylesheet>

my output is

     <swiftResponseReturn>
   <swiftResponseReturnValue>15617TS006140</swiftResponseReturnValue>
   <swiftResponseReturnValue>DC768736</swiftResponseReturnValue>
   <swiftResponseReturnValue>13321.49</swiftResponseReturnValue>
   <swiftResponseReturnValue>04-05-2017 15:13:03</swiftResponseReturnValue>
   <swiftResponseReturnValue>SWIFTINR</swiftResponseReturnValue>
   <swiftResponseReturnValue>NA</swiftResponseReturnValue>
   <swiftResponseReturnValue>FAIL-ACCOUNT UNAVAILABLE</swiftResponseReturnValue>
</swiftResponseReturn>

i want my expected output should be like this

    <swiftResponseReturn>
   <swiftResponseReturnValue1>15617TS006140</swiftResponseReturnValue1>
   <swiftResponseReturnValue2>DC768736</swiftResponseReturnValue2>
   <swiftResponseReturnValue3>13321.49</swiftResponseReturnValue3>
   <swiftResponseReturnValue4>04-05-2017 15:13:03</swiftResponseReturnValue4>
   <swiftResponseReturnValue5>SWIFTINR</swiftResponseReturnValue5>
   <swiftResponseReturnValue6>NA</swiftResponseReturnValue6>
   <swiftResponseReturnValue7>FAIL-ACCOUNT UNAVAILABLE</swiftResponseReturnValue7>
</swiftResponseReturn>

can help me to add ids in my tag or any other value that can uniquely identify my columns.please help been stuck here for while.

Upvotes: 1

Views: 59

Answers (2)

Martin Honnen
Martin Honnen

Reputation: 167446

Having an index in element names is usually not considered a good XML design but if you are sure you want that you could add a parameter to your template and use it, either in the element name or perhaps better in an attribute:

  <xsl:template name="tokenize">
    <xsl:param name="text"/>
    <xsl:param name="separator" />
    <xsl:param name="index" select="1"/>
    <xsl:choose>
        <xsl:when test="not(contains($text, $separator))">
            <swiftResponseReturnValue pos="{$index}">
                <xsl:value-of select="$text"/>
            </swiftResponseReturnValue>
        </xsl:when>
        <xsl:otherwise>
            <swiftResponseReturnValue pos="{$index}">
                <xsl:value-of select="normalize-space(substring-before($text, $separator))"/>
            </swiftResponseReturnValue>
            <xsl:call-template name="tokenize">
                <xsl:with-param name="text" select="substring-after($text, $separator)"/>
                <xsl:with-param name="separator" select="$separator"/>
                <xsl:with-param name="index" select="$index + 1"/>
            </xsl:call-template>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

If you want to construct the element name with an index then use <xsl:element name="swiftResponseReturnValue{$index}">...</xsl:element> instead of <swiftResponseReturnValue pos="{$index}">.

Upvotes: 3

Kris
Kris

Reputation: 41827

Firstly, why would you want to do that? it makes processing the result more difficult. You should be using elementnames that describe what their content means.

That being said, you could add a third optional parameter to the tokenize template, fill it with 1 by default and add 1 to it on the recursive calls to tokenize. Then use that parameter to construct the tagname. something like:

<xsl:element name="'swiftResponseReturnValue'+$myThirdParameter">

<xsl:template name="tokenize">
  <xsl:param name="text"/>
  <xsl:param name="separator" />
  <xsl:param name="index" select="1"/>
  <xsl:choose>
      <xsl:when test="not(contains($text, $separator))">
          <xsl:element name="'swiftResponseReturnValue'+$index">
              <xsl:value-of select="$text"/>
          <xsl:element>
      </xsl:when>
      <xsl:otherwise>
          <swiftResponseReturnValue pos="{$index}">
              <xsl:value-of select="normalize-space(substring-before($text, $separator))"/>
          </swiftResponseReturnValue>
          <xsl:call-template name="tokenize">
              <xsl:with-param name="text" select="substring-after($text, $separator)"/>
              <xsl:with-param name="separator" select="$separator"/>
              <xsl:with-param name="index" select="$index + 1"/>
          </xsl:call-template>
      </xsl:otherwise>
  </xsl:choose>
</xsl:template>

Upvotes: 1

Related Questions