简体   繁体   English

在 Saxon 10.6 HE 中使用 XSLT 转换 XML 时出现 NotSerializableException

[英]NotSerializableException while transforming XML using XSLT in Saxon 10.6 HE

I am trying to transform XML using XSLT in Saxon 10.6 HE.我正在尝试在 Saxon 10.6 HE 中使用 XSLT 转换 XML。 Getting error as below.得到如下错误。

Error: java.io.WriteAbortedException: writing aborted;错误:java.io.WriteAbortedException:写入中止; java.io.NotSerializableException: net.sf.saxon.om.StructuredQName java.io.NotSerializableException:net.sf.saxon.om.StructuredQName

Input XML:输入 XML:

<create>
   <article>
      <identifier>Test</identifier>
   </article>
      <article>
      <identifier>Test123</identifier>
   </article>
</create>
<update>
   <article>
      <identifier>Test</identifier>
   </article>
</update>

XSLT: 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: 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();

Kindly help to resolve this issue.请帮助解决此问题。 Is there any way to implement this without using Serializer.有什么方法可以在不使用 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我看不出如何解析缺少单个根元素的输入,如果你想这样做,你需要 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM