Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I include Html in my Xml Schema

Tags:

xml

xsd

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>

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.

How can I include the tags into my XSD?

like image 251
CLo Avatar asked Dec 15 '22 09:12

CLo


1 Answers

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

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

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

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

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

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>
like image 56
ColdFusion Avatar answered Jan 10 '23 02:01

ColdFusion