1

I have an XML element which contains other elements, attributes as well as text too. A sample XML data is here:

<property readonly="0" eraseable="1" >
    <property readonly="0" eraseable="1" >
        <property readonly="0" eraseable="1" >windows XP unknown SP unknown (Build unknown)</property>
        <property readonly="0" eraseable="1" >Windows 5.1</property>
    </property>
</property>

I want to define XSD schema for the element. I tried adding SimpleContent to ComplexType but I get error on validation. Here is the schema I am trying:

<xs:element name="property">
    <xs:complexType >
        <xs:simpleContent>
            <xs:extension base="xs:string"/>
        </xs:simpleContent>
        <xs:sequence>
            <xs:element ref="property" minOccurs="0" maxOccurs="unbounded"/>
        </xs:sequence>
        <xs:attribute name="id" use="optional" type="xs:integer"/>
        <xs:attribute name="readonly" use="required" type="xs:boolean"/>
        <xs:attribute name="eraseable" use="required" type="xs:boolean"/>
    </xs:complexType>
</xs:element>

The error that I get on validation is:

*The content of '#AnonType_property' is invalid. Element 'sequence' is invalid, misplaced, or occurs too often.*

If I remove the <xs:simpleContent> from schema definition, I get error as:

Element 'property' cannot have character [children], because the type's content type is element-only.

How do I write the schema for such element?

2 Answers 2

1

Will this work for you?

<xs:element name="property">
   <xs:complexType mixed="true">
      <xs:sequence>
         <xs:element minOccurs="0" maxOccurs="unbounded" ref="property"/>
      </xs:sequence>
      <xs:attribute name="eraseable" use="required" type="xs:integer"/>
      <xs:attribute name="readonly" use="required" type="xs:integer"/>
   </xs:complexType>
</xs:element>
1
  • The "mixed" attribute turns on some crazy texts in the elements :) I know. But problem is that it does not restrict the text to between the element start-end tags. But thanks for the suggestion.
    – Shreerang
    Jun 16, 2011 at 19:41
0

If you use visual studio to generate the schema from your XML snippet you get this:

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="property">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="property">
          <xs:complexType>
            <xs:sequence>
              <xs:element maxOccurs="unbounded" name="property">
                <xs:complexType>
                  <xs:simpleContent>
                    <xs:extension base="xs:string">
                      <xs:attribute name="readonly" type="xs:unsignedByte" use="required" />
                      <xs:attribute name="eraseable" type="xs:unsignedByte" use="required" />
                    </xs:extension>
                  </xs:simpleContent>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
            <xs:attribute name="readonly" type="xs:unsignedByte" use="required" />
            <xs:attribute name="eraseable" type="xs:unsignedByte" use="required" />
          </xs:complexType>
        </xs:element>
      </xs:sequence>
      <xs:attribute name="readonly" type="xs:unsignedByte" use="required" />
      <xs:attribute name="eraseable" type="xs:unsignedByte" use="required" />
    </xs:complexType>
  </xs:element>
</xs:schema>
3
  • Yeah, because its only considering the one example case. The <property> element can be nested any number of times so I don't want to hard code that. Also, at any nesting level, the element can have text. Essentially, the element is same at any level and text is optional.
    – Shreerang
    Jun 17, 2011 at 13:18
  • OK I see what you wanted to do now. Sorry for the stupid post. Jun 20, 2011 at 7:46
  • However you cannot mix simple and complex content in a single element. So maybe you should think about hard coding your to most likely instance documents. Jun 20, 2011 at 7:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.