如何将限制与XSD文件中的出现指示符结合使用?
假设我有以下XSD文件:如何将限制与XSD文件中的出现指示符结合使用?
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="PACIDemoSignedDoc" type="PACIDemoSignedDocType" />
<xs:complexType name="PACIDemoSignedDocType">
<xs:all>
<xs:element name="OwnerEnglishName" type="OwnerEnglishNameType" />
</xs:all>
</xs:complexType>
<xs:complexType name="OwnerEnglishNameType">
<xs:simpleContent>
<xs:restriction base="NameType">
<xs:enumeration value="John"/>
<xs:enumeration value="Jack"/>
</xs:restriction>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="NameType">
<xs:simpleContent>
<xs:extension base="xs:string"/>
</xs:simpleContent>
</xs:complexType>
</xs:schema>
我的问题是,我应该在哪里添加属性的minOccurs和maxOccurs的元素“OwnerEnglishName”?我想保留xs:所有,因为我想避免必须按顺序排序我的XML文件,但它禁止直接在<xs:element name="OwnerEnglishName" type="OwnerEnglishNameType" />
行中添加Occurs ...
我也猜测我不能添加OwnerEnglishNameType中的Occur属性,任何人有任何想法?
好了,我已经解决了我的问题,这里的答案,如果别人有同样的问题:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" targetNamespace="test" xmlns="test" elementFormDefault="qualified" version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="animal" type="AnimalType" />
<xs:complexType name="AnimalType">
<xs:all>
<xs:element name="cats" type="Cats" />
</xs:all>
</xs:complexType>
<xs:simpleType name="CatType">
<xs:restriction base="xs:string">
<xs:enumeration value="John"/>
<xs:enumeration value="Jack"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="Cats">
<xs:sequence>
<xs:element maxOccurs="5" minOccurs="2" name="cat" type="Cat"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Cat">
<xs:simpleContent>
<xs:extension base="CatType"/>
</xs:simpleContent>
</xs:complexType>
</xs:schema>
这验证以下XML:
<?xml version="1.0" encoding="UTF-8"?>
<animal xmlns="d" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="test schema.xsd">
<cats>
<cat>John</cat>
<cat>Jack</cat>
</cats>
</animal>
我已成功通过添加xs:sequence后面的子元素来绕过xs:all的Occurs限制。
*我已经成功绕过'xs:all' *的发生限制:**不,你没有。**你**(1)**改变了示例(为什么?),* *(2)**仍然使用带有单个子元素的'xs:all'(它不会购买任何东西),并且**(3)**应用了[着名的包装技术](http://stackoverflow.com/questions/37969890/unordered-elements-in-xsd-with-obligatory-and-unbounded-elements)(它不会绕过限制但改变XML设计以在其中工作)。 – kjhughes
我的问题是,我应该在哪里添加属性minOccurs和 的maxOccurs的元素
OwnerEnglishName
?
论OwnerEnglishName
(非全局)声明:
<xs:element name="OwnerEnglishName" type="OwnerEnglishNameType"
minOccurs="0" maxOccurs="1"/>
我想保持XS:所有我想避免下令在我的XML文件>序列
在XSD 1.0中,您不能在xs:all
下有maxOccurs =“unbounded”;在XSD 1.1中,你可以。
但是,您不需要带有单个子元素的xs:all
;您可以使用xs:sequence
,因为没有第二个元素用于处理重要事项。
更新(每例如在评论OP变化包括额外的子元素xs:all
):
然后,您有三种选择:
- 强加排序。这几乎总是最好的答案,因为 被认为需要允许任何排序在实践中几乎总是不必要的 。
- 使用XSD 1.1,允许
xsd:all
的子女拥有maxOccurs="unbounded"
。 - 将XML设计更改为在您希望允许重复的
xsd:all
的子元素周围使用包装元素。 (对于一个例子见here)
问题是,最后,我会有多个子元素。真的没有解决方法吗?我一直在拼命努力,使一些工作是这样的:
然后你的例子应该显示'xsd:all'的多个子元素,而不是一个。您有三种选择:请参阅** [XSD中的无序元素,带有强制和无界元素?](http://stackoverflow.com/questions/37969890/unordered-elements-in-xsd-with-obligatory-and-unbounded-elements )** – kjhughes
这是谁“禁止[你]添加”?我看不到,为什么' '(或类似的)不应该工作。 –
我想你的意思是错误'F [Saxon-EE 9.4.0.4]在之内,必须有@maxOccurs等于0或1'。请指定您想要的'minOccurs'和'maxOccures'值。 –
准确地说,我想添加不同于0和1的值:我没有任何价值,我只需要为这些属性输入任何值即可。 – Yohann