簡體   English   中英

XSD 節點具有以任意順序出現任意次數的混合內容

[英]XSD node with mixed content that appears any number of times in any order

我被困在如何創建和 XSD 上,它允許“對象”節點的子節點是“文本”或“圖像”節點,可以任意次數和任意順序顯示。 它們出現在“對象”節點中的順序決定了它們的呈現方式,但不需要驗證順序。

示例 1

<objects>
    <textobject x="30" y="100" value="blah1" />
    <imageobject x="0" y="0" src="/path/to/some/image1.png"/> 
    <imageobject x="0" y="0" src="/path/to/some/image2.png"/>
    <textobject x="60" y="250" value="blah2" />
    <textobject x="60" y="250" value="blah3" />
</objects>

示例 2

<objects> 
    <imageobject x="0" y="0" src="/path/to/some/image1.png"/>
    <textobject x="30" y="100" value="blah1" />
    <textobject x="60" y="250" value="blah2" />
    <imageobject x="0" y="0" src="/path/to/some/image2.png"/>
    <textobject x="60" y="250" value="blah3" />
</objects>

謝謝!

在這種情況下,使用替換組可能很合適。 將“mediaObject”定義為抽象元素,將“textObject”和“imageObject”作為其替換組的成員,然后將內容model定義為<xs:element ref="mediaObject" minOccurs="0" maxOccurs="unbounded"/> . 這種設計的優點是擴展性更強,實現了關注點分離,語義表達更好,定義的復用性更高。 當有 15 種媒體 object 而不是兩種時,好處才真正開始顯現。

使用<xs:choice maxOccurs="unbounded">

您可以將xs:choiceminOccurs="0"maxOccurs="unbounded"一起使用:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:element name="objects">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element ref="imageobject"/>
        <xs:element ref="textobject"/>
      </xs:choice>
    </xs:complexType>
  </xs:element>
  <xs:element name="imageobject">
    <xs:complexType>
      <xs:attribute name="src" use="required"/>
      <xs:attribute name="x" use="required" type="xs:integer"/>
      <xs:attribute name="y" use="required" type="xs:integer"/>
    </xs:complexType>
  </xs:element>
  <xs:element name="textobject">
    <xs:complexType>
      <xs:attribute name="value" use="required"/>
      <xs:attribute name="x" use="required" type="xs:integer"/>
      <xs:attribute name="y" use="required" type="xs:integer"/>
    </xs:complexType>
  </xs:element>
</xs:schema>

暫無
暫無

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

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