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 ?