XML Schema 如何通过枚举限制属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8925706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XML Schema How to Restrict Attribute by Enumeration
提问by Luke
I have the following XML Tag
我有以下 XML 标签
<price currency="euros">20000.00</price>
How do I restrict the currency attribute to one the following:
如何将货币属性限制为以下之一:
- euros
- pounds
- dollars
- 欧元
- 磅
- 美元
AND the price to a double?
而且价格翻倍?
I just get an error when I try to a type on both, here's what I've got so far:
当我尝试在两者上输入一个类型时,我只是收到一个错误,这是我到目前为止所得到的:
<xs:element name="price">
<xs:complexType>
<xs:attribute name="currency">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="pounds" />
<xs:enumeration value="euros" />
<xs:enumeration value="dollars" />
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
回答by user998692
The numerical value seems to be missing from your price definition. Try the following:
您的价格定义中似乎缺少数值。请尝试以下操作:
<xs:simpleType name="curr">
<xs:restriction base="xs:string">
<xs:enumeration value="pounds" />
<xs:enumeration value="euros" />
<xs:enumeration value="dollars" />
</xs:restriction>
</xs:simpleType>
<xs:element name="price">
<xs:complexType>
<xs:extension base="xs:decimal">
<xs:attribute name="currency" type="curr"/>
</xs:extension>
</xs:complexType>
</xs:element>
回答by kjhughes
New answer to old question
旧问题的新答案
None of the existing answers to this old question address the real problem.
这个老问题的现有答案都没有解决真正的问题。
The real problemwas that xs:complexTypecannot directly have a xs:extensionas a child in XSD. The fix is to use xs:simpleContentfirst. Details follow...
真正的问题是xs:complexType不能xs:extension在 XSD 中直接将 a作为孩子。解决方法是先使用xs:simpleContent。详情如下...
Your XML,
你的 XML,
<price currency="euros">20000.00</price>
will be valid against eitherof the following corrected XSDs:
将适用于以下任一更正的 XSD:
Locally defined attribute type
本地定义的属性类型
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="price">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:decimal">
<xs:attribute name="currency">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="pounds" />
<xs:enumeration value="euros" />
<xs:enumeration value="dollars" />
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:schema>
Globally defined attribute type
全局定义的属性类型
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="currencyType">
<xs:restriction base="xs:string">
<xs:enumeration value="pounds" />
<xs:enumeration value="euros" />
<xs:enumeration value="dollars" />
</xs:restriction>
</xs:simpleType>
<xs:element name="price">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:decimal">
<xs:attribute name="currency" type="currencyType"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:schema>
Notes
笔记
- As commented by @Paul, these do change the content type of
pricefromxs:stringtoxs:decimal, but this is not strictly necessary and was not the real problem. - As answered by @user998692, you could separate out the
definition of currency, and you could change to
xs:decimal, but this too was not the real problem.
- 正如评论说@保罗,这些做更改的内容类型
price,从xs:string到xs:decimal,但这不是绝对必要的,并没有真正的问题。 - 正如@user998692所回答的,您可以将货币的定义分开,您可以更改为
xs:decimal,但这也不是真正的问题。
The real problemwas that xs:complexTypecannot directly have a xs:extensionas a child in XSD; xs:simpleContentis needed first.
真正的问题是在 XSD 中xs:complexType不能直接有xs:extension一个子项;xs:simpleContent首先需要。
A related matter (that wasn't asked but may have confused other answers):
一个相关的问题(没有被问到,但可能混淆了其他答案):
How could pricebe restricted given that it has an attribute?
price鉴于它具有属性,如何限制?
In this case, a separate, global definition of priceTypewould be needed; it is not possible to do this with only local type definitions.
在这种情况下,需要一个单独的全局定义priceType;仅使用本地类型定义是不可能做到这一点的。
How to restrict element content when element has attribute
当元素具有属性时如何限制元素内容
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="priceType">
<xs:restriction base="xs:decimal">
<xs:minInclusive value="0.00"/>
<xs:maxInclusive value="99999.99"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="price">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="priceType">
<xs:attribute name="currency">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="pounds" />
<xs:enumeration value="euros" />
<xs:enumeration value="dollars" />
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:schema>
回答by A.B.Cade
you need to create a type and make the attribute of that type:
您需要创建一个类型并创建该类型的属性:
<xs:simpleType name="curr">
<xs:restriction base="xs:string">
<xs:enumeration value="pounds" />
<xs:enumeration value="euros" />
<xs:enumeration value="dollars" />
</xs:restriction>
</xs:simpleType>
then:
然后:
<xs:complexType>
<xs:attribute name="currency" type="curr"/>
</xs:complexType>
回答by Nachiket Kulkarni
<xs:element name="price" type="decimal">
<xs:attribute name="currency" type="xs:string" value="(euros|pounds|dollars)" />
</element>
This would eliminate the need for enumeration completely. You could change type to double if required.
这将完全消除枚举的需要。如果需要,您可以将 type 更改为 double。

