0

The following is the Input Message

<ns0:Root xmlns:ns0="LoopInput">
  <MaxCount>6</MaxCount>
  <Detail>
    <Member>
      <CompanyName>XYZ</CompanyName>
      <PersonName>John</PersonName>
      <State>KS</State>
      <Country>USA</Country>
      <Amount>1000</Amount>
      <CombinedState>Yes</CombinedState>
    </Member>
    <Member>
      <CompanyName>ABC</CompanyName>
      <PersonName>Larry</PersonName>
      <State>IL</State>
      <Country>USA</Country>
      <Amount>1000</Amount>
      <CombinedState>No</CombinedState>
    </Member>
  </Detail>
</ns0:Root>

The Output Needs to Look like :

<ns0:Root xmlns:ns0="LoopOutput">
  <Detail>

    <Member>
      <CompanyName>XYZ</CompanyName>
      <PersonName>John</PersonName>
      <State>KS</State>
      <Country>USA</Country>
      <Amount>1000</Amount>
      <CombinedState>Yes</CombinedState>
    </Member>
    <Member>
      <CompanyName>ABC</CompanyName>
      <PersonName>Larry</PersonName>
      <State>IL</State>
      <Country>USA</Country>
      <Amount>1000</Amount>
      <CombinedState>No</CombinedState>
    </Member>
  </Detail>
  <NLoop>
    <Number>9</Number>
  </NLoop>
  <NLoop>
    <Number>9</Number>
  </NLoop>
  <NLoop>
    <Number>9</Number>
  </NLoop>
  <NLoop>
    <Number>9</Number>
  </NLoop>
  <NLoop>
    <Number>9</Number>
  </NLoop>
  <NLoop>
    <Number>9</Number>
  </NLoop>
</ns0:Root>

XSLT 1.0 Attempted

<?xml version="1.0" encoding="UTF-16"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:var="http://schemas.microsoft.com/BizTalk/2003/var" exclude-result-prefixes="msxsl var s0" version="1.0" xmlns:s0="LoopInput" xmlns:ns0="LoopOutput">
  <xsl:output omit-xml-declaration="yes" method="xml" version="1.0" />
  <xsl:template match="/">
    <xsl:apply-templates select="/s0:Root" />
  </xsl:template>
  <xsl:template match="/s0:Root">
    <ns0:Root>
      <Detail>
        <xsl:for-each select="Detail/Member">
          <Member>
            <CompanyName>
              <xsl:value-of select="CompanyName/text()" />
            </CompanyName>
            <PersonName>
              <xsl:value-of select="PersonName/text()" />
            </PersonName>
            <State>
              <xsl:value-of select="State/text()" />
            </State>
            <Country>
              <xsl:value-of select="Country/text()" />
            </Country>
            <Amount>
              <xsl:value-of select="Amount/text()" />
            </Amount>
            <CombinedState>
              <xsl:value-of select="CombinedState/text()" />
            </CombinedState>
          </Member>
        </xsl:for-each>
      </Detail>
    </ns0:Root>
  </xsl:template>
</xsl:stylesheet>

Requirement :

Based on the MaxCounter Field , need to repeat NLoop Record with the field "Number" having constant value of "9"
Example: If the MaxCounter is 6 then need to repeat the N1LoopRecord in output >6 times and if the MaxCounter Value is zero then N1Loop should not be created

Problem :

I tried to isolate this issue and create a separate xslt to test and was not able to do in my own xslt , below is link which i tried to use but still i am failing how to approach this.

Duplicate Element x number of times with XSLT

Appreciate if anyone can help me out with this

sukra
  • 123
  • 1
  • 10
  • Other answers to this question can be found at https://stackoverflow.com/questions/19716449/converting-xhtml-table-to-latex-using-xslt/19717708#19717708 and https://stackoverflow.com/questions/44833374/use-existing-xml-loop-to-generate-full-xml-in-powershell/44834697#44834697 – Michael Kay Feb 25 '19 at 09:17

1 Answers1

2

You can solve this by using a recursive template counting down from MaxCount to 0 (Here, the template is named loop). Adjust this skeleton to your specific needs.

<xsl:template name="loop">
    <xsl:param name="cnt" />
    <xsl:if test="$cnt > 0">
      <NLoop>
        <Number>9</Number>
      </NLoop>      
      <xsl:call-template name="loop">
        <xsl:with-param name="cnt" select="$cnt - 1" />
      </xsl:call-template>
    </xsl:if>  
</xsl:template>

Then call this template at the end of your main template:

<xsl:template match="/s0:Root">
    <ns0:Root>
      <Detail>
        <xsl:for-each select="Detail/Member">
          <Member>
            <CompanyName>
              <xsl:value-of select="CompanyName/text()" />
            </CompanyName>
            <PersonName>
              <xsl:value-of select="PersonName/text()" />
            </PersonName>
            <State>
              <xsl:value-of select="State/text()" />
            </State>
            <Country>
              <xsl:value-of select="Country/text()" />
            </Country>
            <Amount>
              <xsl:value-of select="Amount/text()" />
            </Amount>
            <CombinedState>
              <xsl:value-of select="CombinedState/text()" />
            </CombinedState>
          </Member>
        </xsl:for-each>
      </Detail>
      <!-- Call the 'loop' template -->
      <xsl:call-template name="loop">
        <xsl:with-param name="cnt" select="MaxCount" />
      </xsl:call-template>
    </ns0:Root>
</xsl:template>

The output is as desired.

zx485
  • 28,498
  • 28
  • 50
  • 59