10 Replies Latest reply: Mar 27, 2013 5:58 AM by PhHein RSS

    Split XML files with XSL result document

    843834
      Hi All,
      I have below xml file...


      <?xml version="1.0" encoding="ISO-8859-1"?>
      <T0020
      xsi:schemaLocation="http://www.safersys.org/namespaces/T0020V1 T0020V1.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.safersys.org/namespaces/T0020V1">
      <INTERFACE>
      <NAME>SAFER</NAME>
      <VERSION>04.02</VERSION>
      </INTERFACE>
      <TRANSACTION>
      <VERSION>01.00</VERSION>
      <OPERATION>REPLACE</OPERATION>
      <DATE_TIME>2009-09-01T00:00:00</DATE_TIME>
      <TZ>CT</TZ>
      </TRANSACTION>
      <IRP_ACCOUNT>
      <IRP_CARRIER_ID_NUMBER>274845</IRP_CARRIER_ID_NUMBER>
      <IRP_BASE_COUNTRY>US</IRP_BASE_COUNTRY>
      <IRP_BASE_STATE>AR</IRP_BASE_STATE>
      <IRP_ACCOUNT_NUMBER>55002</IRP_ACCOUNT_NUMBER>
      <IRP_ACCOUNT_TYPE>I</IRP_ACCOUNT_TYPE>
      <IRP_STATUS_CODE>100</IRP_STATUS_CODE>
      <IRP_STATUS_DATE>2007-11-06</IRP_STATUS_DATE>
      <IRP_UPDATE_DATE>2009-08-03</IRP_UPDATE_DATE>
      <IRP_NAME>
      <NAME_TYPE>LG</NAME_TYPE>
      <NAME>A P SUPPLY CO</NAME>
      <IRP_ADDRESS>
      <ADDRESS_TYPE>PH</ADDRESS_TYPE>
      <STREET_LINE_1>1400 N OATS</STREET_LINE_1>
      <STREET_LINE_2/>
      <CITY>TEXARKANA</CITY>
      <STATE>AR</STATE>
      <ZIP_CODE>71854</ZIP_CODE>
      <COUNTY>MILLER</COUNTY>
      <COLONIA/>
      <COUNTRY>US</COUNTRY>
      </IRP_ADDRESS>
      <IRP_ADDRESS>
      <ADDRESS_TYPE>MA</ADDRESS_TYPE>
      <STREET_LINE_1>P O BOX 1927</STREET_LINE_1>
      <STREET_LINE_2/>
      <CITY>TEXARKANA</CITY>
      <STATE>AR</STATE>
      <ZIP_CODE>75504</ZIP_CODE>
      <COUNTY/>
      <COLONIA/>
      <COUNTRY>US</COUNTRY>
      </IRP_ADDRESS>
      </IRP_NAME>
      </IRP_ACCOUNT>
      <IRP_ACCOUNT> ..... </IRP_ACCOUNT>
      <IRP_ACCOUNT> ..... </IRP_ACCOUNT>
      <IRP_ACCOUNT> ..... </IRP_ACCOUNT>
      </T0020>

      and i want to take this xml file and split it into multiple files through java code like this ...

      File1.xml

      <T0020>
      <IRP_ACCOUNT> ..... </IRP_ACCOUNT>
      <IRP_ACCOUNT> ..... </IRP_ACCOUNT>
      </T0020>

      File2.xml

      <T0020>
      <IRP_ACCOUNT> ..... </IRP_ACCOUNT>
      <IRP_ACCOUNT> ..... </IRP_ACCOUNT>
      </T0020>

      so i have applied following xslt ...

      <?xml version="1.0" encoding="UTF-8" ?>
      <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:t="http://www.safersys.org/namespaces/T0020V1" version="2.0">
      <xsl:output method="xml" indent="yes" name="xml" />
      <xsl:variable name="accounts" select="t:T0020/t:IRP_ACCOUNT" />
      <xsl:variable name="size" select="10" />
      <xsl:template match="/">
      <xsl:for-each select="$accounts[position() mod $size = 1]">
      <xsl:variable name="filename" select="resolve-uri(concat('output/',position(),'.xml'))" />
      <xsl:result-document href="{$filename}" format="xml">
      <T0020>
      <xsl:for-each select=". | following-sibling::t:IRP_ACCOUNT[position() < $size]">
      <xsl:copy-of select="." />
      </xsl:for-each>
      </T0020>
      </xsl:result-document>
      </xsl:for-each>
      </xsl:template>
      </xsl:stylesheet>


      Now i want to apply this XSL to xml through Java Code...

      TransformerFactory tFactory = TransformerFactory.newInstance();
      Source xslSource = new StreamSource(xslFilePath);


      Transformer trans = tFactory.newTransformer(xslSource);
      trans.transform(new StreamSource(xmlFileName), new StreamResult( ????));


      here how can i map new StreamResult( ) input parameter with xsl Result document argument ??

      Please help me.....

      Or Can you give me a link of Example which use result document and Java transform method to Output multiple doucment ??

      Here new StreamResult take only 1 file as to be transformed ....
        • 1. Re: Split XML files with XSL result document
          DrClap
          XSLT 1.0 can only produce one result document. It doesn't matter whether you run it from Java or not, it can still only produce one result document no matter how you run it.

          And there is no <xsl:result-document> element in XSLT 1.0. Perhaps you have been reading one of those garbage tutorials which don't distinguish between XSLT 1.0 elements and XSLT 2.0 elements?
          • 2. Re: Split XML files with XSL result document
            843834
            Can you please give me other solution for splitting xml file using xslt ? I am stuck with the problem .Any example will be ok....
            • 3. Re: Split XML files with XSL result document
              802245
              Hi Nisarg,
              nisargmca wrote:
              Can you please give me other solution for splitting xml file using xslt ? I am stuck with the problem .Any example will be ok....
              Xalan-Java XSLT processor with it's Redirect extension can be the easiest solution for this.

              Go through this: [Xalan-Java Version 2.7.1 Extensions - Redirect|http://tejaspurohit.blogspot.com/2010/01/xalan-java-version-271-extensions.html]

              *Cheers,
              typurohit* (TEJAS PUROHIT)
              • 4. Re: Split XML files with XSL result document
                843834
                hi Tejas ,

                I have done as you said but now able to generate multiple xml file .
                I am giving you the xsl file i have used ....

                <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:t="http://www.safersys.org/namespaces/T0020V1" version="2.0">
                     <xsl:output method="xml" indent="yes" name="xml" />
                     <xsl:variable name="accounts" select="t:T0020/t:IRP_ACCOUNT" />
                     <xsl:variable name="size" select="5" />
                     <xsl:template match="/">
                          <xsl:for-each select="$accounts[position() mod $size = 1]">
                               <xsl:variable name="filename" select="concat(position(),'.xml')" />
                                    <xsl:result-document href="{$filename}" format="xml">
                                         <T0020>
                                              <xsl:for-each select=". | following-sibling::t:IRP_ACCOUNT[position() < $size]">
                                                   <xsl:copy-of select="." />
                                              </xsl:for-each>                              
                                         </T0020>
                                    </xsl:result-document>                    
                          </xsl:for-each>
                     </xsl:template>
                     
                </xsl:stylesheet>


                and i have done transformation like this ...

                Transformer trans = tFactory.newTransformer(xslSource);
                trans.transform(new StreamSource(xmlFileName), new DOMResult());

                but not getting any result .

                Can you please help me out ?

                - Nisarg
                • 5. Re: Split XML files with XSL result document
                  416310
                  If you want to use XSLT 2.0's xsl:result-document then you need to use [Saxon 9|http://saxon.sourceforge.net/].
                  • 6. Re: Split XML files with XSL result document
                    802245
                    Hi Nisarg,
                    nisargmca wrote:
                    I have done as you said but now able to generate multiple xml file .
                    . . . . . .
                    but not getting any result .
                    If you are not getting any result, it means there must be something wrong.

                    Put your code into Try Catch block and print full exception stack trace weather is it throwing any error or what? If so, try to resolve them first.

                    [As *Martin_Honnen* said, You need Saxon 9 XSLT Processor for using xsl:result-document.|http://forums.sun.com/thread.jspa?messageID=10918580#10918580]

                    If everything works fine a XML file named "1.XML" should be generated with all <IRP_ACCOUNT> elements copied into it.
                    • 7. Re: Split XML files with XSL result document
                      843834
                      Thanks Martin,
                      Now I have used Saxon 9 to use xslt 2.0 result -document and my problem is solved.
                      Thanks a lot for your help.
                      • 8. Re: Split XML files with XSL result document
                        843834
                        Hi Tejas ,
                        Thanks for your help .
                        I have solved my problem by adding Saxon 9.

                        -Nisarg
                        • 9. Re: Split XML files with XSL result document
                          999496
                          Hi Nisarg , I have a similar problem , I have an xslt whihc outputs multiple xml documents and i want to use saxon in java to generate multiple xml files using the xslt.

                          Can you pleae share the piece of java code tghat works for you.

                          Shiv
                          • 10. Re: Split XML files with XSL result document
                            PhHein
                            Please don't post to years old threads. Start a new one and feel free to link relevant threads.

                            Mod: locking this zombie.