如何在 Xml Schema 中包含 Html [英] How can I include Html in my Xml Schema

查看:25
本文介绍了如何在 Xml Schema 中包含 Html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试允许 Html 标签作为我的一种类型的子标签.

I am trying to allow Html tags as children of one of my types.

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

目的是允许在该类型的任何元素中使用 Html 标签,但对于格式良好的 html,不一定需要有周围的 html 或 body 标签.

The intent is to allow Html tags, inside any element of that type, but not necessarily needing to have surrounding html or body tags for well formed html.

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

How can I include the tags into my XSD?

推荐答案

如果你想在你的 XML 中与你的自定义元素一起使用 HTML 标签(即元素),它们应该是 XHTML 元素.

If you want to use in your XML along with your custom elements also HTML tags (i.e. elements) they should be XHTML elements.

当然,您可以定义一些您自己的 HTML 标签,但这与 HTML 非常相似,因为只有您知道这是HTML".(此外,您必须根据需要定义 HTML 的所有元素,这将形成相当大的架构!)

Of course, you can define some your own HTML tags, but that will be rather HTML look-alike, because only you will know that this is 'HTML'. (Furthermore, you will have to define all the elements of your HTML as they need to be, which would make quite substantial schema!)

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

To make everyone know that you indeed use HTML elements, they must belong to XHTML namespace:

http://www.w3.org/1999/xhtml

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

and that namespace is defined and controlled by W3C. So, rather than defining something of your own, you simply should import the XHTML namespace into your schema, which means importing a schema for XHTML. The schema for XHTML is found by this URL: http://www.w3.org/2002/08/xhtml/xhtml1-strict.xsd

因此,您的初始 XSD 我将重写如下:

So, your initial XSD I would rewrite as the following:

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

这篇关于如何在 Xml Schema 中包含 Html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆