Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » ServerTools (WTP) » XML, Schema and namespaces... :-(
XML, Schema and namespaces... :-( [message #182778] Wed, 15 November 2006 16:51 Go to next message
Michael Moser is currently offline Michael MoserFriend
Messages: 914
Registered: July 2009
Senior Member
(If there is a more appropriate forum to ask this, please advise!)

I am desperately trying to understand XML namespace handling and the
schema locating policies.

I boiled things down to a very simple example - originally taken from
the "understanding web services"-tutorial
( http://www-128.ibm.com/developerworks/edu/ws-dw-ws-understan d-web-services2.html)
but here the focus is now on the handling and locating of a schema:

To reproduce put the following three files all into the same working
directory:

File "ValidateWithSchema.java"
-------------------------
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

public class ValidateWithSchema
{
public static void main(String args[]) {
DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
dbf.setValidating(true);
dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
"http://www.w3.org/2001/XMLSchema");
//
dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource",
// "classifieds.xsd");
try {
DocumentBuilder parser = dbf.newDocumentBuilder();
/*(Document doc =*/ parser.parse("classifieds.xml");
} catch (Exception e) {
e.printStackTrace();
}
}
}
-------------------------
The above should validate the below .xml file. Note, that the line that
sets the schema location is commented. The point of this exercise it
that I am trying to specify that location within the .xml file itself
using the xsi:schemaLocation-attribute:

File: classifieds.xml
-------------------------
<?xml version="1.0" encoding="UTF-8" ?>
<ClassfiedList
xmlns="http://www.example.org/classifieds"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.org/classifieds
classifieds.xsd ">
<ClassifiedAd adId="1138">
<content>Vintage 1963 T-Bird. Less than 300 miles. </content>
<endDate>4/15/2007</endDate>
<startDate>4/1/2007</startDate>
</ClassifiedAd>
<ClassifiedAd adId="2187">
<content>30 ft ladder, only used once. $150 OBO.</content>
<endDate>4/30/2007</endDate>
<startDate>4/10/2007</startDate>
</ClassifiedAd>
</ClassfiedList>
-------------------------

Finally the schema for the above .xml file - file: classifieds.xsd:
-------------------------
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/classifieds"
xmlns:tns="http://www.example.org/classifieds">
<xsd:complexType name="ClassifiedListType">
<xsd:sequence>
<xsd:element
name="ClassifiedAd"
type="tns:ClassifiedAdType" minOccurs="0"
maxOccurs="unbounded">
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="ClassifiedAdType">
<xsd:sequence>
<xsd:element
name="content"
type="xsd:string">
</xsd:element>
<xsd:element
name="endDate"
type="xsd:string">
</xsd:element>
<xsd:element
name="startDate"
type="xsd:string">
</xsd:element>
</xsd:sequence>
<xsd:attribute
name="adId"
type="xsd:int">
</xsd:attribute>
</xsd:complexType>
<xsd:element
name="ClassfiedList"
type="tns:ClassifiedListType">
</xsd:element>
<xsd:element
name="ClassifiedAd"
type="tns:ClassifiedAdType">
</xsd:element>
</xsd:schema>
-------------------------

If I compile the .java file and let the compile class run I always get
the following error message (there also is a warning re. missing error
handler, but that's irrelevant here):
------------
Error: URI=file:///C:/tutorials/ValidateWithSchema/classifieds.xml
Line=5: cvc-elt.1: Cannot find the declaration of element
'ClassfiedList'.
------------

I believe I am doing everything right here. I tried back and forth with
misc. approaches re. the name spaces, different parser (Xerces and
Sun's) everytime with the same result). I tried to specify the location
of the schema file fully qualified, relative or in the short form as
shown, but again no difference. The schema file is found and read, btw.
If one inserts some syntax error, the parser will yell. So it actually
finds and reads that file but why then is that 'ClassfiedList'-element
declaration not found???

Any help, hint or advice highly appreciated!

Michael
Re: XML, Schema and namespaces... :-( [message #182788 is a reply to message #182778] Wed, 15 November 2006 18:10 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: merks.ca.ibm.com

Michael,

You are sure you aren't getting this error about ClassifiedAd instead?
After all, you don't have a ClassifiedList at line 5. The instance
below isn't valid with respect to the schema because all your elements
have namespace "http://www.example.org/classifieds" (because of the
xmlns="http://www.example.org/classifieds" declaration) and yet your
schema has many local element declarations that will only match elements
will null namespace. If you add elementFormDefault="qualified" to your
schema, then the instance would be valid. Did you intent for
ClassifiedListType to reference the ClassifiedAd element? It would
probably nicer to use lower case element and attribute names...


Michael Moser wrote:
> (If there is a more appropriate forum to ask this, please advise!)
>
> I am desperately trying to understand XML namespace handling and the
> schema locating policies.
>
> I boiled things down to a very simple example - originally taken from
> the "understanding web services"-tutorial
> ( http://www-128.ibm.com/developerworks/edu/ws-dw-ws-understan d-web-services2.html)
> but here the focus is now on the handling and locating of a schema:
>
> To reproduce put the following three files all into the same working
> directory:
>
> File "ValidateWithSchema.java"
> -------------------------
> import javax.xml.parsers.DocumentBuilder;
> import javax.xml.parsers.DocumentBuilderFactory;
>
> public class ValidateWithSchema
> {
> public static void main(String args[]) {
> DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
> dbf.setValidating(true);
>
> dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
>
> "http://www.w3.org/2001/XMLSchema");
> //
> dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource",
> // "classifieds.xsd");
> try {
> DocumentBuilder parser = dbf.newDocumentBuilder();
> /*(Document doc =*/ parser.parse("classifieds.xml");
> } catch (Exception e) {
> e.printStackTrace();
> }
> }
> }
> -------------------------
> The above should validate the below .xml file. Note, that the line
> that sets the schema location is commented. The point of this exercise
> it that I am trying to specify that location within the .xml file
> itself using the xsi:schemaLocation-attribute:
>
> File: classifieds.xml
> -------------------------
> <?xml version="1.0" encoding="UTF-8" ?>
> <ClassfiedList
> xmlns="http://www.example.org/classifieds"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xsi:schemaLocation="http://www.example.org/classifieds
> classifieds.xsd ">
> <ClassifiedAd adId="1138">
> <content>Vintage 1963 T-Bird. Less than 300 miles. </content>
> <endDate>4/15/2007</endDate>
> <startDate>4/1/2007</startDate>
> </ClassifiedAd>
> <ClassifiedAd adId="2187">
> <content>30 ft ladder, only used once. $150 OBO.</content>
> <endDate>4/30/2007</endDate>
> <startDate>4/10/2007</startDate>
> </ClassifiedAd>
> </ClassfiedList>
> -------------------------
>
> Finally the schema for the above .xml file - file: classifieds.xsd:
> -------------------------
> <?xml version="1.0" encoding="UTF-8"?>
> <xsd:schema
> xmlns:xsd="http://www.w3.org/2001/XMLSchema"
> targetNamespace="http://www.example.org/classifieds"
> xmlns:tns="http://www.example.org/classifieds">
> <xsd:complexType name="ClassifiedListType">
> <xsd:sequence>
> <xsd:element
> name="ClassifiedAd"
> type="tns:ClassifiedAdType" minOccurs="0"
> maxOccurs="unbounded">
> </xsd:element>
> </xsd:sequence>
> </xsd:complexType>
> <xsd:complexType name="ClassifiedAdType">
> <xsd:sequence>
> <xsd:element
> name="content"
> type="xsd:string">
> </xsd:element>
> <xsd:element
> name="endDate"
> type="xsd:string">
> </xsd:element>
> <xsd:element
> name="startDate"
> type="xsd:string">
> </xsd:element>
> </xsd:sequence>
> <xsd:attribute
> name="adId"
> type="xsd:int">
> </xsd:attribute>
> </xsd:complexType>
> <xsd:element
> name="ClassfiedList"
> type="tns:ClassifiedListType">
> </xsd:element>
> <xsd:element
> name="ClassifiedAd"
> type="tns:ClassifiedAdType">
> </xsd:element>
> </xsd:schema>
> -------------------------
>
> If I compile the .java file and let the compile class run I always get
> the following error message (there also is a warning re. missing error
> handler, but that's irrelevant here):
> ------------
> Error: URI=file:///C:/tutorials/ValidateWithSchema/classifieds.xml
> Line=5: cvc-elt.1: Cannot find the declaration of element
> 'ClassfiedList'.
> ------------
>
> I believe I am doing everything right here. I tried back and forth
> with misc. approaches re. the name spaces, different parser (Xerces
> and Sun's) everytime with the same result). I tried to specify the
> location of the schema file fully qualified, relative or in the short
> form as shown, but again no difference. The schema file is found and
> read, btw. If one inserts some syntax error, the parser will yell. So
> it actually finds and reads that file but why then is that
> 'ClassfiedList'-element declaration not found???
>
> Any help, hint or advice highly appreciated!
>
> Michael
>
Re: XML, Schema and namespaces... :-( [message #182806 is a reply to message #182788] Wed, 15 November 2006 20:17 Go to previous messageGo to next message
Michael Moser is currently offline Michael MoserFriend
Messages: 914
Registered: July 2009
Senior Member
Hi Ed,

"Ed Merks" <merks@ca.ibm.com> wrote in message
news:ejfl5v$lec$1@utils.eclipse.org...
> Michael,
>
> You are sure you aren't getting this error about ClassifiedAd instead?
> After all, you don't have a ClassifiedList at line 5. ...

Yes - I am sure I get it on the ClassifiedList element. Even if I
comment or even remove the entire body, i.e.:
-----------------
<?xml version="1.0" encoding="UTF-8" ?>
<ClassfiedList
xmlns="http://www.example.org/classifieds"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.org/classifieds
classifieds.xsd ">
<!--
...
-->
</ClassfiedList>
-----------------
I still get an error "Cannot find the declaration of element
'ClassfiedList'". The error location is the closing '>' of the
root-element (I actually have a version running here that sets an error
handler for the parser and prints out the exact error location - I only
removed that code in the NG append for brevity). Looking at the line
breaks in my append I realized that my newsreader also interfered here
and wrapped the 'classifieds.xsd ">'.

> ... The instance below isn't valid with respect to the schema because
> all your elements have namespace "http://www.example.org/classifieds"
> (because of the xmlns="http://www.example.org/classifieds"
> declaration) and yet your schema has many local element declarations
> that will only match elements will null namespace. If you add
> elementFormDefault="qualified" to your schema, then the instance would
> be valid.

I tried that (see the new schema below) but it still produces the same
error.

> ... Did you intent for ClassifiedListType to reference the
> ClassifiedAd element? It would probably nicer to use lower case
> element and attribute names...

I agree; the capitalization is not exactly my preferred style, either,
but I sticked to the example.
I had only experimented defining "globally" visible elements for all
types, since I recalled that this once was a problem in another project.

But even if I somewhat "beautify" and shorten the schema back to:
-----------------
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://www.example.org/classifieds"
targetNamespace="http://www.example.org/classifieds"
elementFormDefault="qualified" >

<xsd:element name="ClassfiedList">
<xsd:complexType>
<xsd:sequence>
<xsd:element
name="ClassifiedAd"
type="tns:ClassifiedAdType"
minOccurs="0" <!-- allows to comment the body -->
maxOccurs="unbounded" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>

<xsd:complexType name="ClassifiedAdType">
<xsd:sequence>
<xsd:element name="content" type="xsd:string" />
<xsd:element name="endDate" type="xsd:string" />
<xsd:element name="startDate" type="xsd:string" />
</xsd:sequence>
<xsd:attribute name="adId" type="xsd:int" />
</xsd:complexType>
</xsd:schema>
-----------------
I still get see the same error...

Michael

BTW: Thank for your continous support. I really appreciate it!
Re: XML, Schema and namespaces... :-( [message #182834 is a reply to message #182806] Wed, 15 November 2006 20:59 Go to previous messageGo to next message
Larry Isaacs is currently offline Larry IsaacsFriend
Messages: 1354
Registered: July 2009
Senior Member
I gave this a quick try. In addition to adding
elementFormDefault="qualified" to your schema as Ed suggested, adding
"dbf.setNamespaceAware(true);" to ValidateWithSchema gets it working for me.

Cheers,
Larry


Michael Moser wrote:
> Hi Ed,
>
> "Ed Merks" <merks@ca.ibm.com> wrote in message
> news:ejfl5v$lec$1@utils.eclipse.org...
>> Michael,
>>
>> You are sure you aren't getting this error about ClassifiedAd instead?
>> After all, you don't have a ClassifiedList at line 5. ...
>
> Yes - I am sure I get it on the ClassifiedList element. Even if I
> comment or even remove the entire body, i.e.:
> -----------------
> <?xml version="1.0" encoding="UTF-8" ?>
> <ClassfiedList
> xmlns="http://www.example.org/classifieds"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xsi:schemaLocation="http://www.example.org/classifieds
> classifieds.xsd ">
> <!-- ...
> -->
> </ClassfiedList>
> -----------------
> I still get an error "Cannot find the declaration of element
> 'ClassfiedList'". The error location is the closing '>' of the
> root-element (I actually have a version running here that sets an error
> handler for the parser and prints out the exact error location - I only
> removed that code in the NG append for brevity). Looking at the line
> breaks in my append I realized that my newsreader also interfered here
> and wrapped the 'classifieds.xsd ">'.
>
>> ... The instance below isn't valid with respect to the schema because
>> all your elements have namespace "http://www.example.org/classifieds"
>> (because of the xmlns="http://www.example.org/classifieds"
>> declaration) and yet your schema has many local element declarations
>> that will only match elements will null namespace. If you add
>> elementFormDefault="qualified" to your schema, then the instance would
>> be valid.
>
> I tried that (see the new schema below) but it still produces the same
> error.
>
>> ... Did you intent for ClassifiedListType to reference the
>> ClassifiedAd element? It would probably nicer to use lower case
>> element and attribute names...
>
> I agree; the capitalization is not exactly my preferred style, either,
> but I sticked to the example.
> I had only experimented defining "globally" visible elements for all
> types, since I recalled that this once was a problem in another project.
>
> But even if I somewhat "beautify" and shorten the schema back to:
> -----------------
> <?xml version="1.0" encoding="UTF-8"?>
> <xsd:schema
> xmlns:xsd="http://www.w3.org/2001/XMLSchema"
> xmlns:tns="http://www.example.org/classifieds"
> targetNamespace="http://www.example.org/classifieds"
> elementFormDefault="qualified" >
>
> <xsd:element name="ClassfiedList">
> <xsd:complexType>
> <xsd:sequence>
> <xsd:element
> name="ClassifiedAd"
> type="tns:ClassifiedAdType"
> minOccurs="0" <!-- allows to comment the body -->
> maxOccurs="unbounded" />
> </xsd:sequence>
> </xsd:complexType>
> </xsd:element>
>
> <xsd:complexType name="ClassifiedAdType">
> <xsd:sequence>
> <xsd:element name="content" type="xsd:string" />
> <xsd:element name="endDate" type="xsd:string" />
> <xsd:element name="startDate" type="xsd:string" />
> </xsd:sequence>
> <xsd:attribute name="adId" type="xsd:int" />
> </xsd:complexType>
> </xsd:schema>
> -----------------
> I still get see the same error...
>
> Michael
>
> BTW: Thank for your continous support. I really appreciate it!
>
Re: XML, Schema and namespaces... :-( [message #182842 is a reply to message #182834] Wed, 15 November 2006 21:13 Go to previous message
Michael Moser is currently offline Michael MoserFriend
Messages: 914
Registered: July 2009
Senior Member
Finally! (deep sigh) You are my hero! I owe you more than just a beer!
You saved my sleep tonight! Have a hug and a couple of pats on the back!
;-)

Michael

"Larry Isaacs" <Larry.Isaacs@sas.com> wrote in message
news:ejfv3e$6me$1@utils.eclipse.org...
>I gave this a quick try. In addition to adding
>elementFormDefault="qualified" to your schema as Ed suggested, adding
>"dbf.setNamespaceAware(true);" to ValidateWithSchema gets it working
>for me.
>
> Cheers,
> Larry
Previous Topic:Web tools - cannot update eclipse via update manager - File not found
Next Topic:XHTML editor and Facelets
Goto Forum:
  


Current Time: Fri Apr 19 13:17:35 GMT 2024

Powered by FUDForum. Page generated in 0.02640 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top