2014-02-13 93 views
0

我试图创建此元素的XML架构...XML架构复杂类型属性

<shoesize country="yes">35</shoesize> 

这是解决方案....

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
<xs:element name="shoesize"> 
    <xs:complexType> 
    <xs:simpleContent> 
     <xs:extension base="xs:integer"> 
     <xs:attribute name="country" type="xs:string" /> 
    </xs:extension> 
    </xs:simpleContent> 
    </xs:complexType> 
</xs:element> 
</xs:schema> 

我我试图限制的是,该属性只能是“是”或“否”,内容只能是小于50的整数。任何人都可以给我一些指示,请问如何做到这一点。


行,所以我做了它在单独的文件工作,但是当我把这个代码放到我的大架构中

<xsd:sequence> 
     <xsd:element name="something" type="xsd:string"/> 
     <xsd:element name="something else" type="xsd:string"/> 
     ...... 
     ...... 
     code above 
     .... 
     ... 
</xsd:sequence> 

我得到错误

s4s-elt-must-match.1: The content of 'sequence' must match (annotation?, (element | group | choice | sequence | any)*). 

回答

1

你必须这样做在两个阶段中,首先定义一个名为顶级simpleType来限制内容(将所有现有的xs:element声明,直接在xs:schema之外)

<xs:simpleType name="lessThanFifty"> 
    <xs:restriction base="xs:integer"> 
    <xs:maxExclusive value="50" /> 
    </xs:restriction> 
</xs:simpleType> 

然后让你的complexType扩展,以添加属性

<xs:element name="shoesize"> 
<xs:complexType> 
    <xs:simpleContent> 
    <xs:extension base="lessThanFifty"> 
    <xs:attribute name="country"> 
    <!-- you might want to pull this out into a top-level type if you 
      have other yes/no attributes elsewhere in the schema --> 
    <xs:simpleType> 
     <xs:restriction base="xs:string"> 
     <xs:enumeration value="yes" /> 
     <xs:enumeration value="no" /> 
     </xs:restriction> 
    </xs:simpleType> 
    </xs:attribute> 
    </xs:extension> 
    </xs:simpleContent> 
</xs:complexType> 
</xs:element> 

这将允许任何整数值直到并包括49,所以-500是一个有效的值。开始限制从xs:nonNegativeInteger而不是xs:integer开始可能更合适。

+0

当我尝试验证它时,我得到2个字符... s4s-att-must-appear:属性'name'必须出现在元素'complexType'中。 和cvc-elt.1.a:找不到元素'shoesize'的声明。 – Stribor

+0

实际上我解决了它..谢谢 – Stribor

+0

@Stribor编辑,使其更清晰,命名的'simpleType'必须在模式的顶层,而不是在您现有的'序列'里面。 –

相关问题