在 JAVA 中使用 XSLT 转换 XML

Transform XML using XSLT in JAVA

我在 Java 中有一个使用 XSLT 3.0 使用 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 main class 我收到这个错误:

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);

  }

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

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

Saxonica 还有 JAXP samples for 9.8 online here