繁体   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