在 Saxon 10.6 HE 中使用 XSLT 转换 XML 时出现 NotSerializableException
NotSerializableException while transforming XML using XSLT in Saxon 10.6 HE
我正在尝试在 Saxon 10.6 HE 中使用 XSLT 转换 XML。出现如下错误。
错误:java.io.WriteAbortedException:写入中止; java.io.NotSerializableException: net.sf.saxon.om.StructuredQName
输入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代码:
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();
请帮助解决此问题。有没有什么方法可以不使用 Serializer 来实现。
我不明白你如何解析缺少单个根元素的输入,如果你想这样做,你需要 XPath 3.1 parse-xml-fragment
函数的帮助
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);
我正在尝试在 Saxon 10.6 HE 中使用 XSLT 转换 XML。出现如下错误。
错误:java.io.WriteAbortedException:写入中止; java.io.NotSerializableException: net.sf.saxon.om.StructuredQName
输入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代码:
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();
请帮助解决此问题。有没有什么方法可以不使用 Serializer 来实现。
我不明白你如何解析缺少单个根元素的输入,如果你想这样做,你需要 XPath 3.1 parse-xml-fragment
函数的帮助
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);