xsd中混合模式的约束

时间:2012-10-24 18:10:14

标签: xml xsd

我想创建一个xsd,允许以下内容:

<document>
  Here is first paragraph with e.g. <i>itallic</i> and <b>bold</b>.

  <p>Here is the second paragraph also with some <i>itallic</i></p>

  <p>Here is the third paragraph</p>

  <!-- If there is any character data here it should be rejected -->
</document>

例如,我想允许第一段没有&lt; p&gt;标记它周围,但后续段落必须有它。

关于我应该看什么的任何提示?在我看来,通过在complexType定义上放置mixed =“true”,我无法得到我想要的东西。

更新:这不是因为第一段很特别。这只是因为我想避免写一些标签。例如,我希望能够通过以下方式制作订单:

<ol>
   <le>Here is the first list element, only one paragraph, easy to write</le>
   <le>Here is the second element.
       <p>The second element has an extra paragaph.</p>
   </le>
</ol>

正常情况是每个列表元素中只有一个段落,因此必须同时写入&lt; le&gt;和&lt; p&gt;。不过,我想支持列表元素中多个段落的异常情况。

1 个答案:

答案 0 :(得分:0)

事实上,你无法通过XSD 1.0获得你想要的东西。虽然XSD 1.0混合内容可以控制实例XML中出现的子元素的顺序和数量,但它无法控制其中的文本。假设您不允许在&lt; p&gt;之外的任何文本,拆分这两个“领域”可能是您唯一的选择。标签:第一个是混合内容,对于第一段,其他的将是具有混合或任何内容的p标签的任意重复序列。

我在说明一个可能的例子,假设第一段是“特殊的”,因为它是一个“介绍”;所以对于这个XML:

<document> 
  <intro>Here is first paragraph with e.g. <i>itallic</i> and <b>bold</b>.</intro>  
  <p>Here is the second paragraph also with some <i>itallic</i></p>  
  <p>Here is the third paragraph</p>  
</document> 

您可以定义类似的内容:

<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)-->
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="document">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="intro">
          <xsd:complexType mixed="true">
            <xsd:sequence>
              <xsd:element name="i" type="xsd:string" />
              <xsd:element name="b" type="xsd:string" />
            </xsd:sequence>
          </xsd:complexType>
        </xsd:element>
        <xsd:element maxOccurs="unbounded" name="p">
          <xsd:complexType mixed="true">
            <xsd:sequence minOccurs="0">
              <xsd:element name="i" type="xsd:string" />
            </xsd:sequence>
          </xsd:complexType>
        </xsd:element>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

当然,你必须改进标记的使用方式,但你明白了......你的转换可以解决剥离&lt; intro&gt;标签根据需要。

如果您的提示请求意味着您可能使用的其他架构语言,那么......

  • 如果您可以部署XSD 1.1架构,新的xsd:assert可能会对您有所帮助。

  • 如果您可以部署其他架构语言,那么Relax NG就是您的门票;试试introduction