0
votes

I am facing a problem with my XSLT transformation. I want to transform xml from one form to other. My Input XML root tag is .

Input XML:

<ICWRRsp>
<IcwrId>379505</IcwrId>
<IcwrId>379506</IcwrId>
<IcwrId>379507</IcwrId>
<IcwrId>379508</IcwrId>
<IcwrId>379509</IcwrId>
<IcwrId>379510</IcwrId>
<WorkId>1920305</WorkId>
<WorkId>1920475</WorkId>
<WorkId>1920673</WorkId>
<WorkId>1920676</WorkId>
<WorkId>1920717</WorkId>
<WorkId>1920729</WorkId>
<Jurisdiction>V1</Jurisdiction>
<Jurisdiction>V1</Jurisdiction>
<Jurisdiction>V1</Jurisdiction>
<Jurisdiction>V1</Jurisdiction>
<Jurisdiction>MD</Jurisdiction>
<Jurisdiction>MD</Jurisdiction>
<IcgsWc>0FCC</IcgsWc>
<IcgsWc>0FCC</IcgsWc>
<IcgsWc>0FCC</IcgsWc>
<IcgsWc>0FCC</IcgsWc>
<IcgsWc>0FEN</IcgsWc>
<IcgsWc>0FEN</IcgsWc>
<WcId>0</WcId>
<WcId>0</WcId>
<WcId>0</WcId>
<WcId>0</WcId>
<WcId>0</WcId>
<WcId>0</WcId>
<StatusCode>0</StatusCode>
<StatusDesc>SUCESS</StatusDesc>
</ICWRRsp>

Output XML:

<ICWRRsp>
<ICWR>
    <IcwrId>379505</IcwrId>
    <WorkId>1920305</WorkId>
    <Jurisdiction>V1</Jurisdiction>
    <IcgsWc>0FCC</IcgsWc>
    <WcId>0</WcId>
</ICWR> 
<ICWR>
    <IcwrId>379505</IcwrId>
    <Jurisdiction>V1</Jurisdiction>
    <IcgsWc>0FCC</IcgsWc>
    <WcId>0</WcId>
</ICWR> 
<StatusCode>0</StatusCode>
<StatusDesc>SUCESS</StatusDesc>

XSLT:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <ICWRRsp>
            <xsl:for-each select="ICWRRsp">
                <ICWR>
                    <IcwrId><xsl:value-of select="IcwrId"/></IcwrId>
                    <WorkId><xsl:value-of select="WorkId"/></WorkId>
                    <Jurisdiction><xsl:value-of select="Jurisdiction"/></Jurisdiction>
                    <IcgsWc><xsl:value-of select="IcgsWc"/></IcgsWc>
                    <WcId><xsl:value-of select="WcId"/></WcId>
                </ICWR> 
            </xsl:for-each>             
        </ICWRRsp>
    </xsl:template>
</xsl:stylesheet>

I have written the XSLT, but It's not iterating. I am stuck in looping. I am getting the following output:

<?xml version="1.0" encoding="UTF-8"?>
<ICWRRsp>
   <ICWR>
      <IcwrId>379505</IcwrId>
      <WorkId>1920305</WorkId>
      <Jurisdiction>V1</Jurisdiction>
      <IcgsWc>0FCC</IcgsWc>
      <WcId>0</WcId>
   </ICWR>

 </ICWRRsp>

Can anyone please help me to write the XSLT?

1

1 Answers

1
votes

There is only one ICWRRsp element in your XML. It is the root element, and so your xsl:for-each is only ever going to do one thing.

It looks like you want one ICWR element per IcwrId, so you need to select the IcwrId elements

<xsl:for-each select="ICWRRsp/IcwrId">

The only issue is then with getting the associated elements, which are following siblings, and not children.

To do this, first store the position of the current IcwrId element in a variable:

<xsl:variable name="pos" select="position()" />

Then, to get the other elements, you do this, for example...

<WorkId><xsl:value-of select="following-sibling::WorkId[$pos]"/></WorkId>

i.e. Get the element that occurs in the same position as the IcwrId element

Try this XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" />
    <xsl:template match="/">
        <ICWRRsp>
            <xsl:for-each select="ICWRRsp/IcwrId">
                <xsl:variable name="pos" select="position()" />
                <ICWR>
                    <IcwrId><xsl:value-of select="."/></IcwrId>
                    <WorkId><xsl:value-of select="following-sibling::WorkId[$pos]"/></WorkId>
                    <Jurisdiction><xsl:value-of select="following-sibling::Jurisdiction[$pos]"/></Jurisdiction>
                    <IcgsWc><xsl:value-of select="following-sibling::IcgsWc[$pos]"/></IcgsWc>
                    <WcId><xsl:value-of select="following-sibling::WcId[$pos]"/></WcId>
                </ICWR> 
            </xsl:for-each>             
        </ICWRRsp>
    </xsl:template>
</xsl:stylesheet>