【问题标题】:NotSerializableException while transforming XML using XSLT in Saxon 10.6 HE在 Saxon 10.6 HE 中使用 XSLT 转换 XML 时出现 NotSerializableException
【发布时间】:2022-01-09 08:57:37
【问题描述】:

我正在尝试在 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 的情况下实现这一点。

【问题讨论】:

  • 没有单个根元素的输入是否完全解析?
  • 嗨@MartinHonnen 添加根元素后,它工作正常。有没有其他方法可以在不使用 Serializer 的情况下在 java 代码中进行转换?
  • 有多种重载(applyTemplates 方法)可以返回 XdmValue。但我有点怀疑这个问题与使用序列化程序有关,解析没有单根的输入是行不通的,除非你使用函数或方法来处理它,比如 XPath 3.1 函数parse-xml-fragment.
  • 这是一个非常奇怪的错误,我无法重现它。我得到了我所期望的:错误消息“第 9 行第 2 列 SXXP0003 错误由 XML 解析器报告:根元素之后的文档中的标记必须格式正确。”
  • 我要补充一点,Saxon 本身从不抛出 NotSerializableException,而且这个异常与 XML 序列化无关(它与 Java 对象的序列化有关,这是完全不同的东西)。异常的堆栈跟踪会有所帮助。

标签: java xml xslt saxon saxon-js


【解决方案1】:

我不知道如何解析缺少单个根元素的输入,如果你想这样做,你需要 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);

【讨论】:

  • 谢谢@MartinHonnen。这个效果很好。
  • 嗨@MartinHonnen,我有带有包含空格并以数字开头的标签名称的XML。有没有办法解析它?示例 XML:FIdentifier> 212759999108:> 4f8a-a92d-cba1b4e07cf0121:> CRDDED4!c> 23B:>>
  • @Nijith,XML 元素名称既不能以数字开头,也不能包含空格,因此标准的 XML 处理方式肯定是不可能的。一些 XSLT 处理链允许您提供其他输入,例如 HTML 或 SGML,但我不知道任何允许元素名称中包含空格的格式,所以我不知道这种格式的现有解析器。
  • 感谢@MartinHonnen 提供的信息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-11
  • 1970-01-01
相关资源
最近更新 更多