XSD设计了一个动态子结构的xml元素

时间:2014-01-16 09:46:57

标签: xml xsd structure

我猜动词这个词有误导性。我感兴趣的元素可能有三种可能的子结构。一个例子可能是:

<body> 
    <date></date>
    <addr></addr>
</body>

<body>
    <loc></loc>
    <time> </time>
    <city> </city>
</body>

<body>
    //a whole lot of different sub elements
</body>

我陷入设计XSD,可以用来验证上面的三个结构(三个结构是已知和固定的)。我不确定我是否正确地提出了这个问题。请详细说明解决方案,甚至是它的可行性。

2 个答案:

答案 0 :(得分:2)

我认为这样的事情会起作用:

<xs:group name="group_one">
  <xs:sequence>
    <xs:element name="date"/>
     <xs:element name="addr"/>
  </xs:sequence>
</xs:group>

<xs:group name="group_two">
  <xs:sequence>
    <xs:element name="loc"/>
    <xs:element name="time"/>
    <xs:element name="city"/>
  </xs:sequence>
</xs:group>

<xs:complexType name="body">
  <xs:choice>
    <xs:group ref="group_one" />
    <xs:group ref="group_two"/>
  </xs:choice>
</xs:complexType>

您当然可以添加更多群组,并有更多选择。

答案 1 :(得分:0)

您可以定义&lt; body&gt;使用CDATA包含任意xml片段。

    <xs:element name="body" type="xs:CDATA"/>

更多信息,请参阅http://www.w3.org/TR/2000/CR-xmlschema-2-20001024/#CDATA

相关问题