开发者

Global declarations within Xml Schema

a) Why must global element and attribute declarations always belong to a namespace and as such must always be qualified within instance documents?

b) Are local element/attribute declarations ( with their form attributes set to unqualified) still part of a schema’s target namespace, even though they aren’t associated with any namespace in instance documents?

c) Even if type/group declaration is global, it is still part of the target namespace and thus when referring to global declarations we must include the target namespace prefix.

Anyways, is there a particular reason why global declarations are part of a target namespace while local declarations aren’t? Namely, even if global type/group declarations wouldn’t belong to a target namespace, we could still refer to them within our Xml Schema, so there must be other benefits for them to be part of a namespace?!

Thank you

EDIT:

@user437291: If you understand the concept of the namespaces and qualified name why you use words "local" and "global" which is not a part of the concept?

But why do local type declarations need to be members of Xml Schema’s target namespace? They are not referenced from instance documents or other Xml Schemas, so what is the purpose/benefit of them being a member of a target namespace?

Moreover the question whether an element or an attribute belong to a namespace is really independent from the its "qualification". It is different properties like color and size of something.

How can an element/attribute belong to a namespace and at the same time doesn’t have to be qualified in instance documents? In programming languages if type T is a member of a namespace N1, it means that its fully qualified name is N1.T and thus other types need to refer to this type using fully qualified name N1.T( here I’m ignoring the using directives etc ).

I would expect it’s the same with Xml Schemas and xml instance documents - thus, as I understand it, when Xml Schema validator parsing Contacts.xml encounters qualified element contact:firstName, it searches Contacts.xsd for a matching element declaration using a name http://www.someDomain.com.firstName.

After it finds a match, validator then proceeds with the next element in Contacts.xml, which is lastName. Seeing that lastName element is unqualified, it searches Contacts.xsd for an element declaration named lastName. Now if within Contacts.xsd its fully qualified name was http://www.someDomain.com.lastName, then validator wouldn’t be able to find the matching declaration! But it does find a matching declaration, so I’m assuming lastName isn't a member of namespace http://www.someDomain.com

Contacts.xml

<?xml version="1.0" encoding="UTF-8"?>
<contact:contact      
     xmlns:contact="http://www.someDomain.com"
     xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
     xs:schemaLocation="http://www.someDomain.com contacts1.xsd"&g开发者_如何学运维t;
            <contact:firstName></contact:firstName>
            <lastName></lastName>         
</contact:contact>

Contacts.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.someDomain.com"  targetNamespace="http://www.someDomain.com" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="contact">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="firstName" />
                <xs:element name="lastName" form="unqualified"/>        
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>


It seems to me that your question is connected with the understanding of special namspace: "no namespace". In the following simple XML document

<?xml version="1.0" encoding="utf-8"?>
<root>
    <myElement>Bla Bla</myElement>
</root>

both root and myElement elements has the special namespace named as "no namespace". You can define the corresponding XML Schema:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="root">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="myElement" type="xs:string"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

Because in the schema it is not used targetNamespace, the schema defines the schema of the "no namespace".

By the way the schema "http://www.w3.org/2001/XMLSchema-instance" gives you an easy way to reference the schema from the XML document. Let us we save the above schema document in the file NoNamespace.xsd and the XML document we save in the file NoNamespace.xml. Moreover we place both files in the same folder. Then we can modify the XML document to the following

<?xml version="1.0" encoding="utf-8"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="NoNamespace.xsd">
    <myElement>Bla Bla</myElement>
</root>

Now we have an explicit reference to the schema which we use.

The question about qualified element names or qualified attribure names is independ from your main question. It is very easy to explain it. The qualified names is nothing more as the names with the namespace prefix like <xs:element> or xsi:noNamespaceSchemaLocation="NoNamespace.xsd". So qualified can be both elements and attributes.

In one document you can mix elements belonds to the different namespace. For example in the following document

<?xml version="1.0" encoding="utf-8"?>
<root>
    <html xmlns="http://www.w3.org/1999/xhtml">
        <body>
            <p>Hello!</p>
        </body>
    </html>
    <myElement>
        <myChild xmlns="http://www.ok-soft-gmbh.com/xsd/test/123">
            <mySubChild xmlns="">
                <x></x>
            </mySubChild>
            <mySecondChild>Hello!<mySecondChild>
        </myChild>
    </myElement>
</root>

the element <html> and its childs has "http://www.w3.org/1999/xhtml" namespace and there should be validated against the well-known XHTML schema. The elements <root> and <myElement> has "no namespece". The elements <myChild> and <mySecondChild> has the namespace "http://www.ok-soft-gmbh.com/xsd/test/123". The element <mySubChild> together with <x> element has also "no namespece" like <root> and <myElement>. So everything is very simple.

UPDATED: If I understand you correct all types which one define in the XML Schema will be "global" in your definition. You can not define an "internal" or "private" types.

I am not sure what is your question th the XML file and XSD Schema which you included in your example. Probably it will be helpful for you that one can rewrite the XML file using the same schema as following:

<?xml version="1.0" encoding="UTF-8"?>
<contact xmlns="http://www.someDomain.com"
         xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
         xs:schemaLocation="http://www.someDomain.com Contacts.xsd">
    <firstName></firstName>
    <lastName xmlns=""></lastName>
</contact>

So the element lastName is defined in the schema Contacts.xsd, but it is defined as an element from the "no namespace". You XML schema say that <contact> element from the "http://www.someDomain.com" must has two child elements: firstName from the targetNamespace "http://www.someDomain.com" and the element lastName from the "no namespace". I hope it will help you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜