3

The XSD:

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:DataBodyTemperature="Docobo.DataBodyTemperature" attributeFormDefault="qualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="tDataBodyTemperature">
    <xs:sequence>
      <xs:element name="Answer" type="xs:double" />
      <xs:element minOccurs="0" maxOccurs="1" name="AmbientTemperature" type="xs:double" />
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="tDataItem">
      <xs:choice>
        <xs:element name="DataBodyTemperature" type="tDataBodyTemperature" />
      </xs:choice>
  </xs:complexType>
  <xs:element name="DataItem">
    <xs:complexType>
      <xs:complexContent mixed="false">
        <xs:extension base="tDataItem">
          <xs:attribute fixed="1" name="SchemaVersion" type="xs:integer" />
        </xs:extension>
      </xs:complexContent>
    </xs:complexType>
  </xs:element>
</xs:schema>

The XML:

<DataItem>
  <DataBodyTemperature xmlns:DataBodyTemperature="Docobo.DataBodyTemperature">
    <DataBodyTemperature:Answer>37.8</DataBodyTemperature:Answer>
    <DataBodyTemperature:AmbientTemperature>28.5</DataBodyTemperature:AmbientTemperature>
  </DataBodyTemperature >
</DataItem>

I am getting a validation error: Xml failed schema validation: The element 'DataBodyTemperature' has invalid child element 'Answer' in namespace 'Docobo.DataBodyTemperature'. List of possible elements expected: 'Answer'

Alberto
  • 5,021
  • 4
  • 46
  • 69
Shahid
  • 995
  • 3
  • 11
  • 24
  • The error message doesn't match your XML - there *is* no element named `DataBodyTemperature` in that XML. – skaffman Sep 21 '11 at 10:15
  • @skaffman: Sorry that was a mistake I have corrected the XML now and it has a body temperature element but I think there is some namespace issue which causes this error – Shahid Sep 21 '11 at 10:40

1 Answers1

4

Your problem is that your schema doesn't specify a target namespace - consequently all types are associated with the null namespace.

Your error is caused because the schema specifies that the DataBodyTemperature element (which is in the null namespace) should contain only Answer and AmbientTemperature elements (both of which are declared in the null namespace), however in your document these elements are in fact in the Docobo.DataBodyTemperature namespace, essentially making them completely different elements.

A sample of xml conforming to your given schema would be:

<DataItem SchemaVersion="1">
  <DataBodyTemperature>
    <Answer>1</Answer>
    <AmbientTemperature>1</AmbientTemperature>
  </DataBodyTemperature>
</DataItem>

Notice that there is no namespace declaration - all elements are in the default namespace. I suspect what you really want however is to modify your xsd so that it specifies a target namespace.

<xs:schema targetNamespace="Docobo.DataBodyTemperature" xmlns:dbt="Docobo.DataBodyTemperature" attributeFormDefault="qualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="tDataBodyTemperature">
    <xs:sequence>
      <xs:element name="Answer" type="xs:double" />
      <xs:element minOccurs="0" maxOccurs="1" name="AmbientTemperature" type="xs:double" />
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="tDataItem">
    <xs:choice>
      <xs:element name="DataBodyTemperature" type="dbt:tDataBodyTemperature" />
    </xs:choice>
  </xs:complexType>
  <xs:element name="DataItem">
    <xs:complexType>
      <xs:complexContent mixed="false">
        <xs:extension base="dbt:tDataItem">
          <xs:attribute fixed="1" name="SchemaVersion" type="xs:integer" />
        </xs:extension>
      </xs:complexContent>
    </xs:complexType>
  </xs:element>
</xs:schema>

Note that you now also need to qualify the tDataItem and tDataBodyTemperature types as they are no longer declared in the null namespace.

Also note that in your sameple XML the DataItem and DataBodyTemperature elements aren't in the "Docobo.DataBodyTemperature" namespace and so now wouldn't be validated against the above schema.

You may also find it helpeful to get a sample xml document for a schema - you can do this in Visual Studio 2008 or later using the XML Schema Explorer, see How to generate sample XML documents from their DTD or XSD?.

Community
  • 1
  • 1
Justin
  • 84,773
  • 49
  • 224
  • 367
  • No the schema specifies a DataItem element of type tDataItem and the tDataItem has a choice of elements (currently only DataBodyTemperature). The DataBodyTemperature is of type tDataBodyTemperature which then contains 2 elements; The answer and AmbientTemperature – Shahid Sep 21 '11 at 10:43
  • @Shahid Ah - you updated your question, I've now fixed my answer. – Justin Sep 21 '11 at 11:01
  • Thank you justin. I put just one choice 'DataBodyTemperature' here but I have many other choices all with different namespace qualifiers. Can we use multiple target namespaces. Is it possible to only qualify the tDataBodyTemperature with a namespace and leave any other elements with the null namespace – Shahid Sep 21 '11 at 11:06
  • 1
    @Shahid Yes you can use multiple target namespaces, however to my knowledge you would need a different XSD schema document for each namespace, for example one document which describes all elements in the null namespace and another describing all elements in the `Docobo.DataBodyTemperature` namespace. – Justin Sep 21 '11 at 11:11