0

I am unable to understand certain behavior of declaring XML schema .

Question This xml schema works fine :

*<?xml version="1.0" encoding="windows-1252" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns="http://www.example.org" xmlns:ab="http://test.com"
            targetNamespace="http://www.example.org"
            elementFormDefault="qualified">
  <xsd:element name="simple1" type="complexType1"/>

  <xsd:complexType name="complexType1">
    <xsd:sequence>
      <xsd:element name="element1" type="xsd:string"/>
    </xsd:sequence>
  </xsd:complexType>
</xsd:schema>*

But if I change targetnamespace anything apart from http://www.example.org , the schema does not find complexType1. Why this happens. This does not work.

*<?xml version="1.0" encoding="windows-1252" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns="http://www.example.org" xmlns:ab="http://test.com"
            targetNamespace="http://www.example.org99999"
            elementFormDefault="qualified">
  <xsd:element name="simple1" type="complexType1"/>

  <xsd:complexType name="complexType1">
    <xsd:sequence>
      <xsd:element name="element1" type="xsd:string"/>
    </xsd:sequence>
  </xsd:complexType>
</xsd:schema>*

Thanks in advance

4

1 回答 1

0

When you declare a component such as a complexType, it goes in the targetNamespace of the schema. When you reference a component with an unprefixed name, as in your type="complexType1" attribute, this is taken as a reference to the default namespace (declared in the xmlns attribute). In your first example the targetNamespace and the default namespace are the same, so it works; in the second example they are different, so it doesn't.

How to fix it? That depends on what you are trying to achieve, which you haven't told us.

于 2013-10-20T16:57:56.047 回答