• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Holy Je-Bejesus! I frickin' got it.... WAHOOOOOOOOOOOO-weeeee!
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What was the solution? What was the actual problem?
 
Dave Seng
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok. Thanks.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic