2012-10-31 43 views
3

我正在通過Ant的XSLT任務運行一些XSL轉換。我使用Saxon 9HE作爲處理引擎。我有一種情況,輸入XML文件都使用相同的DTD,但聲明它在不同的地方。一些聲明它在當前目錄中,一些在一個文件夾中,而另一些則引用一個URL。這裏是Ant腳本:在XSLT Ant任務中使用Saxon時,有沒有辦法禁用驗證?

<?xml version="1.0" encoding="UTF-8"?> 

<project name="PubXML2EHeader" default="transform"> 
    <property name="data.dir.input" value="./InputXML"/> 
    <property name="data.dir.output" value="./converted-xml"/> 
    <property name="xslt.processor.location" value="D:\\saxon9he.jar"/> 
    <property name="xslt.processor.factory" value="net.sf.saxon.TransformerFactoryImpl"/> 

    <path id="saxon9.classpath" location="${xslt.processor.location}"/> 

    <target name="clean"> 
     <delete dir="${data.dir.output}" includes="*.xml" failonerror="no"/> 
    </target> 

    <target name="transform" depends="clean"> 
     <xslt destdir="${data.dir.output}" 
       extension=".xml" 
       failOnTransformationError="false" 
       processor="trax" 
       style="Transform.xsl" 
       useImplicitFileset="false" 
       classpathref="saxon9.classpath" 
     > 
      <outputproperty name="method" value="xml"/> 
      <outputproperty name="indent" value="yes"/> 
      <fileset dir="${data.dir.input}" includes="**/*.xml" excludes="Transform.xml"/> 
      <factory name="${xslt.processor.factory}"/> 
     </xslt> 
    </target> 

</project> 

當我運行這個Ant腳本我得到的錯誤是這樣的:

[XSLT]:致命錯誤!通過XML解析器處理報告I/O錯誤 文件:/ d:/annurev.biophys.093008.131228.xml: http://www.atypon.com/DTD/nlm-dtd/archivearticle.dtd原因: java.io.FileNotFoundException: http://www.atypon.com/DTD/nlm-dtd/archivearticle.dtd

我覺得這些都是由引起事實上Saxon無法訪問DTD(在這種情況下,這實際上是防火牆問題)。我不認爲我關心驗證輸入,這是我認爲在這裏發生的,我想跳過它。是否有可以添加到XSLT Ant任務的屬性來阻止Saxon嘗試讀取DTD?

回答

6

您在使用驗證時「讀取DTD」令人困惑。 XSLT處理器總是會要求解析器讀取文檔的外部DTD,無論它是否正在驗證。這是因爲DTD不僅用於驗證;它也用於擴展實體引用。

處理此問題的常用方法是將DTD引用重定向到某個可以訪問的副本,通常通過使用目錄。這涉及在底層XML解析器上設置EntityResolver。

有很多有關如何通過命令行建立一個目錄解析與撒克遜人,平時在網絡上:例如參見這裏:http://www.sagehill.net/docbookxsl/UseCatalog.html

的建議通常是設置-x,-y ,和-r選項,但實際上只有-x是相關的,如果您只需要在源文檔中重定向DTD引用(-y影響樣式表,-r影響document()函數)。在Ant中,與設置-x選項等效的是使用factory元素的屬性child來設置配置屬性<attribute name="http://saxon.sf.net/feature/sourceParserClass" value="org.apache.xml.resolver.tools.ResolvingXMLReader"/>

這仍然留下我覺得棘手的部分,這實際上是創建您的目錄文件。

相關問題