Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Split xml using xsl

 
S Mithal
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying to split an xml using xslt. For this I am using <redirect:write> which is Xalan specific. Is there an alternative tag I can use and get the same functionality?

This is an example xml file:

<?xml version="1.0" encoding="UTF-8"?>
<MyFType>
<MyRType>
<MCode>
<ID>1</ID>
<Title>ABC</Title>
<ShortSummary>EFG</ShortSummary>
</MCode>
</MyRType>
<MyRType>
<MCode>
<ID>2</ID>
<Title>WER</Title>
<ShortSummary>EFG</ShortSummary>
</MCode>
</MyRType>
</MyFType>

This is the xslt which will split the above into 1.xml and 2.xml

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:redirect="org.apache.xalan.xslt.extensions.Redirect"
extension-element-prefixes="redirect"
version="1.0"
>
<xsl utput method="xml"/>

<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>

<xsl:template match="MyFType">
<xsl:apply-templates />
</xsl:template>

<xsl:template match="MyRType">
<xsl:variable name="filename" select="concat(MCode/ID,'.xml')" />
<redirect:write select="$filename">
<MyRType>
<xsl:apply-templates />
</MyRType>
</redirect:write>
</xsl:template>

<xsl:template match="MCode">
<xsl:copy-of select="." />
</xsl:template>

</xsl:stylesheet>
 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. XSLT 1.0 does not include that functionality, but several of its implementations (Xalan, Saxon that I know of) have provided implementation-specific ways to write to multiple destinations from a single XSLT.

XSLT 2.0 does provide the ability to write to multiple destinations, I believe, but no JRE comes with XSLT 2.0 build in. You would have to get an XSLT 2.0 implementation (they do exist) and make sure that you were calling it from your Java code.
 
S Mithal
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your post. I read the same while trying to explore this issue, but wanted to confirm if there was an alternative to <redirect:write>.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic