views:

144

answers:

3

I am trying to create an element in an XML schema such that only standard (X)HTML elements can be used as children. What I've tried is this:

<xs:element name="description">
    <xs:complexType>
     <xs:sequence minOccurs="0" maxOccurs="unbounded">
      <xs:any namespace="http://www.w3.org/1999/xhtml" />
     </xs:sequence>
    </xs:complexType>
</xs:element>

Of course, this doesn't work, as the following XML doesn't explicitly specify the namespace:

<description>
    <p>this is a test</p>
    <p>this is a <b>bold</b> test</p>
    <h1>Those were the tests</h1>
</description>

Do I need to specify the namespace somewhere in the document, or can I get it in the schema?

+1  A: 

I think you need to disable the content processing thus:

  <xs:any namespace="http://www.w3.org/1999/xhtml" processContents="skip"/>

See section 5.5 in the XML Schema spec (particularly the examples)

Brian Agnew
I was hoping that there was a better way than saying "ignore it".
Eric
But you have to ignore it, because it's not valid. Use of the correct namespace is not optional in XML.
John Saunders
I was hoping that the schema could tell the xml document what the namespace was. If I were to explicitly state thenamespace, where would I do it?
Eric
A: 

Your schema looks ok. Note that the default value for xs:any/@processContents is strict, which means your XHTML elements will be also validated so you will need to have also an XHTML schema and import it from your schema. You can use processContents="lax" inside xs:any to specify that the validation will be applied only if there is a schema for those elements.

Your problem is in the instance where you should specify the namespace for the XHTML element. You can declare the XHTML namespace as default namespace on each element, for example

<p xmlns="http://www.w3.org/1999/xhtml"&gt;this is a test</p>

or you can declare it bound to a prefix, h for instance and then use that prefix to qualify your XHTML elements:

<description xmlns:h="http://www.w3.org/1999/xhtml"&gt;
  <h:p>this is a test</h:p>
  <h:p>this is a <b>bold</b> test</h:p>
  <h:h1>Those were the tests</h:h1>
</description>

DTDs are not namespace aware and there namepsace declarations are just attributes, thus it is possible to declare a fixed xmlns attribute on an element to put it in a specific namespace automatically. XML Schemas are namespace aware and you cannot have a namespace declaration as a fixed attribute.

George Bina
A: 

I think you really need to view this page: http://msdn.microsoft.com/en-us/library/ms190756.aspx

Nicolás