【问题标题】:Transform XML using XSLT in JAVA在 JAVA 中使用 XSLT 转换 XML
【发布时间】:2019-01-30 18:48:24
【问题描述】:

我有一个使用 XSLT 3.0 在 Java 中使用 XSLT 3.0 (xsltfilePersonnes.xsl) 合并​​ 2 个 xml 文件(personne1,personne2)的示例。

我正在使用 Saxon 9.8(XSLT 3.0 版本)。到目前为止尝试过:

在命令行中我运行这个命令行:

C:\test2>java -jar saxon-he-9.8.0-1.jar personne1.xml xsltFilePersonnes.xsl -o:resultFile.xml

它运行没有错误,fileResult 和预期的一样,它合并了 resultfile.xml 中的两个文件(personne1 和 personne2),但是当我运行 java 主类时,我得到了这个错误:

Recoverable error on line 1 column 39 of file:C:\test2\xsltFilePersonnes.xsl:
  SXXP0003: Error reported by XML parser:
  file:C:\test2\xsltFilePersonnes.xsl<Line 1, Column 39>
: XML-20180: (Error) User Supplied NodeFactory returned a Null Pointer.: Invalid URI for stylesheet: file:C:test2\xsltFilePersonnes.xsl

感谢任何帮助我正在学习 Xslt,谢谢你们。

personne1.xml:

<personnes>
  <personne>
    <name>aaa</name>
    <age>10</age>
    <adress>aaaaaa</adress>
  </personne>

  <personne>
    <name>bbb</name>
    <age>10</age>
    <adress>aaaaaa</adress>
  </personne>

  <personne>
    <name>ccc</name>
    <age>20</age>
    <adress>cccccc</adress>
  </personne>

  <personne>
    <name>ddd</name>
    <age>10</age>
    <adress>cccccc</adress>
  </personne>


</personnes>

personne2.xml:

<?xml version="1.0" encoding="UTF-8"?>
<personnes>
  <personne>

    <id>1111</id>
    <quantity>1100</quantity>
  </personne>

  <personne>

     <id>2222</id>
     <quantity>2200</quantity>
  </personne>

  <personne>

    <id>3333</id>
    <quantity>3300</quantity>
  </personne>

  <personne>

    <id>4444</id>
    <quantity>4400</quantity>
  </personne>

  <personne>

    <id>5555</id>
    <quantity>5500</quantity>
  </personne>
</personnes>

xsltfilePersonnes.xsl:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="#all"
    version="3.0">

     <xsl:variable name="personne2"
        select="document('file:test2\personne2.xml')" />
    <xsl:accumulator name="pos" as="xs:integer" initial-value="0">
        <xsl:accumulator-rule match="personnes" select="0"/>
        <xsl:accumulator-rule match="personnes/personne" select="$value + 1"/>
    </xsl:accumulator>

    <xsl:mode use-accumulators="pos"/>    

    <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/*">
    <xsl:copy>
        <xsl:merge>
            <xsl:merge-source select="personne">
                <xsl:merge-key select="accumulator-before('pos')"/>
            </xsl:merge-source>
            <xsl:merge-source for-each-item="$personne2" select="personnes/personne">
                <xsl:merge-key select="accumulator-before('pos')"/>
            </xsl:merge-source>
            <xsl:merge-action>
                <xsl:copy>
                    <xsl:copy-of
                      select="current-merge-group()[2]/id, name, current-merge-group()[2]/quantity, age, adress"/>
                </xsl:copy>
            </xsl:merge-action>
        </xsl:merge>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

主java:

public static void main (String [] args) throws ParserConfigurationException, SAXException, IOException, TransformerException {

  Source input  = new StreamSource("file:\test2\personne1.xml");
  Source xsl    = new StreamSource("file:\test2\filexsltFilePersonnes.xsl");
  Result output = new StreamResult(new File("file:\test2\resultFile.xml")); 
    TransformerFactory factory =new  net.sf.saxon.TransformerFactoryImpl();
        //TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer(xsl);
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.transform(input, output);

  }

【问题讨论】:

    标签: java xml xslt saxon


    【解决方案1】:

    如果您想使用StreamSource(String systemId),则有效的本地绝对文件 URI 应类似于:new StreamSource("file:///C:/test2/xsltFilePersonnes.xsl")

    但我认为在某个文件夹(例如 C:\test2)中运行您的 Java 代码,然后使用像 new StreamSource("xsltFilePersonnes.xsl") 这样的相对 URI 应该也可以。

    Saxonica 也有 JAXP samples for 9.8 online here

    【讨论】:

    • StreamSource javadoc 说明参数“必须是符合 URI 语法的字符串”。这在技术上不允许相对引用(当然也没有说明应该如何解释它),但是 JAXP 规范一直对这种细节很模糊。
    猜你喜欢
    • 2012-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多