如何在我的Xml架构中包含Html

Chr*_*ris 3 xml xsd

我试图允许Html标签作为我的一种类型的孩子.

<xs:complexType name="Html">
    <xs:sequence>
        <!-- Attempting to allow us to include necessary HTML right into our XML -->
        <xs:any minOccurs="0" namespace="http://www.w3.org/1999/xhtml"></xs:any>
    </xs:sequence>
</xs:complexType>

<xs:element name="Html" type="Html"></xs:element>
Run Code Online (Sandbox Code Playgroud)

目的是允许在该类型的任何元素内部使用Html标签,但不一定需要为良好形成的html包含周围的html或body标签.

如何将标签包含在我的XSD中?

Col*_*ion 8

如果要在XML中使用自定义元素以及HTML标记(即元素),它们应该是XHTML元素.

当然,你可以定义一些你自己的HTML标签,但这看起来很像HTML,因为只有你才会知道这是'HTML'.(此外,您必须根据需要定义HTML的所有元素,这将构成非常重要的模式!)

为了让每个人都知道你确实使用HTML元素,它们必须属于XHTML命名空间:

http://www.w3.org/1999/xhtml
Run Code Online (Sandbox Code Playgroud)

并且该命名空间由W3C定义和控制.因此,您只需将XHTML命名空间导入到模式中,而不是定义自己的东西,这意味着导入XHTML的模式.XHTML的架构可通过以下URL找到:http://www.w3.org/2002/08/xhtml/xhtml1-strict.xsd

那么,你的初始XSD我会重写如下:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xhtml="http://www.w3.org/1999/xhtml">

  <!-- Importing XHTML namespace -->
  <xs:import namespace="http://www.w3.org/1999/xhtml"
      schemaLocation="http://www.w3.org/2002/08/xhtml/xhtml1-strict.xsd"/>

  <!-- 
    Here, you define your 'Html' type the same as they define
    the content of <body> element.

    Notice that 'xhtml' namespace prefix must be used with each reference
    to a W3C XHTML component.
  -->
  <xs:complexType name="Html">
    <xs:complexContent>
      <xs:extension base="xhtml:Block">
        <xs:attributeGroup ref="xhtml:attrs"/>
        <xs:attribute name="onload" type="xhtml:Script"/>
        <xs:attribute name="onunload" type="xhtml:Script"/>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

  <!-- Now, your custom 'Html' element has the same content model
       as the standard XHTML <body> element! -->
  <xs:element name="Html" type="Html"></xs:element>

</xs:schema>
Run Code Online (Sandbox Code Playgroud)