【问题标题】:XSD Restriction on AttributeXSD 对属性的限制
【发布时间】:2012-12-25 12:37:47
【问题描述】:

我想我已经对此进行了很多搜索,但仍然没有。

将不胜感激。

我正在尝试限制内容为空的元素的属性。 “颜色”应该有一个限制,只能容纳 3 位数字或 minLength=3 和 maxLength=3。它不应该有任何内容。

<?xml version="1.0" encoding="utf-8"?>
  <items xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:noNamespaceSchemaLocation="">
  <product id="" name="">
    <article id="1001">
      <umbrella color="100"/>
      <umbrella color="101"/>
    </article>
    <article id="1002">
      <umbrella color="110"/>
    </article>
  </product>
</items>

编辑:我知道如何对 simpleType 进行 XSD 限制。但我不知道如何将它与 ComplexType 组合成一个实体。

如果您能提供更详细(或完整)的解决方案,我会很高兴。

顺便说一句,“颜色”不限于 xs:integer。它实际上是一个 xs:string。

【问题讨论】:

    标签: attributes xsd restriction


    【解决方案1】:

    您可以定义类似于以下的属性。此示例使用模式来限制值,但如果更合适,您也可以使用 min 和 max。

    <xs:attribute name="color">
        <xs:simpleType>
            <xs:restriction base="xs:integer">
                <xs:pattern value="[0-9][0-9][0-9]"/>
            </xs:restriction>
        </xs:simpleType>
    </xs:attribute>
    

    然后在您的元素定义中,您只需使用ref 来引用定义的属性:

    <xs:attribute ref="color"/>
    

    更新(响应 OP 的评论):

    整个架构可能如下所示:

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    
        <xs:attribute name="color">
            <xs:simpleType>
                <xs:restriction base="xs:integer">
                    <xs:pattern value="[0-9][0-9][0-9]"/>
                </xs:restriction>
            </xs:simpleType>
        </xs:attribute>
    
        <xs:attribute name="id">
            <xs:simpleType>
                <xs:restriction base="xs:integer">
                    <xs:pattern value="[0-9][0-9][0-9][0-9]"/>
                </xs:restriction>
            </xs:simpleType>
        </xs:attribute>
    
        <xs:attribute name="name" type="xs:string"/>
    
        <xs:complexType name="article_type">
            <xs:attribute ref="color" use="required"/>
        </xs:complexType>
    
        <xs:element name="article">
            <xs:complexType>
                <xs:choice maxOccurs="unbounded" minOccurs="0">
                    <xs:element name="umbrella" type="article_type"/>
                </xs:choice>
                <xs:attribute ref="id" use="required"/>
            </xs:complexType>
        </xs:element>
    
        <xs:element name="product">
            <xs:complexType>
                <xs:choice maxOccurs="unbounded" minOccurs="0">
                    <xs:element ref="article"/>
                </xs:choice>
                <xs:attribute ref="id" use="required"/>
                <xs:attribute ref="name"/>
            </xs:complexType>
        </xs:element>
    
        <xs:element name="items">
            <xs:complexType>
                <xs:choice maxOccurs="unbounded" minOccurs="0">
                    <xs:element ref="product"/>
                </xs:choice>
            </xs:complexType>
        </xs:element>
    
    </xs:schema>
    

    【讨论】:

    • 短一点:[0-9]{3}
    • 感谢您的意见。看我上面的编辑。我知道如何做一个 xs:restriction 但我不知道如何将它组合成一个整体。请为我的示例提供更多或完整的信息。如果我理解正确,具有 SimpleType 属性的 ComplexType 和限制。
    • 似乎可以解决问题。谢谢你的帮助。我希望我能够根据您的意见整理出我自己的意见。干杯。
    【解决方案2】:

    以下应该可以工作

     <element name="umbrella" nillable="true" type="umbrellaType">
    

    <complexType name="umbrellaType">
       <attribute name="color">
         <simpleType>
           <restriction base="int">
            <minExclusive value="99"></minExclusive>
            <maxInclusive value="999"></maxInclusive>
           </restriction>
         </simpleType>
       </attribute>
    </complexType>
    

    【讨论】:

    • 感谢您的意见。看我上面的编辑。我知道如何做一个 xs:restriction 但我不知道如何将它组合成一个整体。请为我的示例提供更多或完整的信息。如果我理解正确,具有 SimpleType 属性的 ComplexType 和限制。
    猜你喜欢
    • 1970-01-01
    • 2018-08-20
    • 2016-10-10
    • 2012-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多