2016-12-24 55 views
2

我能够在Java中使用XSLT 1.0如图所示folllowing例如: -如何在Java中使用XSLT 2.0和XSLT 3.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等)。我怎样才能做到这一点 ?

+0

使用实现该功能的Java库。搜索网络找到一个。 – Andreas

回答

5

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

+0

我做到了。现在我可以使用XSLT 2了,但是我仍然遇到了XSLT 3的错误,因为关键字(在XSLT 3中引入的)无法识别。任何其他方式在Java中使用XSLT 3? – user6276653

+0

请详细说明您做了什么,首先阅读Saxon的文档,特别是http://saxonica.com/html/documentation/about/license/,查看并检查您是否正确运行PE或EE执照。另见http://saxonica.com/html/documentation/using-xsl/embedding/jaxp-transformation.html。如果您仍然遇到问题,请详细了解您所做的事情以及您收到哪些错误消息,最好在撒克逊帮助论坛https://saxonica.plan.io/projects/saxon/boards或者带有标记为撒克逊的新问题。 –