简体   繁体   English

在 JAVA 中使用 XSLT 转换 XML

[英]Transform XML using XSLT in JAVA

I have an example in Java using XSLT 3.0 to merge 2 xml files (personne1 , personne2) using XSLT 3.0 (xsltfilePersonnes.xsl).我在 Java 中有一个示例,使用 XSLT 3.0 合并使用 XSLT 3.0 (xsltfilePersonnes.xsl) 的 2 个 xml 文件(personne1、personne2)。

I m using Saxon 9.8 (version for XSLT 3.0).我正在使用 Saxon 9.8(XSLT 3.0 版本)。 Tried so far :到目前为止尝试过:

in command line I run this command line :在命令行中,我运行此命令行:

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

it run without error and the fileResult was as expected it merge the two files(personne1 and personne2) in the resultfile.xml, but when I run java main class I got this error :它运行没有错误,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

any help is appreciated I m learning Xslt , thank you guys.感谢任何帮助我正在学习 Xslt,谢谢你们。

personne1.xml : 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: 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: 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>

main java :主要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);

  }

If you want to use StreamSource(String systemId) then a valid local absolute file URI would look something like: new StreamSource("file:///C:/test2/xsltFilePersonnes.xsl") .如果您想使用StreamSource(String systemId)那么有效的本地绝对文件 URI 将类似于: new StreamSource("file:///C:/test2/xsltFilePersonnes.xsl")

But I think running your Java code in a certain folder (eg C:\\test2 ) and then using relative URIs like new StreamSource("xsltFilePersonnes.xsl") should work as well.但我认为在某个文件夹(例如C:\\test2 )中运行您的 Java 代码,然后使用像new StreamSource("xsltFilePersonnes.xsl")这样的相对 URI 应该也能正常工作。

Saxonica also has JAXP samples for 9.8 online here . Saxonica 还在线提供了 9.8 的 JAXP 示例

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

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