当前位置: 首页 > 知识库问答 >
问题:

如何在Java中使用XSLT2.0和XSLT3.0?

邬令
2023-03-14

我能够在Java中使用XSLT1.0,如以下示例所示:-

copy.xml

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="identityxfm.xsl"?>
<catalog>
    <book id="bk101">
        <author>Gambardella, Matthew</author>
        <title>XML Developer's Guide</title>
        <genre>Computer</genre>
        <price>44.95</price>
        <publish_date>2000-10-01</publish_date>        
        <description>An in-depth look at creating applications with
 XML.</description>
    </book>
    <book id="bk102">
        <author>Ralls, Kim</author>
        <title>Midnight Rain</title>
        <genre>Fantasy</genre>
        <price>5.95</price>
        <publish_date>2000-12-16</publish_date>
        <description>A former architect battles corporate zombies,
 an evil sorceress, and her own childhood to become queen of the
 world.</description>
    </book>
    <book id="bk103">
        <author>Corets, Eva</author>
        <title>Maeve Ascendant</title>
        <genre>Fantasy</genre>
        <price>5.95</price>
        <publish_date>2000-11-17</publish_date>
        <description>After the collapse of a nanotechnology society
 in England, the young survivors lay the foundation for a new 
society.</description>
    </book>
</catalog>

copy.xsl

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >

  <xsl:template match="/ | @* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

copy.java

package com.data.transform;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public class Copy {

    public static void main(String args[]) throws Exception {
        StreamSource source = new StreamSource("copy.xml");
        StreamSource stylesource = new StreamSource("copy.xsl");

        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer(stylesource);

        StreamResult result = new StreamResult(System.out);
        transformer.transform(source, result);
      }
}

输出

<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="identityxfm.xsl"?><catalog>
    <book id="bk101">
        <author>Gambardella, Matthew</author>
        <title>XML Developer's Guide</title>
        <genre>Computer</genre>
        <price>44.95</price>
        <publish_date>2000-10-01</publish_date>        
        <description>An in-depth look at creating applications with
 XML.</description>
    </book>
    <book id="bk102">
        <author>Ralls, Kim</author>
        <title>Midnight Rain</title>
        <genre>Fantasy</genre>
        <price>5.95</price>
        <publish_date>2000-12-16</publish_date>
        <description>A former architect battles corporate zombies,
 an evil sorceress, and her own childhood to become queen of the
 world.</description>
    </book>
    <book id="bk103">
        <author>Corets, Eva</author>
        <title>Maeve Ascendant</title>
        <genre>Fantasy</genre>
        <price>5.95</price>
        <publish_date>2000-11-17</publish_date>
        <description>After the collapse of a nanotechnology society
 in England, the young survivors lay the foundation for a new 
society.</description>
    </book>
</catalog>

共有1个答案

孙成益
2023-03-14

从Maven或Sourceforge获取Saxon 9 HE并将其放在类路径上,那么您就可以在9.8之前获得对Saxon 9.x的XSLT 2.0支持,或者在9.8中获得对XSLT 3.0的支持(流、高阶函数、xsl:evaluate、模式感知、向后兼容除外)。要获得完整的XSLT3.0支持,您需要从Saxonica下载Saxon9PE或EE,并将其与购买的许可证或在类路径上请求的试用许可证放在一起。

 类似资料:
  • 我必须使用 XSLT 2.0 处理器来实现字符串操作函数,如 我在 POM 文件中添加了撒克逊的依赖项并运行了“mvn install”命令。通过执行此操作,“saxon-9.1.0.8.jar”被添加到“引用的库”文件夹下。 在代码中,我使用了 当我尝试调用下面一行< code > transformer factory . new instance(" net . SF . Saxon . t

  • 问题内容: 我曾经使用过JUnit和Mocks,但我想知道,JUnit中的Mocks和Stub之间有什么区别,以及如何在JUnit,Java中使用Stub?作为具有EasyMock,Mockito等的Mocks,Stubs在Java中使用什么? 请提供Java中的存根示例代码。 问题答案: 要在junit中使用存根,您不需要任何框架。 如果您想存根某些接口,只需实现它: 然后创建一个新的存根对象并

  • 问题内容: 我试图了解我正在制作的一个小程序的PrintWriter,而且我似乎无法让Java来制作文件然后在上面写。当我执行下面的程序时,它在第9行显示Filenotfoundexeption错误。它也无法在我指定的目录中创建文件。我对此并不陌生,因此请尝试使答案保持简单。我正在使用Eclipse。 问题答案: 如果该目录不存在,则需要创建它。Java不会自己创建它,因为该类只是到一个实体的链接

  • 问题内容: 使a饱和的最简单方法是什么? 问题答案: 用途: 更新: 确实有一种更简单的方法。您可以使用该类。该类的优点是它提供了一个静态实用程序方法(即),该方法将返回图像的变灰版本。 话虽如此,我认为使实例饱和的最简单方法是:

  • 问题内容: 我有以下类别描述代码段: 该代码在Hibernate 4上不起作用,因为不支持 我从 Joda-Time 看到了有关如何使用 LocalDateTime 的建议,但我使用的是Java 8。 __ 问题答案: 由于Hibernate 4不支持它,因此您需要实现一个如本示例所示的用户类型。 然后,可以在带有@Type批注的映射中使用新的用户类型。例如 @Type批注需要一个实现userTy

  • 问题内容: 我想对英语句子加标签,并进行一些处理。我想使用openNLP。我已经安装了 当我执行命令时 它提供输出POSTagging Text.txt中的输入 我希望它安装正确吗? 现在如何从Java应用程序内部进行此POStagging?我已将openNLPtools,jwnl,maxent jar添加到项目中,但是如何调用POStagging? 问题答案: 这是我放在一起的一些(旧)示例代码