java - 在 JAVA 中使用 XSLT 转换 XML

标签 java xml xslt saxon

我有一个使用 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);

  }

最佳答案

如果你想使用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 .

关于java - 在 JAVA 中使用 XSLT 转换 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54447528/

相关文章:

java - 如果字符串重复,如何返回字符串的第一个字符?

sql - 如何分组连接多列?

xml - XSLT 中的包装 DIV

XSLT 2.0,检查特定位置是否存在特定节点

xml - XSLT 嵌套选择

java - cipher 对象的 doFinal 方法返回 java.lang.ArrayIndexOutOfBoundsException

Java map 内容对比

java - 使用 Maven 的命名约定在 Gradle 中进行集成测试?

xml - 是否可以在 XSD 中重命名基本类型

java - Eclipse Android 开发 - [I18N] 硬编码字符串 "TextView",应该使用 @string 资源