File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes Condition on attributes : Can an element has same attribute from two different namespaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Condition on attributes : Can an element has same attribute from two different namespaces " Watch "Condition on attributes : Can an element has same attribute from two different namespaces " New topic
Author

Condition on attributes : Can an element has same attribute from two different namespaces

Rajani Gummadi
Ranch Hand

Joined: Dec 17, 2010
Posts: 48
Hello All,

I read in the spec that, attributes can not be repeated in an element declaration. Based on this, I have few questions.

1) Can an element has same attribute taken from different namespace.

tns and xs belong are prefixes of two different namespaces


tns, xs are prefixes of same namepace


First attribute belongs to default namespace and xs is the prefix of the same namespace as that of default


I know that first example is a valid one, second is invalid, but can not understand the third one. Can some body throw some light on this ?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18650
    
    8

You seem to have fallen into the common error of believing that the prefix is the important part of the namespace. It isn't. The namespace URI is what defines the namespace, and the prefix is just a meaningless string for the convenience of XML authors.

In the first example you have a "{http://www.example1.org/xml/sample}category" attribute and then you have a "{http://www.example2.org/xml/sample}category" attribute. These have different names.

In the second example you have two "{http://www.example.org/xml/sample}category" attributes. They have the same name.

And in the third example you also have two "{http://www.example.org/xml/sample}category" attributes, which have the same name.
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
<myname:products
xmlns:myname="Jimmy"
xmlns:yourname="Rajani"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.org/xml/sample Sample.xsd ">

<myname:product

myname:category="books"
yourname:category="printed materials">

</myname:product>

</myname:products>


This example shows two different category attributes, each belongs to a different namespace. An XML parser will read this as:


<Jimmy:products
xmlns:myname="Jimmy"
xmlns:yourname="Rajani"
xmlns:xsi="XMLSchema-instance"

XMLSchema-instance:schemaLocation="http://www.example.org/xml/sample Sample.xsd ">

<Jimmy:product

Jimmy:category="books"
Rajani:category="printed materials">

</Jimmy:product>

</Jimmy:products>



g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 535
    
    3
>I know that first example is a valid one, second is invalid,...
To be more precise, the first example is well-formed, the second is not. Despite the apparent of having some xsd schema referred in the root, it has nothing to do with the validation which has never begun to begin.

ps: As to the second figurative explanation starting with xmlns:yourname etc... and ending with Rajani:category etc..., on the face of it reverting namespace uri to namespace prefix: I can see what is intended to convery, but, it risks to distort the issue with more confusion. But, again, I see what is intended to convey and I have no problem with the hidden message.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Condition on attributes : Can an element has same attribute from two different namespaces