问题
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?
回答1:
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>
回答2:
I googled for xhtml xsd and found XHTML 1.0 in XML Schema on w3c. There is the xhtml1-strict.xsd linked. If you look into that you'll find the definition for the body tag. You can use this as the base for your html tag:
<xs:element name="body">
<xs:complexType>
<xs:complexContent>
<xs:extension base="Block">
<xs:attributeGroup ref="attrs" />
<xs:attribute name="onload" type="Script" />
<xs:attribute name="onunload" type="Script" />
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:element>
回答3:
I wanted to use XHTML 1.1.
Here's a schema that does that based on ColdFusion's answer, the XHTML 1.1 XSD, and this answer.
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/NewXMLSchema"
xmlns:tns="http://www.example.org/NewXMLSchema"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
elementFormDefault="qualified">
<!-- Importing XHTML 1.1 namespace -->
<import namespace="http://www.w3.org/1999/xhtml"
schemaLocation="http://www.w3.org/MarkUp/Schema/xhtml11.xsd"/>
<!--
Define the xhtml type.
I didn't want forms in my special xhtml-like element.
-->
<xs:complexType name="xhtml">
<xs:group ref="xhtml:xhtml.BlkNoForm.mix"/>
</xs:complexType>
<!-- Defining a type that matches the xhtml <body> element. -->
<xs:complexType name="xhtml2">
<xs:complexContent>
<xs:extension base="xhtml:xhtml.body.type"/>
</xs:complexContent>
</xs:complexType>
<xs:element name="my-xhtml-element" type="tns:xhtml"></xs:element>
</schema>
来源:https://stackoverflow.com/questions/17012597/how-can-i-include-html-in-my-xml-schema