raviteja
raviteja

Reputation: 21

xslt code to split the xml file in to mulitple xml files in runtime based on xml tag <events> and <id>

Am trying to split the xml file in to multiple xml files based on the xml tag / in the input xml file. the individual output xml files should go in sequence as per the input xml /from top to bottom of the input xml, xml tag is have unique id.

All the output individual xml files should be in sequence as per the input xml file.

If the input xml doesnt have root element then the xslt code should ignore input xml file, we don't want the empty xml file.

when we execute the xslt code should generate multiple xml files, its have to split the input xml file in to multiple xml files/individual xml files in runtime not to write to any folder.

input xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <ns0:MT_SHIPMENT xmlns:ns0="http://dtc.com/Shipment">
        <events>
            <id>1739821652558</id>
            <carrier>UPSN</carrier>
        </events>
        <events>
            <id>4239821652567</id>
            <carrier>Fedex</carrier>
        </events>
        <events>
            <id>6239828652573</id>
            <carrier>USPS</carrier>
        </events>
    </ns0:MT_SHIPMENT>

when we run the xslt code the output should be Mulitple xml files/individual xml files separate xml files in runtime: Expected output individual files as below:

 <?xml version="1.0" encoding="UTF-8"?>
    <ns0:MT_SHIPMENT xmlns:ns0="http://dtc.com/Shipment">
        <events>
            <id>1739821652558</id>
            <carrier>UPSN</carrier>
        </events>
    </ns0:MT_SHIPMENT>



    <?xml version="1.0" encoding="UTF-8"?>
    <ns0:MT_SHIPMENT xmlns:ns0="http://dtc.com/Shipment">
        <events>
            <id>4239821652567</id>
            <carrier>USPS</carrier>
        </events>
    </ns0:MT_SHIPMENT>


<?xml version="1.0" encoding="UTF-8"?>
<ns0:MT_SHIPMENT xmlns:ns0="http://dtc.com/Shipment">
    <events>
        <id>6239828652573</id>
        <carrier>USPS</carrier>
    </events>
</ns0:MT_SHIPMENT>

xsltcode:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
  <xsl:template match="/root">
  <xsl:for-each select="/events/*">
    <xsl:result-document method="xml">
      <root>
        <xsl:copy-of select="/root/@*" />
        <elem>
          <xsl:copy-of select="../@* | ." />
        </elem>
      </root>
    </xsl:result-document>
  </xsl:for-each>
</xsl:template> 
</xsl:stylesheet>

Am not getting expected output with xml tags and individual/separate xml's.

    1739821652558
    UPSN


    4239821652567
    Fedex


    6239828652573
    USPS
    

please help us with correct xslt code.

Thanks Ravi

Upvotes: 0

Views: 51

Answers (1)

y.arazim
y.arazim

Reputation: 3250

Your template matches /root but the root element of your input XML is ns0:MT_SHIPMENT. As a result the entire input is processed by the built-in templates.

To get the result you show you would need to do something like:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns0="http://dtc.com/Shipment">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/ns0:MT_SHIPMENT">
    <xsl:for-each select="events">
        <xsl:result-document href="{id}.xml">
            <ns0:MT_SHIPMENT>
                <xsl:copy-of select="."/>
            </ns0:MT_SHIPMENT>
        </xsl:result-document>
    </xsl:for-each>
</xsl:template> 

</xsl:stylesheet>

Upvotes: 3

Related Questions