![](/img/trans.png)
[英]How to transform unordered elements in xml to match an xsd:sequence order?
[英]XML validation with XSD: how to avoid caring about the sequence of the elements?
我有以下XSD代碼:
<xsd:complexType name="questions">
<xsd:sequence>
<xsd:element name="location" type="location"/>
<xsd:element name="multipleChoiceInput" type="multipleChoiceInput" minOccurs="0" maxOccurs="unbounded"/>
<xsd:element name="textInput" type="textInput" minOccurs="0" maxOccurs="unbounded"/>
<xsd:element name="pictureInput" type="pictureInput" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
這里的問題是:元素位置,multipleChoiceInput等必須以它們聲明的相同順序出現。 我不希望這種情況發生,我希望在驗證過程中,序列不應該相關。 我怎樣才能做到這一點?
我嘗試過的另一種可能性是:
<xsd:complexType name="questions">
<xsd:choice maxOccurs="unbounded">
<xsd:element name="location" type="location"/>
<xsd:element name="multipleChoiceInput" type="multipleChoiceInput" minOccurs="0" maxOccurs="unbounded"/>
<xsd:element name="textInput" type="textInput" minOccurs="0" maxOccurs="unbounded"/>
<xsd:element name="pictureInput" type="pictureInput" minOccurs="0" maxOccurs="1"/>
</xsd:choice>
</xsd:complexType>
在這個例子中,序列真的無關緊要,我可以擁有我想要的那么多元素(“所有”不允許我做什么)。 但我仍然遇到min-和maxOccurs的問題。 在這個例子中,我可以擁有盡可能多的“pictureInput”,但是我希望擁有0或1的約束。
非常感謝您的幫助!
<xsd:complexType name="questions">
<xsd:all>
<xsd:element name="location" type="location"/>
<xsd:element name="multipleChoiceInput" type="multipleChoiceInput"/>
<xsd:element name="textInput" type="textInput"/>
<xsd:element name="pictureInput" type="pictureInput"/>
</xsd:all>
</xsd:complexType>
注意:我已將“序列”更改為“全部”
序列強制順序(如定義)。 如果訂單無關緊要則全部使用。
如果元素出現的可能性不止一次,則可以使用xsd:any。
<xsd:complexType name="questions">
<xsd:sequence>
<xsd:any minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
您可以在以下鏈接中找到xsd:any的詳細信息:
我討論的時間有點晚,但我遇到了同樣的問題並找到了解決方案:
<xsd:complexType name="questions">
<xsd:choice maxOccurs="unbounded">
<xsd:element name="location" type="location"/>
<xsd:element name="multipleChoiceInput" type="multipleChoiceInput"/>
<xsd:element name="textInput" type="textInput"/>
<xsd:element name="pictureInput" type="pictureInput"/>
</xsd:choice>
</xsd:complexType>
關鍵是將xs:choice與maxOccurs =“unbounded”結合起來。 如果您只使用xs:all,則允許每個句點中的一個。
編輯添加:雖然xs:any將起作用,但它不會將您的選擇限制為逐項列出的四個元素。 它將允許任何東西,這幾乎違背了模式的目的。
這里聚會也很晚,但是將<xsd:all>
和minOccurs
一起使用並且maxOccurs
不起作用?:
<xsd:complexType name="questions">
<xsd:all>
<xsd:element name="location" type="location" minOccurs="0" maxOccurs="1"/>
<xsd:element name="multipleChoiceInput" type="multipleChoiceInput" minOccurs="0" maxOccurs="1"/>
<xsd:element name="textInput" type="textInput" minOccurs="0" maxOccurs="1"/>
<xsd:element name="pictureInput" type="pictureInput" minOccurs="0" maxOccurs="1"/>
</xsd:all>
</xsd:complexType>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.