I am trying to transform XML using XSLT in Saxon 10.6 HE. Getting error as below.
Error: java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: net.sf.saxon.om.StructuredQName
Input XML:
<create>
<article>
<identifier>Test</identifier>
</article>
<article>
<identifier>Test123</identifier>
</article>
</create>
<update>
<article>
<identifier>Test</identifier>
</article>
</update>
XSLT:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
xmlns:my="http://example.com/my-functions"
expand-text="yes">
<xsl:variable name="vPop" as="element()*">
<item path="/header/txCtry">Test</item>
<item path="/data/txSttlmInf/instgRmbrsmntAgt/BICFI">nijith</item>
<item path="/data/txCityCd">33</item>
</xsl:variable>
<xsl:variable name="new-nodes">
<xsl:sequence select="my:subTree($vPop/@path/concat(.,'/',string(..)))"/>
</xsl:variable>
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/" name="xsl:initial-template">
<xsl:sequence select="my:merge(*, $new-nodes/*)"/>
</xsl:template>
<xsl:function name="my:merge" as="node()*">
<xsl:param name="node1" as="node()*"/>
<xsl:param name="node2" as="node()*"/>
<xsl:for-each-group select="$node1, $node2" group-by="path()">
<xsl:copy>
<xsl:sequence select="my:merge(@*, current-group()[2]/@*)"/>
<xsl:sequence select="my:merge(node(), current-group()[2]/node())"/>
</xsl:copy>
</xsl:for-each-group>
</xsl:function>
<xsl:function name="my:subTree" as="node()*">
<xsl:param name="pPaths" as="xs:string*"/>
<xsl:for-each-group select="$pPaths"
group-adjacent=
"substring-before(substring-after(concat(., '/'), '/'), '/')">
<xsl:if test="current-grouping-key()">
<xsl:choose>
<xsl:when test=
"substring-after(current-group()[1], current-grouping-key())">
<xsl:element name=
"{substring-before(concat(current-grouping-key(), '['), '[')}">
<xsl:sequence select=
"my:subTree(for $s in current-group()
return
concat('/',substring-after(substring($s, 2),'/'))
)
"/>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="current-grouping-key()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:for-each-group>
</xsl:function>
</xsl:stylesheet>
Java code:
Source xslt = new StreamSource(new StringReader(inputXSLT));
Source xml = new StreamSource(new StringReader(inputXML));
StringWriter transformedXML = new StringWriter();
Processor processor = new Processor(false);
XsltCompiler compiler = processor.newXsltCompiler();
XsltExecutable stylesheet = compiler.compile(xslt);
Serializer out = processor.newSerializer(transformedXML);
out.setOutputProperty(Serializer.Property.METHOD, "xml");
out.setOutputProperty(Serializer.Property.INDENT, "yes");
Xslt30Transformer transformer = stylesheet.load30();
transformer.transform(xml, out);
outputXML = transformedXML.toString();
Kindly help to resolve this issue. Is there any way to implement this without using Serializer.
I don't see how you could parse that input lacking a single root element, if you want to do that you need the help of the XPath 3.1 parse-xml-fragment
function
String inputXMLFragment = "<create>\n" +
" <article>\n" +
" <identifier>Test</identifier>\n" +
" </article>\n" +
" <article>\n" +
" <identifier>Test123</identifier>\n" +
" </article>\n" +
"</create>\n" +
"<update>\n" +
" <article>\n" +
" <identifier>Test</identifier>\n" +
" </article>\n" +
"</update>";
XdmNode inputNode = (XdmNode) XdmFunctionItem.getSystemFunction(processor, new QName("http://www.w3.org/2005/xpath-functions", "parse-xml-fragment"), 1)
.call(processor, XdmAtomicValue.makeAtomicValue(inputXMLFragment));
...
transformer.applyTemplates(inputNode, out);