如何在Java中使用XSLT 2.0和XSLT 3.0?

5
我可以在Java中使用XSLT 1.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>

但是,我现在想在Java中使用一些包含在XSLT 2.0和XSLT 3.0中的内容(如xsl:analyze-stringxsl:try等)。我该怎么做?


使用一个实现了该功能的Java库。在网上搜索以找到一个。 - Andreas
1个回答

10

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


我已经做到了。现在我能够使用XSLT 2,但是对于XSLT 3仍然出现错误,因为(XSLT 3中引入的)关键字未被识别。有其他方法在Java中使用XSLT 3吗? - user6276653
1
请提供您所做的确切细节,首先阅读Saxon文档,特别是http://saxonica.com/html/documentation/about/license/,以查看并检查您是否正确地使用PE或EE许可证。另请参阅http://saxonica.com/html/documentation/using-xsl/embedding/jaxp-transformation.html。如果仍然有问题,请在Saxon帮助论坛https://saxonica.plan.io/projects/saxon/boards上发布详细信息,说明您所做的事情以及收到的错误消息,或在此处使用标记为saxon的新问题。 - Martin Honnen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接