aspose file tools*
The moose likes XML and Related Technologies and the fly likes validation of XML w/ XSD... almost there... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "validation of XML w/ XSD... almost there..." Watch "validation of XML w/ XSD... almost there..." New topic
Author

validation of XML w/ XSD... almost there...

Dave Seng
Greenhorn

Joined: Sep 29, 2003
Posts: 5
Well, I've almost done it. I've been trying to figure how to make the parser use my specific XSD, no matter if a particular xml document has specified where the XSD should be... If someone could provide some insight I would be deeply in debt to you...
Here's my snippet of code:
...
SAXParserFactory spfactory = SAXParserFactory.newInstance();
spfactory.setNamespaceAware(true);
spfactory.setValidating(true);

SAXParser saxParser = spfactory.newSAXParser();
xmlReader = saxParser.getXMLReader();
xmlReader.setFeature("http://apache.org/xml/features/validation/schema", true);
xmlReader.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation", (new File(this.xsd)).getAbsolutePath());
xmlReader.setContentHandler(handler);
xmlReader.setErrorHandler(handler);
xmlReader.parse(new InputSource(new StringReader(this.xml)));
...
If I comment out the line: xmlReader.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation", (new File(this.xsd)).getAbsolutePath()); everthing works fine and dandy, otherwise I get this nifty error:
org.xml.sax.SAXParseException: TargetNamespace.2: Expecting no namespace, but the schema document has a target namespace.
at org.apache.xerces.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1172)
at com.whateverittakes.templates.Template.validate(Template.java:96)
at com.whateverittakes.templates.Test.main(Test.java:42)
I've searched google for this particular error and I get 4 results, none which provide any useful information whatsoever.
One link I found suggests that by inserting that line of code, that I should be able to specify what xsd to use not matter what is specified in the schema location hints.
I'm developing with WSAD 5.1 and I'm just try to execute this code as a sample java application. I had the WSAD generate the XSD and the XML for me and I must admit, I'm no pro when it comes to XML. Here's the XSD WSAD generated for me:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema
targetNamespace="http://www.whateverittakes.com/TEMPLATE/ABC"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:Q1="xs" xmlns:AFITESTTEMPLATE="http://www.blah.com/TEMPLATE/ABC">
<xsd:element name="order" type="ABCTESTTEMPLATE rder"/>
<xsd:complexType name="order">
<xsd:sequence>
<xsd:element name="renter" type="xsd:boolean" minOccurs="0" maxOccurs="1" />
<xsd:element name="childage" type="xsd:string" minOccurs="0" maxOccurs="1" />
<xsd:element name="income" type="xsd:string" minOccurs="0" maxOccurs="1" />
<xsd:element name="language" type="xsd:string" minOccurs="0" maxOccurs="1" />
<xsd:element name="homevalue" type="xsd:string" minOccurs="0" maxOccurs="1" />
<xsd:element name="polkapp" type="xsd:string" minOccurs="0" maxOccurs="1" />
<xsd:element name="carrierroute" type="xsd:string" minOccurs="0" maxOccurs="1" />
<xsd:element name="phoneonly" type="xsd:boolean" minOccurs="0" maxOccurs="1" />
<xsd:element name="zipcodes" type="xsd:string" minOccurs="1" maxOccurs="1" />
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
and here's a sample XML doc:
<?xml version="1.0" encoding="UTF-8"?>
<ABCTESTTEMPLATE rder
xmlns:ABCTESTTEMPLATE="http://www.blah.com/TEMPLATE/ABC"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.blah.com/TEMPLATE/ABC Order.xsd ">
<renter>true</renter>
<childage>44</childage>
<income>33000</income>
<language>english</language>
<homevalue>150000</homevalue>
<polkapp>ok</polkapp>
<carrierroute>ok</carrierroute>
<phoneonly>true</phoneonly>
<zipcodes>61704</zipcodes>
</ABCTESTTEMPLATE rder>
Again, any insight would be much appreciated. Thanks, Dave
Dave Seng
Greenhorn

Joined: Sep 29, 2003
Posts: 5
Made a type-o in the xsd... this line:
<xsd:schema
targetNamespace="http://www.whateverittakes.com/TEMPLATE/ABC"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:Q1="xs" xmlns:AFITESTTEMPLATE="http://www.blah.com/TEMPLATE/ABC">
should be this:
<xsd:schema
targetNamespace="http://www.whateverittakes.com/TEMPLATE/ABC"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:Q1="xs" xmlns:ABCTESTTEMPLATE="http://www.blah.com/TEMPLATE/ABC">
Dave Seng
Greenhorn

Joined: Sep 29, 2003
Posts: 5
Oh yeah, one other thing I forgot to mention, this code doesn't work until I change the first line of the xml file:
<ABCTESTTEMPLATE rder
xmlns:ABCTESTTEMPLATE="http://www.blah.com/TEMPLATE/ABC"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.blah.com/TEMPLATE/ABC Order.xsd ">
If the xsi:schemaLocation remains the way it appears now, the vaildator cannot find the xsd, only until I replace 'Order.xsd' with the absolute path of the xsd doc, does this code truly work.
Dave Seng
Greenhorn

Joined: Sep 29, 2003
Posts: 5
Holy Je-Bejesus! I frickin' got it.... WAHOOOOOOOOOOOO-weeeee!
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
What was the solution? What was the actual problem?


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Dave Seng
Greenhorn

Joined: Sep 29, 2003
Posts: 5
Was using the wrong property. Had to set - "http://apache.org/xml/properties/schema/external-schemaLocation"
instead of
"http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation"
Then had to set the value to something like: "namespace filelocation"
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Ok. Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: validation of XML w/ XSD... almost there...