2013-01-11 55 views
15

我想我已经搜索了很多关于这个,但仍然没有去。XSD属性限制

将不胜感激任何帮助。

我想限制空元素的属性。 “颜色”应该有一个限制,只能保持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:整数。它实际上是一个xs:字符串。

回答

23

您可以定义与以下类似的属性。此示例使用模式来限制该值,但如果更合适,也可以使用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"/> 

UPDATE(响应从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> 
+1

点点短:'[0-9] {3}' – 13ren

+0

谢谢您的输入。上面我的编辑。我知道如何做一个xs:限制,但我不知道如何将它们合并为一个整体。请为我的示例提供更多或完整的内容。如果我理解正确,则带有SimpleType属性的复杂类型带有限制。 – ZiggyStardust

+0

@ZiggyStardust查看更新。 – David

1

以下应该工作

<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> 
+0

感谢您的意见。上面我的编辑。我知道如何做一个xs:限制,但我不知道如何将它们合并为一个整体。请为我的示例提供更多或完整的内容。如果我理解正确,则带有SimpleType属性的复杂类型带有限制。 – ZiggyStardust