跳轉到內容

Apache Ant/執行 Saxon

來自華夏公益教科書,開放的書籍,開放的世界

您希望有一個執行 Saxon XSLT 轉換的 Apache Ant 任務。

下載 Saxon jar 檔案。將 saxon.jar 檔案放到 lib 資料夾中。執行以下測試。

原始碼

[編輯 | 編輯原始碼]

構建檔案

[編輯 | 編輯原始碼]

以下是 Apache Ant 如何呼叫 Saxon 的方法。

<target name="test-saxon">
   <xslt classpath="lib\saxon8.jar"
      in="in.xml" 
      out="out.html" 
      style="check-version.xsl">
      <factory name="net.sf.saxon.TransformerFactoryImpl"/>
   </xslt>
</target>

請注意,如果您在 Eclipse 中執行,則需要轉到“首選項”選單並將 saxon9.jar 檔案新增到 Ant/執行時/Ant 主目錄條目中。只需點選“新增 JAR”並將 saxon9jar 檔案新增到此列表的末尾。

XSLT 版本檢查

[編輯 | 編輯原始碼]

check-version.xsl

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:template match="/">
        <results>
            <Version><xsl:value-of select="system-property('xsl:version')" /></Version>
            <Vendor><xsl:value-of select="system-property('xsl:vendor')" /></Vendor>
            <Vendor-URL><xsl:value-of select="system-property('xsl:vendor-url')" /></Vendor-URL>
        </results>
    </xsl:template>
</xsl:stylesheet>

或者,如果您正在生成網頁

<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:template match="/">
      <html>
        <head>
         <title>XSL Version</title>
        </head>
        <body>
           <p>Version:
           <xsl:value-of select="system-property('xsl:version')" />
           <br />
           Vendor:
           <xsl:value-of select="system-property('xsl:vendor')" />
           <br />
           Vendor URL:
           <xsl:value-of select="system-property('xsl:vendor-url')" />
           </p>
        </body>
     </html>
  </xsl:template>
</xsl:stylesheet>

XALAN 的結果

[編輯 | 編輯原始碼]

Apache XALAN 的結果

  1.0
  Vendor: Apache Software Foundation (Xalan XSLT)
  Vendor URL: http://xml.apache.org/xalan-j

Saxon 的結果

[編輯 | 編輯原始碼]

版本:2.0 供應商:SAXON 9.1.0.7 來自 Saxonica 供應商網址:http://www.saxonica.com/

華夏公益教科書