允许嵌入html的XML Schema

时间:2012-09-11 20:53:26

标签: xml xsd

在我的xml架构中,我有一个名为itemsetting的标签:

    <xs:element name="itemsetting">
    <xs:complexType>
        <xs:simpleContent>
            <xs:extension base="xs:string">
                <xs:attribute name="key" use="required">
                    <xs:simpleType>
                        <xs:restriction base="xs:string">
                            <xs:enumeration value="questionscript"/>
                            <xs:enumeration value="timeframe"/>
                            <xs:enumeration value="textlabel"/>
                            <xs:enumeration value="textboxtype"/>
                        </xs:restriction>
                    </xs:simpleType>
                </xs:attribute>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>
</xs:element>

我想做的是能够将html嵌入到问题类型中。例如:

<itemsetting key="questionscript">this<html:b>is bold </html:b> </itemsetting>

我试图摆弄复杂/简单的时间,每次我最终得到一个无法解析的模式文件。正确方向的指针非常有用。

2 个答案:

答案 0 :(得分:3)

扩展迈克尔的答案,如下:

<xs:element name="itemsetting">
  <xs:complexType mixed="true">
    <xs:sequence>
      <xs:any namespace="http://www.w3.org/1999/xhtml" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
    <xs:attribute name="key" use="required">
      <xs:simpleType>
        <xs:restriction base="xs:string">
          <xs:enumeration value="questionscript"/>
          <xs:enumeration value="timeframe"/>
          <xs:enumeration value="textlabel"/>
          <xs:enumeration value="textboxtype"/>
        </xs:restriction>
      </xs:simpleType>
    </xs:attribute>
  </xs:complexType>
</xs:element>

应该有效 - 假设http://www.w3.org/1999/xhtml是与XML中html前缀对应的HTML命名空间。

如果您有多个名称空间,或者您不想使用命名空间检查,请使用

 . . . 
      <xs:any namespace="##any" minOccurs="0" maxOccurs="unbounded"/>
 . . . 

请注意,这假设嵌入式HTML是格式良好的XML,如果 - 例如 - 它包含不会使整个XML文件不可读的封闭标记,那么就无法使用模式。

答案 1 :(得分:2)

您的元素没有简单的内容:它包含子元素,这意味着它是复杂的内容(具体来说,<complexContent mixed="true">)。

如果要允许HTML命名空间中的任何子元素,可以通过使用单个通配符粒子<xs:any namespace="..." minOccurs="0" maxOccurs="unbounded"/>

定义内容模型来实现这一点。