xsd design

I'm trying to build an xsd file to validate help xml files I am writing.

I have a node <p> that I want to have a single optional attribute (class) and allow any content under it....both text and html.  I'm getting the validation error stating that "parent element's content model is text only" when I try to add html. I don't know how to fix it.  I'm using VS 2010 to create the xsd and xml files.

Below is my xsd file

***********************
<?xml version="1.0" encoding="utf-8"?>
<schema id="helptopics"  elementFormDefault="qualified"  xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://myurl.com">
      <element name="topic">
            <complexType mixed="true">
                  <sequence>
                        <element name="title" type="string">
                        </element>
                        <element name="topicdescription" type="string" minOccurs="0">
                        </element>
                        <element name="content">
                              <complexType mixed="true">
                                    <choice minOccurs="0" maxOccurs="unbounded">
                                          <element name="p" minOccurs="0" maxOccurs="unbounded">
                                                <complexType mixed="true">
                                                      <attribute name="class" />
                                                </complexType>            
                                          </element>
                                          <element name="ul" minOccurs="0" maxOccurs="unbounded">
                                                <complexType>
                                                      <sequence minOccurs="1" maxOccurs="unbounded">
                                                            <element name="li" maxOccurs="unbounded">
                                                                  <complexType mixed="true"></complexType>
                                                            </element>
                                                      </sequence>
                                                </complexType>
                                          </element>
                                          <element name="ol" minOccurs="0" maxOccurs="unbounded">
                                                <complexType>
                                                      <choice minOccurs="1" maxOccurs="unbounded">
                                                            <element name="li" maxOccurs="unbounded">
                                                                  <complexType mixed="true">
                                                                  </complexType>
                                                            </element>
                                                      </choice>
                                                </complexType>
                                          </element>
                                          <element name="media" minOccurs="0" maxOccurs="unbounded">
                                                <complexType>
                                                      <attribute name="src">
                                                      </attribute>
                                                      <attribute name="alt" use="optional">
                                                            <simpleType>
                                                                  <restriction base="string" />
                                                            </simpleType>
                                                      </attribute>
                                                </complexType>
                                          </element>
                                    </choice>
                              </complexType>
                        </element>


                  </sequence>

            </complexType>
      </element>

</schema>
dbashley1Asked:
Who is Participating?
 
Zlatin ZlatevConnect With a Mentor Technical ArchitectCommented:
Sadly there is no easy way to define an element as containing html.
This is so, because html is effectivelly set of tags, and when "embeded" in an xml document, it is considered as part of the XML DOM.

You have the following options:
1. Include in your schema (a subset of) rules defined for xhtml schemas http://www.w3.org/TR/xhtml1-schema/#schemas However this will bloat your schema. The advantage is that you can specify which exact html tags are allowed as content.
2. Use CDATA sections and effectivelly have your html considered as text, when the xml is parsed. See http://stackoverflow.com/questions/1398571/html-inside-xml-should-i-use-cdata-or-encode-the-html The advantage is that you can have any html in the CDATA, however you may need to change the logic that currently works with your xml file.

Example of the later approach is the following XML snippet
<topic>
	<title>This is a sample with CDATA in content</title>
	<content><![CDATA[<p>You can put any html in here</p><div>Some test div</div>]]></content>
</topic>

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.