Here is an example of how we would normally use XSLT:
// During app startupTo use XInclude within an XSLT stylesheet we need to read it using a DocumentBuilder which has been created a DocumentBuilderFactory. This has the methods that allows us to enable XInclude.
TransformerFactory transformerFactory =
TransformerFactory.newInstance();
Templates stylesheet = transformerFactory.newTemplates(
new StreamSource("stylesheet.xsl"));
// Transformation
Source source = new StreamSource(new File("input.xml"));
Result result = new StreamResult(new File("output.xml"));
stylesheet.newTransformer().transform(source, result);
// During app startupIt is bit more verbose than the orginal, but this is the only way I have found to do this.
DocumentBuilderFactory documentBuilderFactory =
DocumentBuilderFactory.newInstance(); documentBuilderFactory.setXIncludeAware(true);
documentBuilderFactory.setNamespaceAware(true);
// We don't want xml:base and xml:lang attributes in the output.
documentBuilderFactory.setFeature(
"http://apache.org/xml/features/xinclude/fixup-base-uris", false);
documentBuilderFactory.setFeature(
"http://apache.org/xml/features/xinclude/fixup-language", false);
DocumentBuilder docBuilder =
documentBuilderFactory.newDocumentBuilder();
Document stylesheetDoc = docBuilder.parse("stylesheet.xsl");
Source stylesheetSource = new DOMSource(stylesheetDoc);
TransformerFactory transformerFactory =
TransformerFactory.newInstance();
Templates stylesheet =
transformerFactory.newTemplates(stylesheetSource);
// Transformation
Source source = new StreamSource(new File("input.xml"));
Result result = new StreamResult(new File("output.xml"));
stylesheet.newTransformer().transform(source, result);
The stylesheet this uses might look like this:
<?xml version="1.0"?>And the included sub-stylesheet that uses the data elements within the myRootElements might be:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xi="http://www.w3.org/2001/XInclude"
exclude-result-prefixes="xi xsl xml">
<xsl:output method="html" />
<xsl:template match="/myRootElement">
<xi:include href="mail.html" parse="xml" />
</xsl:template>
</xsl:stylesheet>
<html xmlns:xsl="http://www.w3.org/1999/XSL/Transform">Of course you may wish to use XInclude within the input XML, in which case you need to build a DOMSource for the transformation in the same way.
<body>
<p><xsl:value-of select="name"/></p>
<p><xsl:value-of select="email"/></p>
<ul>
<xsl:for-each select="list/a">
<li><xsl:value-of select="."/></li>
</xsl:for-each>
</ul>
</body>
</html>
No comments:
Post a Comment