跳轉到內容

Apache Ant/XMLvalidate

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

您想要一個命令列介面來驗證一個或多個 XML 檔案。

講師說明:此檔案用作 Apache Ant 課程的實驗室練習,該課程包括對 XML 的廣泛使用。

您可以使用 Apache Ant 檢查一個或一組檔案是否有效。這是透過使用 <xmlvalidate> Apache Ant 任務來完成的。xmlvalidate ant 任務將使用標準 ant <fileset> 並遍歷並檢查每個檔案。在下面的示例中,我們使用屬性指定了一個名為“in”的目錄。然後,我們使用 fileset 查詢該目錄和該目錄的所有子目錄中的所有 XML 檔案。每個檔案都根據 XML 架構成功檢查其有效性。

驗證資料夾中所有 XML 檔案的示例 Ant 任務

[編輯 | 編輯原始碼]
<project default="ValidateXML">

   <property name="MYROOTDIR" value="in"/>
   <target name="ValidateXML" description="Checks that all files at or below MYROOTDIR are well formed">
     <xmlvalidate>
        <fileset dir="${MYROOTDIR}" includes="**/*.xml"/>
        <attribute name="http://xml.org/sax/features/validation" value="true"/>
        <attribute name="http://apache.org/xml/features/validation/schema"  value="true"/>
        <attribute name="http://xml.org/sax/features/namespaces" value="true"/>
     </xmlvalidate>
   </target>
 
 </project>

在上面的示例中,我們假設每個 XML 檔案都有一個指令,告訴它從哪裡獲取其 XML 架構。

此目標將執行 Ant 附帶的預設 XML 解析器(通常是 Xerces)並報告任何格式不正確的檔案。

示例 XML 架構 MyMessages.xsd

[編輯 | 編輯原始碼]

為了測試這一點,您將需要一個小的 XML 架構檔案。以下檔案讀取了三個訊息的檔案

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
    <xs:element name="MyMessage" type="xs:string"/>
    <xs:element name="MyMessages">
    <xs:complexType>
        <xs:sequence>
           <xs:element ref="MyMessage" maxOccurs="3"/>
        </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

示例有效資料檔案

[編輯 | 編輯原始碼]

這是一個示例訊息檔案

<?xml version="1.0" encoding="UTF-8"?>
<MyMessages xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="MyMessages.xsd">
   <MyMessage>Hello World!</MyMessage>
   <MyMessage>ANT AND XML Schema ROCK</MyMessage>
</MyMessages>

請注意,根元素的 noNamespaceSchemaLocation 屬性告訴它在當前目錄中查詢 XML 架構檔案 (MyMessages.xsd)

示例無效資料檔案

[編輯 | 編輯原始碼]

如果新增第四條訊息,該檔案應根據上述 XML 架構中的規則驗證失敗。

<?xml version="1.0" encoding="UTF-8"?>
<MyMessages xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
xsi:noNamespaceSchemaLocation="MyMessages.xsd">
   <MyMessage>Hello From XSLT</MyMessage>
   <MyMessage>From input: Hi</MyMessage>
   <MyMessage>ANT AND XSLT ROCK</MyMessage>
   <MyMessage>I am the fourth message.</MyMessage>
</MyMessages>

要測試此示例,請新增一個名為“in”的資料夾,並將多個無效的 XML 檔案放入該資料夾。在本例中,我們建立了一個名為 MyInputBad.xml 的無效檔案。當我們在命令列中鍵入“build”時,以下是輸出結果

示例輸出

[編輯 | 編輯原始碼]
 ValidateXML:
 [xmlvalidate] C:\XMLClass\Ant\validate\in\MyInput.xml:6:15: cvc-complex type.2.4.d:    
 Invalid content was found starting with element 'MyMessage'. No child element is expected at this point.

這是一個示例輸出。請注意,錯誤訊息沒有說明您超出了 3 個數據元素的計數。

提供 XML 架構定義檔案

[編輯 | 編輯原始碼]

如果您在空名稱空間中工作,請新增以下屬性

  <attribute name="http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation" value="${xsd.file}"/>

如果您的文件有名稱空間,請使用以下方法

  <attribute name="http://xml.org/sax/features/namespaces" value="true"/>
  <attribute name="http://apache.org/xml/properties/schema/external-schemaLocation" value="${xsd.file}"/>

如果 XML 檔案沒有包含它們自己的架構,您還可以建立一個包含在哪裡找到 XML 架構的 ant 任務。這是使用特殊的 ant 屬性來完成的。

 <property
   name="http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation"
   value="${xsd.file}"/>
 
 <xmlvalidate file="xml/endpiece-noSchema.xml" lenient="false" failonerror="true" warn="true">
    <attribute name="http://apache.org/xml/features/validation/schema" value="true"/>
    <attribute name="http://xml.org/sax/features/namespaces" value="true"/>
 </xmlvalidate>

Schematron 驗證

[編輯 | 編輯原始碼]

Apache ant 還有一個元素可以根據 schematron 規則檔案進行驗證

<taskdef name="schematron"
classname="com.schematron.ant.SchematronTask"
classpath="lib/ant-schematron.jar"/>

<schematron schema="rules.sch" failonerror="false">
   <fileset includes="schmatron-input.xml"/>
</schematron>

有關更多詳細資訊,請參見 http://www.schematron.com/resource/Using_Schematron_for_Ant.pdf

上一章下一章

參考資料

[編輯 | 編輯原始碼]
華夏公益教科書