2016-12-24 59 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或者帶有標記爲撒克遜的新問題。 –