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

Am trying to split the xml file in to multiple xml files based on the xml tagin the input xml file.

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=";>
        <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=";>
        <events>
            <id>1739821652558</id>
            <carrier>UPSN</carrier>
        </events>
    </ns0:MT_SHIPMENT>



    <?xml version="1.0" encoding="UTF-8"?>
    <ns0:MT_SHIPMENT xmlns:ns0=";>
        <events>
            <id>4239821652567</id>
            <carrier>USPS</carrier>
        </events>
    </ns0:MT_SHIPMENT>


<?xml version="1.0" encoding="UTF-8"?>
<ns0:MT_SHIPMENT xmlns:ns0=";>
    <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=";>
    <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

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/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/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/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/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./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

Share Improve this question asked Feb 21 at 3:20 ravitejaraviteja 217 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

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./1999/XSL/Transform"
xmlns:ns0="http://dtc/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>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745168655a4614789.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信