Xalan-Java Version 2.7.1 Extensions - Redirect

Xalan-Java Version 2.7.1 Extensions: Extension elements and functions provide a powerful mechanism for extending and simplifying what you can do with an XLST processor like Xalan. With input and contributions from the XML open-source developer community, we are working on placing the most useful extensions in an extensions library distributed with Xalan-Java.

Redirect Extension: A standard XSL transformation involves an XSL stylesheet, an XML source tree, and the transformation result tree. The transformation sends the entire result to a single javax.xml.transform.Result object.

The namespace for the Redirect extension is: http://xml.apache.org/xalan/redirect


It supplies three extension elements that you can use to redirect portions of your transformation output to multiple files: <open>, <write>, and <close>. If you use the <write> element alone, the extension opens a file, writes to it, and closes the file immediately. If you want explicit control over the opening and closing of files, use <write> in conjunction with the <open> and <close> elements.

The <open> and <write> elements include a file attribute and/or a select attribute to designate the output file. The file attribute takes a string, so you can use it to directly specify the output file name. The select attribute takes an XPath expression, so you can use it to dynamically generate the output file name. If you include both attributes, the Redirect extension first evaluates the select attribute, and falls back to the file attribute if the select attribute expression does not return a valid file name.

The <open> and <write> elements also support an append attribute. If the append attribute is set to true or yes, then the result is appended to the output file.

Simple Example of using Redirect extension with XSLT:
I have a XML file which contains some set of XML elements as shown in Source.XML below. My requirement is, I want to move all <REF1> elements to file REF1.XML, all <REF2> elements to REF2.XML and so on.

A simple XSLT using Redirect extension from Xalan can do the trick as shown in MultiRedirect.XSL. I am selecting all <REF1> elements and copying it to REF1.XML using Redirect extension and Similar for all <REF2>, <REF3>, <REF4> and <REF5> elements. On XSLT transformation of Source.XML by MultiRedirect.XSL will create five new XML files like REF1.XML, REF2.XML, REF3.XML, REF4.XML and REF5.XML as shown below.

Before running this example you should have Xalan-J 2.7.1 available to program. I mean you should have Xalan-J 2.7.1 downloaded and set on CLASSPATH. Refer Bibliography for downloads and other references of Xalan-J 2.7.1

Source.XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<Invoice>
 <InvoiceHeader>
  
  <!-- Gruop of Ref1 Elements to go in file REF1.XML -->
  <Ref1 code="1">
   <text>Reference Text 1</text>
  </Ref1>
  <Ref1 code="1">
   <text>Reference Text 2</text>
  </Ref1>

  <!-- Gruop of Ref2 Elements to go in file REF2.XML -->
  <Ref2 code="2">
   <text>Reference Text 1</text>
  </Ref2>
  <Ref2 code="2">
   <text>Reference Text 2</text>
  </Ref2>

 </InvoiceHeader>
</Invoice>

MultiRedirect.XSL
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:redirect="http://xml.apache.org/xalan/redirect"
 extension-element-prefixes="redirect">
 <xsl:variable name="REF1" select="'REF1.XML'" />
 <xsl:variable name="REF2" select="'REF2.XML'" />
 <xsl:template match="/">
 
  <!-- Redirecting REF1 Elementns to REF1.XML -->
  <redirect:open select="$REF1" />
  <redirect:write select="$REF1">
   <Invoice>
    <InvoiceHeader>
     <xsl:copy-of select="Invoice/InvoiceHeader/Ref1" />
    </InvoiceHeader>
   </Invoice>
  </redirect:write>
  <redirect:close select="$REF1" />
  
  <!-- Redirecting REF2 Elementns to REF2.XML -->
  <redirect:open select="$REF2" />
  <redirect:write select="$REF2">
   <Invoice>
    <InvoiceHeader>
     <xsl:copy-of select="Invoice/InvoiceHeader/Ref2" />
    </InvoiceHeader>
   </Invoice>
  </redirect:write>
  <redirect:close select="$REF2" />
  
 </xsl:template>
</xsl:stylesheet>

REF1.XML
<?xml version="1.0" encoding="UTF-8"?>
<Invoice>
 <InvoiceHeader>
  <Ref1 code="1">
   <text>Reference Text 1</text>
  </Ref1>
  <Ref1 code="1">
   <text>Reference Text 2</text>
  </Ref1>
 </InvoiceHeader>
</Invoice>

REF2.XML
<?xml version="1.0" encoding="UTF-8"?>
<Invoice>
 <InvoiceHeader>
  <Ref2 code="2">
   <text>Reference Text 1</text>
  </Ref2>
  <Ref2 code="2">
   <text>Reference Text 2</text>
  </Ref2>
 </InvoiceHeader>
</Invoice>

Bibliography:
Apache Organization, Xalan-Java Extensions, Redirect Extension, Example with the Redirect extension, Download Xalan-J 2.7.1

Comments