【问题标题】:Validating XSD Schema with Schematron and xsltproc使用 Schematron 和 xsltproc 验证 XSD 架构
【发布时间】:2012-05-10 05:13:47
【问题描述】:

我在验证与 Schematron 结合的 SXD 架构时遇到了困难。

按照guide 中描述的步骤,我在 XSD 文档中的 <xs:appinfo> 标记之间合并了 schematron,如下所示:

<?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"/>

使用 schematron 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

非常感谢您帮助找出导致此错误的原因。

【问题讨论】:

    标签: xml validation xsd schematron


    【解决方案1】:

    您的架构是正确的,并且执行了它应该做的事情......

    问题出在脚本上:这个脚本需要接收一个 Schematron 模式,而你给它一个带有嵌入规则的 XML 模式,这是一种不同的野兽。

    要进行验证,您需要运行第一个转换,该转换将从 XML Schema 中提取 Schematron 并对此结果运行验证。

    您还可以使用 xmllint (libxml) 根据 XML Schema 验证文档,这是一种不同的操作。

    为此,您可以将下载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
    

    或者,您可以使用原生支持架构中嵌入的 Schematron 规则的实现,或者使用 oXygen 等工具。

    【讨论】:

    • 谢谢。有用。但是,鉴于 XSD2 刚刚标准化,您是否仍会提倡使用 Schematron?
    • 您的意思是 XML Schema 1.1 ;) ? Schematron 仍然比 XML SCHema 1.1 断言灵活得多,后者对您可以使用的 XPath 表达式施加了限制,并且 Schematron 保留了其独特的功能,让您可以定义错误消息。我的建议是在满足您的需求时使用 XSD 1.1,但请记住,如果您需要更多,您仍然可以使用 Schematron。
    猜你喜欢
    • 2011-03-10
    • 2011-06-18
    • 1970-01-01
    • 2012-05-29
    • 2010-11-15
    • 1970-01-01
    • 1970-01-01
    • 2016-10-16
    • 2012-09-16
    相关资源
    最近更新 更多