如何在 Java 中使用 XSLT 2.0 和 XSLT 3.0? [英] How can I use XSLT 2.0 and XSLT 3.0 in Java?

查看:23
本文介绍了如何在 Java 中使用 XSLT 2.0 和 XSLT 3.0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在 Java 中使用 XSLT 1.0,如下例所示:-

I am able to use XSLT 1.0 in Java as shown in the folllowing example :-

复制.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>

复制.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>

复制.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>

但是,现在我想使用 XSLT 2.0 和 XSLT 3.0 中包含的一些东西(例如 xsl:analyze-stringxsl:try 等)爪哇.我该怎么做?

But, now I want to use few things that are included in XSLT 2.0 and XSLT 3.0 (like xsl:analyze-string, xsl:try etc.) in Java. How can I do that ?

推荐答案

从 Maven 或 获取 Saxon 9 HESourceforge 并把它放在类路径上,然后你有 XSLT 2.0 支持和 Saxon 9.x 之前的 9.8 或 XSLT 3.0 支持(除了流,高阶函数,xsl:evaluate,schema-意识,向后兼容)与 9.8.要获得完整的 XSLT 3.0 支持,您需要从 Saxonica 下载 Saxon 9 PE 或 EE 并将其与您购买的许可证或您在类路径上请求的试用许可证.

Get Saxon 9 HE from Maven or Sourceforge and put it on the classpath, then you have XSLT 2.0 support with Saxon 9.x before 9.8 or XSLT 3.0 support (except streaming, higher order functions, xsl:evaluate, schema-awareness, backwards compatibility) with 9.8. For full XSLT 3.0 support you need to download Saxon 9 PE or EE from Saxonica and put it together with a license you buy or a trial licence you request on the classpath.

这篇关于如何在 Java 中使用 XSLT 2.0 和 XSLT 3.0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆