【问题标题】:XSD:Controlling the restriction on length of xml element based on that element's attribute value using XSDXSD:使用 XSD 根据元素的属性值控制 xml 元素的长度限制
【发布时间】:2016-06-22 09:02:26
【问题描述】:

以下是示例 XML:

 <PatientName>
     <FamilyName updatable="2">Bob</FamilyName>
  </PatientName>

目前使用XSD是这样的:

<xs:simpleType name="reviewFamilyname">  
    <xs:restriction base="xs:string">   
        <xs:maxLength value="20"/>  
    </xs:restriction>  
</xs:simpleType>

<xs:element name="FamilyName" nillable="true">
    <xs:complexType>
        <xs:simpleContent>
            <xs:extension base="reviewFamilyname">
                <xs:attribute type="xs:string" name="updatable"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>
</xs:element>

目前我对姓氏进行了限制,其最大长度应为 20 个字符。 我正在使用 XSD 1.0,我想根据 FamilyName 元素的属性值对 FamilyName 元素应用限制。我想在什么时候禁用限制,

FamilyName 元素中的属性(可更新)值等于 2。

且attribute(updatable) 值等于1,FamilyName 元素值为null。

在所有其他情况下,应启用限制。我应该如何更改 XSD 以完全满足这些要求,这可能吗?如果可以,如何?

谢谢。

【问题讨论】:

    标签: xml xsd


    【解决方案1】:

    在 XSD 1.0 中,元素内容的类型不能依赖于其属性的值。

    在 XSD 1.1 中,您可以使用称为条件类型分配的功能实现此目的。

    你会想要这样的:

    <xs:element name="FamilyName" type="xs:anyType">
      <xs:alternative test="@updateable = 1" type="...."/>
      <xs:alternative test="@updateable = 2" type="...."/>
      <xs:alternative type="...."/>
    </xs:element>
    

    【讨论】:

    • 非常感谢。请您提供 XSD 的确切代码。这真的很有帮助。
    • 我应该如何更改我的 XSD 架构版本?
    • 已知的 XSD 1.1 处理器共有三种:Saxon、Xerces 和 Altova。安装其中一个。
    猜你喜欢
    • 2018-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多