【问题标题】:Embedded validation within XSD schemaXSD 架构中的嵌入式验证
【发布时间】:2014-02-06 21:19:54
【问题描述】:

我想在我的 XSD 架构定义中嵌入规则,以了解如何使用元素的可选属性。考虑以下元素定义:

<xs:complexType name="sampleElement">
  <xs:attribute name="name" type="xs:string" use="required"/>
  <xs:attribute name="description" type="xs:string" use="optional"/>
  <xs:attribute name="optPrimary" type="xs:string" use="optional"/>
  <xs:attribute name="optSecondary" type="xs:string" use="optional"/>
</xs:complexType

在这个例子中,optPrimary 和 optSecondary 都是可选的。属性 optPrimary 可以单独使用,但 optSecondary 必须与 optPrimary 结合使用。因此,我想要一个嵌入在模式中的规则,可以在验证 XML 时强制执行。

我在一个单独的文件中找到了用于此目的的 Schematron 示例,但我还没有找到如何将它作为模式的一部分嵌入。

【问题讨论】:

  • XML 模式不支持在元素之间创建约束的能力。您已正确识别出解决此差距的 schematron。

标签: xml xsd xsd-validation


【解决方案1】:

XSD 1.1 支持这一点。这是一种表达方式:

<?xml version="1.0" encoding="utf-8" ?>
<!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) -->
<xsd:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" xmlns="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xerces="http://xerces.apache.org">
    <xsd:element name="sampleElement">
        <xsd:complexType>
            <xsd:attribute name="name" type="xsd:string" use="required"/>
            <xsd:attribute name="description" type="xsd:string" use="optional"/>
            <xsd:attribute name="optPrimary" type="xsd:string" use="optional"/>
            <xsd:attribute name="optSecondary" type="xsd:string" use="optional"/>
            <xsd:assert test="@optPrimary or not(@optSecondary)" xerces:message="Secondary needs primary..."/>
        </xsd:complexType>          
    </xsd:element>
</xsd:schema> 

【讨论】:

    猜你喜欢
    • 2012-05-29
    • 2010-11-15
    • 1970-01-01
    • 2011-06-18
    • 2015-04-20
    • 2011-05-30
    • 1970-01-01
    • 2013-06-14
    • 1970-01-01
    相关资源
    最近更新 更多