簡體   English   中英

通過XSD以任意順序允許任何數量的XML元素?

[英]Allow XML element in any order with any number of appearance via XSD?

我很難用這些約束編寫XSD: 名稱元素是必需的; 元素可以出現0次或多次; 類別元素是可選的。 我現在有這樣的XSD:

<xs:element name="Detail" maxOccurs="unbounded" minOccurs="0"> <!-- 0 or more Details -->
  <xs:complexType>
    <xs:sequence>
      <xs:element type="xs:string" name="Name"/> <!-- Name element is required -->
      <xs:element type="xs:string" name="Value" maxOccurs="unbounded" minOccurs="0"/> <!-- 0 or more occurences of Value element -->
      <xs:element type="xs:string" name="Category" minOccurs="0" maxOccurs="1"/> <!-- optional (0 or 1) Category element -->
    </xs:sequence>
  </xs:complexType>
</xs:element>

問題是輸入XML可能不遵循模式中的順序。 xs:choicexs:都有外觀約束,對我來說還不夠好。

任何人都可以幫我解決這個問題,允許XML元素以任何順序出現在任何次序中嗎?

附上示例輸入XML:

<Detail>
    <Category />
    <Name> Thanks </Name>
    <Value> 1 </Value>
    <Value> 2 </Value>
</Detail>

XSD 1.0

不可能。 要么強制執行訂單,要么放寬發生約束。

XSD 1.1

可能,因為在XSD 1.1中, xs:all現在可能有@maxOccurs="unbounded"

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
           elementFormDefault="qualified"
  vc:minVersion="1.1">
  <xs:element name="Detail"> 
    <xs:complexType>
      <xs:all>
        <xs:element type="xs:string" name="Name"/>
        <xs:element type="xs:string" name="Value" 
                    minOccurs="0" maxOccurs="unbounded" />
        <xs:element type="xs:string" name="Category" 
                    minOccurs="0" maxOccurs="1"/>
      </xs:all>
    </xs:complexType>
  </xs:element>
</xs:schema>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM