Re: XML namespaces
- From: "Tomas Restrepo \(MVP\)" <tomasr@xxxxxxxx>
- Date: Thu, 21 Jul 2021 09:55:16 -0500
Hi Benjamin,
> Is it absolutely essential to qualify all elements in a document with the
> namespace prefix even when all the elements are from the same namespace?
>
> I have a XML file that looks approximately like
>
> <SupplierRecord xmlns="http://mycompany/schemas/supplier1";>
> <Header>.....</Header>
> <Body>....</Body>
> </Supplier Record>
>
> When i try to validate this against the schema, it throws errors for every
> single element saying that it couldnt locate the namespace. So i generated
> a
> instance from the schema and found that it creates a file that looks like
> this
>
> <ns0:SupplierRecord xmlns:ns0="http://mycompany/schemas/supplier1";>
> <Header>.....</Header>
> <Body>....</Body>
> </ns0:Supplier Record>
>
> When i manually put the ns0: into my original document , it validated
> without a complaint
>
> I cant understand it .. why would it matter if there is a prefix or not
> when
> all the elements are in the same schema?
Ahh, now I understand you. Your problem is indeed the
elementQualifiedDefault. See, you are right that all elements are defined in
the same schema. However, XSD has the concept of qualified and unqualified
elements, which means that qualified elements are defined to be in the
targetNamespace of the XSD, while unqualified elements are in the global
unnamed namespace ("").
By default, XSD uses elementQualifiedDefault="unqualified", which means that
global elements (such as the root element) are qualified always, while other
elements are unqualified. That's what leads to your second example document.
What you want is to create your XSD with
elementQualifiedDefault="qualified", which will ensure that all elements are
required to be qualified (that is, belong to the targetNamespace)
independent of where they are located. This would lead you to:
<ns0:SupplierRecord xmlns:ns0="http://mycompany/schemas/supplier1";>
<ns0:Header>.....</ns0:Header>
<ns0:Body>....</ns0:Body>
</ns0:Supplier Record>
which is equivalent xml-wise to:
<SupplierRecord xmlns="http://mycompany/schemas/supplier1";>
<Header>.....</Header>
<Body>....</Body>
</Supplier Record>
BTW, it doesn't matter that the prefix above is ns0 or MyPrefix; it's the
same as far as XML namespace is concerned as long as the prefix matches to
the correct namespace.
--
Tomas Restrepo
tomasr@xxxxxxxx
http://www.winterdom.com/
.
- Follow-Ups:
- Re: XML namespaces
- From: David Carroll
- Re: XML namespaces
- References:
- XML namespaces
- From: BizTalk Benjamin
- XML namespaces
- Prev by Date: Re: how to: unqualify root element with targetnamespace
- Next by Date: Re: Biztalk 2002 - MSMQ port destination
- Previous by thread: XML namespaces
- Next by thread: Re: XML namespaces
- Index(es):
Relevant Pages
|