Let's take a simple example. Let's assume that we have a ContactInfo.xml document:
<?xml version="1.0" encoding="UTF-8"?>which includes a PersonName.xml:
<hrxml:ContactInfo xml:lang="EN"
xmlns:hrxml="http://ns.hr-xml.org/2004-08-02"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../schemas/hrxmlResume-2.3.xsd"
xmlns:xi="http://www.w3.org/2001/XInclude">
<xi:include href="contactinfo/PersonName.xml"/>
</hrxml:ContactInfo>
<?xml version="1.0" encoding="UTF-8"?>Each document is fully well-formed and can be transformed individually. In this example, we can create a htmlPersonName.xslt stylesheet to format a person name:
<hrxml:PersonName xml:lang="EN"
xmlns:hrxml="http://ns.hr-xml.org/2004-08-02"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../schemas/hrxmlResume-2.3.xsd"
xmlns:xi="http://www.w3.org/2001/XInclude">
<hrxml:GivenName>David</hrxml:GivenName>
<hrxml:FamilyName>Le Strat</hrxml:FamilyName>
</hrxml:PersonName>
<?xml version="1.0" encoding="UTF-8"?>Leveraging Xalan (2.7.0), we can apply the stylesheet to the well-formed PersonName.xml document.
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:hrxml="http://ns.hr-xml.org/2004-08-02"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:template match="hrxml:PersonName">
<xsl:value-of select="hrxml:GivenName" />
<xsl:text> </xsl:text>
<xsl:value-of select="hrxml:FamilyName" />
</xsl:template>
</xsl:stylesheet>
To do so, we can leverage the Xalan command line utility. In this particular example, we invoke the utility through an ant script as described below:
<java classname="org.apache.xalan.xslt.Process" fork="true" dir="." >The jvmarg are of particular interest as they enable the processing of XML XInclude with the Xerces parser.
<jvmarg value="-Djavax.xml.parsers.DocumentBuilderFactory=
org.apache.xerces.jaxp.DocumentBuilderFactoryImpl"/>
<jvmarg value="-Djavax.xml.parsers.SAXParserFactory=
org.apache.xerces.jaxp.SAXParserFactoryImpl"/>
<jvmarg value="-Dorg.apache.xerces.xni.parser.XMLParserConfiguration=
org.apache.xerces.parsers.XIncludeParserConfiguration"/>
<arg value="-IN"/>
<arg value="${home.dir}/components/contactinfo/${xml.name}.xml"/>
<arg value="-XSL"/>
<arg value="${home.dir}/styles/html${xml.name}.xslt"/>
<arg value="-OUT"/>
<arg value="${home.dir}/output/${xml.name}.html"/>
<arg value="-HTML"/>
<classpath>
...Your classpath...
</classpath>
</java>
In addition, for rendering the ContactInfo.xml, we can leverage XSL include as follow:
<?xml version="1.0" encoding="UTF-8"?<And voila, we have achieved a high level of reusability of both presentation and data components!
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:hrxml="http://ns.hr-xml.org/2004-08-02"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:include href="htmlPersonName.xslt" />
<xsl:template match="hrxml:ContactInfo">
<xsl:apply-templates select="hrxml:PersonName" />
</xsl:template>
</xsl:stylesheet>
No comments:
Post a Comment