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 namespace 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 "namespace" Watch "namespace" New topic
Author

namespace

Saritha Penumudi
Ranch Hand

Joined: Aug 18, 2003
Posts: 147
Can anyone help me in understanding what is namespace in xml schema documents.

I understand that namspace is used to group elements together so that same element names can be used in different namespaces. Please correct me if my understanding is wrong.

My problem is I am not sure how to define and use these namespaces.
Dose it has got something to do with location where my schema reside. How both are linked?.

Ur help would be greatly appreciated

Thank you
Saritha
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
You could think of XML namespaces as the equivalent of packages in Java.
A class in Java has a fully-qualified name like com.javaranch.foo.MyClass where com.javaranch.foo is the namespace (package) and MyClass is the element name (class). Similarly an XML Schema might specify a set of elements, including MyClass, in a namespace named com.javaranch.foo (although usually namespaces follow the URL or URI syntax, e.g. "http://javaranch.com/foo" or "urn://javaranch.com.foo").


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Originally posted by Saritha ventrapragada:
My problem is I am not sure how to define and use these namespaces.
Dose it has got something to do with location where my schema reside. How both are linked?.
You use namespaces by declaring in your XML document that a particular set of elements belong to a given namespace. That has nothing to do with where your XML Schema document resides (even if a namespace looks like a URL, there usually isn't anything behind that URL if you type the URL into a browser!).

Here's an example of an XML document that uses a namespace named "urn:javaranch-com-fruits":

The xmlns="..." part is called a namespace declaration. The above example is a special case where you specify a default namespace. It means that all elements within that containing element that don't have a namespace prefix belong to the given namespace.

The other type of namespace declaration looks like this:

Notice how I added ":f" into the namespace declaration? That syntax is used to tell the XML parser that all elements within this containing element that are prefixed with "f:" belong to this namespace.

I'd recommend googling for an online tutorial, reading a book on XML, or just going through w3schools.com to learn about XML namespaces. It's really quite easy to get started.
Saritha Penumudi
Ranch Hand

Joined: Aug 18, 2003
Posts: 147
Thank you,
I was just working on sample
Here is the .xsd file

[ October 28, 2004: Message edited by: Saritha ventrapragada ]
Saritha Penumudi
Ranch Hand

Joined: Aug 18, 2003
Posts: 147
xml file

<?xml version="1.0" encoding="iso-8859-1"?>
<Report xmlns="http://localhost/Report" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="Report.xsd">
<xsi:HeaderImage img="D:/projects/Safari/img/SAFARI_header.jpg" width="" height=""/>
<xsi:BR/>
<xsi:Title> Ticket </xsi:Title>
<xsi:SubTitle> Unmatched Ticket Report </xsi:SubTitle>

<xsi:Criteria>
<xsi:FieldInfo>
<xsi:FieldLabel>Ticket# </xsi:FieldLabel>
<xsi:FieldValue>123456</xsi:FieldValue>
</xsi:FieldInfo>
<xsi:FieldInfo>
<xsi:FieldLabel>Date </xsi:FieldLabel>
<xsi:FieldValue>12/04/2004</xsi:FieldValue>
</xsi:FieldInfo>
<xsi:FieldInfo>
<xsi:FieldLabel>Origin Gateway</xsi:FieldLabel>
<xsi:FieldValue>KSDF</xsi:FieldValue>
</xsi:FieldInfo>
<xsi:FieldInfo>
<xsi:FieldLabel>Destination Gateway</xsi:FieldLabel>
<xsi:FieldValue>CSDK</xsi:FieldValue>
</xsi:FieldInfo>
</xsi:Criteria>

<xsi:ReportHeader>
<xsi:Header>Ticket #</xsi:Header>
<xsi:Header>Date</xsi:Header>
<xsi:Header>Origin Gateway</xsi:Header>
<xsi:Header>Destination Gateway</xsi:Header>
<xsi:Header>Gallons Amount</xsi:Header>
</xsi:ReportHeader>

<xsi:ReportData hightlight="yes">
<xsi ata>123456</xsi ata>
<xsi ata>12/04/2004</xsi ata>
<xsi ata>KSDF</xsi ata>
<xsi ata>CSDK</xsi ata>
<xsi ata>10000</xsi ata>
</xsi:ReportData>

<xsi:ReportData hightlight="yes">
<xsi ata>123456</xsi ata>
<xsi ata>12/04/2004</xsi ata>
<xsi ata>KSDF</xsi ata>
<xsi ata>CSDK</xsi ata>
<xsi ata>15000</xsi ata>
</xsi:ReportData>

<xsi:FooterImage img="D:/projects/Safari/img/safari_footer.jpg" width="" height=""/>
</Report>
[ October 28, 2004: Message edited by: Saritha ventrapragada ]
Saritha Penumudi
Ranch Hand

Joined: Aug 18, 2003
Posts: 147
Errors I get when I validate


I wasn't sure where I was going wrong.
I googled for namespace but none of the link explained the correct declaration of the namespaces.

I would appreciate if you could help me in solving my problem.
May it might be small syntax mistake which I am not able to debug bcoz of the ignorance in the subject.

I will look at the link u have provided. Thank you
Saritha
[ October 28, 2004: Message edited by: Saritha ventrapragada ]
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
When you have this in your XML instance document...

...you're basically saying that there should be an element defined in namespace "http://www.w3.org/2001/XMLSchema-instance" called "HeaderImage". Instead, you should just use <HeaderImage> since you've specified that your "http://localhost/Report" namespace is the default namespace, i.e. all elements without a prefix belong to that namespace.
Saritha Penumudi
Ranch Hand

Joined: Aug 18, 2003
Posts: 147
I have corrected that mistake,
But still I am getting the following errors


My doubt is schemaLocation attribute. I guess validator look for the schema file using the namespace URI and schema URI that I give in schemaLocation attribute. Please correct me if I am wrong.

I am guessing that the errors are due to validator not being able to locate the schema file. As the validator I am using is on the web. I am uploading xml and xsd file into the this site

I guess that validator is looking for report.xsd file in localhost which it can never find.

Please let me know if my understanding is wrong.
I really appreciate all your patience with me and thank you very much for helping me

saritha
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
Originally posted by Saritha ventrapragada:

My doubt is schemaLocation attribute. I guess validator look for the schema file using the namespace URI and schema URI that I give in schemaLocation attribute. Please correct me if I am wrong.

I am guessing that the errors are due to validator not being able to locate the schema file. As the validator I am using is on the web. I am uploading xml and xsd file into the this site

I guess that validator is looking for report.xsd file in localhost which it can never find.

Please let me know if my understanding is wrong.
I really appreciate all your patience with me and thank you very much for helping me

saritha


Yes and no.....

See the schemaLocation needs to contain atleast two values. In this case, I would expect -



This will tell the processor that the namespace http://localhost/Report is related to the schema document Report.xsd. Then it does some comparision of the targetNamespace, if you have defined it in the schema document.
That is the first part.
The second part is that you should have a copy of the schema document Report.xsd in the same folder.

This is what I think.

- m

ps: Sorry but I haven't read the whole thread though.
Saritha Penumudi
Ranch Hand

Joined: Aug 18, 2003
Posts: 147
Thank you for your response.

I have two values for schemaTarget


I have specified targetNamespace as http://localhost/Report in the schema. Here is the code


I have both .xml and .xsd in the same directory. I mean in the same folder. But still I am getting those errors.
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
I have both .xml and .xsd in the same directory. I mean in the same folder. But still I am getting those errors.

Is the HeaderImage element deifined in the Report.xsd ?
Never mind, gimme a minute, I will look into your XSD and XML and get back....

- m
[ October 29, 2004: Message edited by: Madhav Lakkapragada ]
Saritha Penumudi
Ranch Hand

Joined: Aug 18, 2003
Posts: 147
Before that I would like to post the updated .xsd and .xml documents

I really appreciate your help.
Saritha Penumudi
Ranch Hand

Joined: Aug 18, 2003
Posts: 147
<?xml version="1.0" encoding="iso-8859-1"?>
<Report xmlns="http://localhost/Report" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://localhost/Report Report.xsd">
<HeaderImage img="D:/projects/Safari/img/SAFARI_header.jpg" width="800" height="60"/>
<BR/>
<Title> Ticket </Title>
<Subtitle> Unmatched Ticket Report </Subtitle>

<Criteria>
<FieldInfo>
<FieldLabel>Ticket# </FieldLabel>
<FieldValue>123456</FieldValue>
</FieldInfo>
<FieldInfo>
<FieldLabel>Date </FieldLabel>
<FieldValue>12/04/2004</FieldValue>
</FieldInfo>
<FieldInfo>
<FieldLabel>Origin Gateway</FieldLabel>
<FieldValue>KSDF</FieldValue>
</FieldInfo>
<FieldInfo>
<FieldLabel>Destination Gateway</FieldLabel>
<FieldValue>CSDK</FieldValue>
</FieldInfo>
</Criteria>

<ReportHeader>
<Header>Ticket #</Header>
<Header>Date</Header>
<Header>Origin Gateway</Header>
<Header>Destination Gateway</Header>
<Header>Gallons Amount</Header>
</ReportHeader>

<ReportData hightlight="yes">
<Data>123456</Data>
<Data>12/04/2004</Data>
<Data>KSDF</Data>
<Data>CSDK</Data>
<Data>10000</Data>
</ReportData>

<ReportData hightlight="yes">
<Data>123456</Data>
<Data>12/04/2004</Data>
<Data>KSDF</Data>
<Data>CSDK</Data>
<Data>15000</Data>
</ReportData>

<FooterImage img="D:/projects/Safari/img/safari_footer.jpg" width="800" height="20"/>
</Report>
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
I have a solution. The targetNamespace defined in the schema file
applies to all the globab declarations (elements, attributes,
types, groups).

You have a choice on local elements to be qualified or unqualified.
Examples of local elements, in your case are, HeaderImage, BR, Title,
Subtitle etc that are defined inside the complexType.

If you leave these declarations as it is in the schema file, then
you must ONLY qualify the root element Report. To do this, you must
not use a default namespace. You must prefix it.

The following XML file shows a valid file -


Changes I made:

1. Added a prefix 'rep' to the namespace, thus removing the
default namespace.
2. Qualified the root element Report to be in the rep namespace.
3. Removed an invalid attribute 'highlight' on the ReportData element.

Hope this helps.

- m
[ October 29, 2004: Message edited by: Madhav Lakkapragada ]
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
Since you were not using a prefix, all the elements were being mapped
to the default namespace and hence, they were invalid as they aren't
defined (globally) in that namespace.

If you want these local elements to be in the same namespace,
then you should split the schema file into two. Define all the
local elements (those that are defined in the complextype) in
one schema file. Attach a targetNamespace in this file, say tgt1.
Define another schema file for the global element with a different
targetNamespace, say tgt2.
Now you can include the first schema file (tgt1)in the definition of the
schema file for the global element (tgt2). Additionally, you must also
set the elementFormDefault to qualified in both the files.

If you choose to do this, then you can qualify the elements appropriately.
This is ONE way to do it.
Thanks.

- m......................nice learning for me!
Saritha Penumudi
Ranch Hand

Joined: Aug 18, 2003
Posts: 147
Hello Madhav,

I guess I am missing something that is very important.

I have read this link about schema but I guess this does not explain local and global elements declaration w.r.t namespace declaration.

I would greatly appreciate if you could provide me some links which explains the whole architecture.

I have declared 'hightlight' as an attribute to "ReportData"


I am not sure why I have to remove highlight attribute from ReportData element.

Thank you
Saritha
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040

I guess I am missing something that is very important.

I have read this link about schema but I guess this does
not explain local and global elements declaration w.r.t
namespace declaration.


What you are missing here is the elementFormDefault attribute in the
schema definition. In the link that you pointed out, you see that the
value of this attribute is set to qualified. In your schema document,
you didn't specify this attribute. Hence, this attribute takes the default
value, which is unqualified. Because of that, the local elements
are not in the targetNamespace. In your schema document if you add this
attribute like in the namespace_song.xsd file in your link, then what you
did will be valid.


I have declared 'hightlight' as an attribute to "ReportData"
....
I am not sure why I have to remove highlight attribute from ReportData
element.


Yes, you did. But the value was wrong. Since you declared it as boolean,
it should have a value either true or false. A yes/no value
will be invalid. So, in the XML file I posted, if you change the value to
true instead of yes then its still valid.

I will get back to you on the links for global/local stuff.
Most of what I posted is what I read from Definitive XML Schema book
that I am currently reviewing. So far a good book, I thought.

Later....

- m
[ October 29, 2004: Message edited by: Madhav Lakkapragada ]
Saritha Penumudi
Ranch Hand

Joined: Aug 18, 2003
Posts: 147
I aplogize for late reply. I was tied up with some other work.

Yes your right, I have intentionally did not include that element in first place bcoz I wasn't sure what that element does. I have gone through XML best practices, Hide vs Expose document which says that, when elementFormDefault=unqualified, then all the elements are localized and there is will be no requirement to explicitly qualify them, hence I thought I am good with not declaring that element. when elementFromDefault=qualified then the namespaces are exposed.

Why do the local elements do not come under targetnamespace when their parent element is?

Even when i gave value of highlight="true" than "yes" i was getting same error.

I would really appreciate if you could suggest me some good URL's to understand all these concepts correctly.

Thank you very much for everything
Saritha
[ November 04, 2004: Message edited by: Saritha ventrapragada ]
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
Originally posted by Saritha ventrapragada:
when elementFormDefault=unqualified, then all the elements are localized and there is will be no requirement to explicitly qualify them, hence I thought I am good with not declaring that element.


Not really. While your understanding is correct, what you missed here
is the concept of default namespace. When you don't qualify an element
with a prefix, it is considered to be in the default namespace. Hence,
based on your original code you had

Report xmlns="http://localhost/Report"

This becomes your default namespace. Hence, all unqualified elements
are assumed to belong to this namespace. But based on the way you defined
the schema and the targetNamespace, they are not part of the namespace.
Hence the problems.


Why do the local elements do not come under targetnamespace when their parent element is?

That's by definition of the XML Schema recommentations.


Even when i gave value of highlight="yes" than "true" i was getting same error.

The errors were because of the namespace. Once, the element was properly
defined, then it looked at the definition of the attributes for the
element in that namespace and so the attribute highlight was then
validated to be wrong according to the definition given in the schema.


I would really appreciate if you could suggest me some good URL's to understand all these concepts correctly.


I think you need to understand two concepts really to solve this.

1. Namespaces in XML.
2. Namespaces in XML Schema. Not sure how useful this link is.

Another article in Namespaces.
There was another link on namespace Faq, can't locate it at the moment.
Also, Definitive XML Schema by Priscilla Walmsley, book talk a lot about this topic.
Thanks.

- m
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
Namespaces FAQ link!

- m.............google does wonderful things.
Saritha Penumudi
Ranch Hand

Joined: Aug 18, 2003
Posts: 147
Thank you very much

All your help is really appreciated. i will go through the links you have provided.

Thank you
Saritha
Saritha Penumudi
Ranch Hand

Joined: Aug 18, 2003
Posts: 147
Hi,

I guess there is lots of documentation on w3c recommendations and it might take some time to digest all the rules.

After I make the following changes to the schema




What should be the declarations of namespace for both xml file and schema so that I don't get errors.
It seems, there is no namespace issues with attributes define for ImageType, but there are namespace issues with attribute defined for "ReportDataType".

I would appreciate if you help me in solving this problem.

Thank you
Saritha
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040

What should be the declarations of namespace for both xml file and schema
so that I don't get errors.


Are you saying you are still getting errors?

XML File: You can declare the default namespace to be http://localhost/Reports like you already did.

Schema File: Same declaration with the targetNamespace and the
elementFormDefault like you have. This doesn't give any errors.
See my posts above for the XML file.



It seems, there is no namespace issues with attributes define for
ImageType, but there are namespace issues with attribute defined
for "ReportDataType".


I am a little confused about what you are trying to say here...
IMO, both elements' attributes are not in any namespace.
or Am I missing something here ?


- m
Saritha Penumudi
Ranch Hand

Joined: Aug 18, 2003
Posts: 147
When I try to validate XML against Schema,
I am getting errors for 'highlight' attribute in ReportDataType.


Error at (38,14): The 'hightlight' attribute is not declared. An error occurred at , (38, 14).
<ReportData hightlight = "true">
<Data>123456</Data>
<Data>12/04/2004</Data>
<Data>KSDF</Data>
<Data>CSDK</Data>
<Data>10000</Data>
</ReportData>



Schema declaration
<?xml version="1.0" encoding="iso-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://localhost/Report"
targetNamespace="http://localhost/Report"
elementFormDefault="qualified"
>
<xs:element name="Report" type="ReportType" />

<xs:complexType name="ImageType">
<xs:attribute name="img" type="xs:anyURI" use="required"/>
<xs:attribute name="width" type="xs :p ositiveInteger" use="required"/>
<xs:attribute name="height" type="xs :p ositiveInteger" use="required"/>
<xs:attribute name="alt" type="xs:string"/>
</xs:complexType>

<xs:complexType name="FieldInfoType">
<xs:sequence>
<xs:element name="FieldLabel" type="xs:string" />
<xs:element name="FieldValue" type="xs:string" />
</xs:sequence>
</xs:complexType>

<xs:complexType name="CriteriaType">
<xs:sequence>
<xs:element name="FieldInfo" type="FieldInfoType" minOccurs="1" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>

<xs:complexType name="ReportHeaderType">
<xs:sequence>
<xs:element name="Header" type="xs:string" minOccurs="1" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>

<xs:complexType name="ReportDataType">
<xs:sequence>
<xs:element name="Data" type="xs:string" minOccurs="1" maxOccurs="unbounded" />
</xs:sequence>
<xs:attribute name="highlight" type="xs:boolean" />
</xs:complexType>

<xs:complexType name="ReportTotalsType">
<xs:sequence>
<xs:element name="FieldInfo" type="FieldInfoType" minOccurs="1" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>

<xs:complexType name="EmptyType">
</xs:complexType>

<xs:complexType name="ReportType">
<xs:sequence>
<xs:element name="HeaderImage" type="ImageType" minOccurs="0" maxOccurs="1"/>
<xs:element name="BR" type="EmptyType" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="Title" type="xs:string" minOccurs="0" maxOccurs="1"/>
<xs:element name="Subtitle" type="xs:string" minOccurs="0" maxOccurs="1"/>
<xs:element name="Criteria" type="CriteriaType"/>
<xs:element name="ReportHeader" type="ReportHeaderType"/>
<xs:element name="ReportData" type="ReportDataType" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="ReportTotals" type="ReportTotalsType" minOccurs="0" maxOccurs="1" />
<xs:element name="FooterImage" type="ImageType" minOccurs="0" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:schema>


I am sorry if my questions does not make any sense. In the schema declaration, attributes declared for ImageType are being recognized by the validator, but I am getting above error for Hightlight attribute.

Thanks
Saritha
[ November 05, 2004: Message edited by: Saritha ventrapragada ]
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
Sure, Why not?
Thats a valid error. You made a mistake.


Error at (38,14): The 'hightlight' attribute is not declared. An error occurred at , (38, 14).
<ReportData hightlight = "true">


Its one of those great Duh!!! moments for you.
Everyone has them, now its your turn.
I will give you some time to tell us what it is.

- m...............read out each letter loudly!
mat hues
Greenhorn

Joined: Sep 15, 2005
Posts: 1
I know this is an old post, but the problem is a typo:
hightlight - notice the t in between high and light?

Also, here's a Q&A between myself and Madhav (thx Madhav!):
I have a similar problem � I'm using VS.Net to validate an xml schema that references another xml schema � all on my localhost (using IIS). Basically, MyXmlSchema defined the elements of my schema, and references the complex and simple types I've defined in TypeDefinitions.xsd. I'd like to validate this using localhost, but I get an error:

Namespace ' http://localhost/TypeDefinitions.xsd' is not available to be referenced in this schema.

<xs:schema id ="MyXmlSchema"
targetNamespace ="http://localhost/MyXmlSchema.xsd"
xmlns ="http://localhost/MyXmlSchema.xsd"
xmlns :td ="http://localhost/TypeDefinitions"
td :schemaLocation ="C:\Inetpub\wwwroot\TypeDefinitions.xsd"
xmlns :xs ="http://www.w3.org/2001/XMLSchema"
attributeFormDefault ="qualified"
elementFormDefault ="qualified">

<xs:schema id="TypeDefinitions"
targetNamespace ="http://localhost/TypeDefinitions.xsd"
elementFormDefault ="qualified"
xmlns ="http://localhost/TypeDefinitions.xsd"
xmlns :xs ="http://www.w3.org/2001/XMLSchema">

Matthew,

As the error message says, 'http://localhost/TypeDefinitions.xsd' is NOT a namespace. Your namespace should be a URI, something like http://localhost/TypeDefinitions. You should then map this URI to a schema document using the
"schemalocation" attribute. So, I would try to change this to be:

<xs:schema id ="MyXmlSchema"
targetNamespace="http://localhost/MyXmlSchema"
xmlns="http://localhost/MyXmlSchema"
xmlns:td="http://localhost/TypeDefinitions"
schemaLocation="http://localhost/TypeDefinitions
C:\Inetpub\wwwroot\TypeDefinitions.xsd"
xmlns: xs="http://www.w3.org/2001/XMLSchema"
attributeFormDefault="qualified"
elementFormDefault="qualified">

I am not sure on how the schemaLocation is resolved in IIS, so if the above one does not work, then I would try
schemaLocation=" http://localhost/TypeDefinitions TypeDefinitions.xsd"

Madhav


mat_hues
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: namespace