2012-04-30 89 views
2

我很難驗證與Schematron結合使用的SXD Schema。使用Schematron和xsltproc驗證XSD Schema

繼我XSD文檔中併入<xs:appinfo>標籤之間的Schematron如下本guide描述的步驟:

<?xml version="1.0" encoding="UTF-8"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 

    <xs:element name="Test"> 

     <xs:annotation> 
      <xs:appinfo> 
       <sch:pattern name="Testing schematron" xmlns:sch="http://purl.oclc.org/dsdl/schematron"> 
        <sch:rule context="Test"> 
         <sch:assert test="@Attribute1">Attribute 1 exists</sch:assert> 
        </sch:rule> 
       </sch:pattern> 
      </xs:appinfo> 
     </xs:annotation> 

     <xs:complexType> 
      <xs:attribute name="Attribute1" type="xs:string" use="optional"/> 
      <xs:attribute name="Attribute2" type="xs:string" use="optional"/> 
     </xs:complexType> 
    </xs:element> 

</xs:schema> 

本文檔應該測試(或驗證)該文件

<?xml version="1.0" encoding="ISO-8859-1"?> 
<Test Attribute1="attr1"/> 

使用schetrontron page上列出的簡單的基於xsltproc的腳本。不幸的是,我在腳本的最後一步收到以下錯誤消息。

step3.xsl:13: parser error : Extra content at the end of the document 
plates select="*|comment()|processing-instruction()" mode="M0"/></axsl:template> 
                      ^
cannot parse step3.xsl 

我很感激幫助找出這個錯誤的原因。

回答

3

你的模式是正確的,確實它是什麼意思做...

的問題是用腳本:這個腳本預計將收到一個Schematron模式,你給它一個XML架構嵌入規則,這是一種不同類型的野獸。

要做驗證,您需要運行第一個轉換,它將從XML Schema中提取Schematron並對此結果運行驗證。

而且您還可以使用xmllint(libxml)根據不同操作的XML模式驗證文檔。

要做到,你可以下載ExtractSchFromXSD.xsl腳本更改爲:

#!/bin/bash 

echo XSD validation 
xmllint -schema $1 $2 

echo Step0 ... 
xsltproc ExtractSchFromXSD.xsl $1 > schema.sch 

echo Step1 ... 
xsltproc iso_dsdl_include.xsl schema.sch > step1.xsl 

echo Step2 ... 
xsltproc iso_abstract_expand.xsl step1.xsl > step2.xsl 

echo Step3 ... 
xsltproc iso_svrl_for_xslt1.xsl step2.xsl > step3.xsl 

echo Validation ... 
xsltproc step3.xsl $2 | tee result.svrl 

或者,您可以使用本機支持的模式或工具,如oXygen嵌入的Schematron規則的實現。

+0

謝謝。有用。但是鑑於XSD2已經標準化了,你還會主張使用Schematron嗎? – Olumide

+1

你的意思是XML Schema 1.1;)? Schematron比XML SChema 1.1斷言更靈活,這些斷言對您可以使用的XPath表達式施加限制,而Schematron保留其獨特功能以允許您定義錯​​誤消息。然後,我的建議是在滿足您的需求時使用XSD 1.1,但請記住,如果您需要更多,仍然可以使用Schematron。 –