The general java code i use to process XSLT
and XML
files are :
public static final String transformXmlDocument(String inputXmlString,
File xsltFile) {
TransformerFactory factory = TransformerFactory.newInstance();
StreamSource xslt = new StreamSource(xsltFile);
StreamSource text = new StreamSource(new StringReader(inputXmlString));
StringWriter writer = new StringWriter();
StreamResult textOP = new StreamResult(writer);
try {
Transformer transformer = factory.newTransformer(xslt);
transformer.transform(text, textOP);
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e2) {
e2.printStackTrace();
}
String results = writer.toString();
return results;
}
I have to process an XSLT
of 3.0 version to use the following function :
parse-xml-fragment()
It throws error for this version of XSLT
saying:
parse-xml-fragment() not found as a function
My input XML :
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<data>
<![CDATA[<pi>hi</pi>]]>
</data>
XSLT code:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:data="http://example.com/data"
xmlns:text="http://exselt.net/text"
xmlns:err="http://www.w3.org/2005/xqt-errors"
exclude-result-prefixes="xs xsl data text err"
version="3.0">
<xsl:output indent="yes"/>
<xsl:template match="/">
<xsl:variable name="sample">
<xsl:copy-of select="parse-xml-fragment('<gi>surface</gi><gi>surface</gi><gi>surface</gi>')" />
</xsl:variable>
<final>
<xsl:copy-of select="data/pi"/>
<xsl:for-each select="$sample/gi">
<pi><xsl:value-of select="."/></pi>
</xsl:for-each>
</final>
</xsl:template>
</xsl:stylesheet>
expected output:
<final>
<pi>hi</pi>
<pi>surface</pi>
<pi>surface</pi>
<pi>surface</pi>
</final>
Can anyone please provide a solution ?
You will need to make sure Saxon 9.8 HE or PE or EE is on your class path, HE is available on Sourceforge and Maven, the commercial editions PE and EE from saxonica.com. See http://saxonica.com/html/documentation/about/installationjava/installingjava.html and also http://saxonica.com/html/documentation/using-xsl/embedding/jaxp-transformation.html which recommend, once you have installed a particular edition, to use e.g. http://saxonica.com/html/documentation/javadoc/net/sf/saxon/TransformerFactoryImpl.html directly instead of relying on the JAXP class loader mechanism, so assuming you have Saxon 9.8 HE installed you can replace
TransformerFactory factory = TransformerFactory.newInstance();
with
TransformerFactory factory = new net.sf.saxon.TransformerFactoryImpl();
来源:https://stackoverflow.com/questions/44796871/how-to-use-xslt-3-0-from-a-java-application