aspose file tools*
The moose likes XML and Related Technologies and the fly likes Xerces: Using the method sax.Counter in xercesSamples.jar from the command line 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 "Xerces: Using the method sax.Counter in xercesSamples.jar from the command line" Watch "Xerces: Using the method sax.Counter in xercesSamples.jar from the command line" New topic
Author

Xerces: Using the method sax.Counter in xercesSamples.jar from the command line

Rick Silva
Greenhorn

Joined: May 05, 2011
Posts: 6
I am looking for a way to validate XML from the command line. I have an XML file and an XSD file (an XML Schema definition file). My objective is to make sure the XML file follows the rules defined in the XSD file.

I noticed that Xerces has a xercesSamples.jar file that comes with a sax.Counter method. My understanding is that this method will count, and also do validation if you use the -v switch. But I can't figure out how to call it from the command line and provide the XML file name and the XSD file name.

The closest I get is:

java -classpath /xerces-2_9_0/xercesSamples.jar:/xerces-2_9_0/xercesImpl.jar sax.Counter -v -s my_xml_data_file.xml

But where do I specify the name of the xsd file?
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 535
    
    3
If you want to use the sample/demo application sax.Counter class (not a "method" properly speaking) as a vehicle of validating an xml document, just note that it is not its principal functionality. Hence, it could appear fairly restrictive in that regard, meaning the information of schema location has to be provisioned inside the xml document via the XMLSchema-instance (xsi) namespace's vocabulary, namely the attribute added to the root of the xml document: xsi:schemaLocation (if it is defined in a non-null namespace) or xsi:noNamespaceSchemaLocation. Here is the reference, tracing the links further for syntax and specific detail of it:
http://www.w3.org/TR/2004/REC-xmlschema-1-20041028/structures.html#xsi_schemaLocation

To help you get the concret idea of it, this is how it operates.
[1] Suppose your xml document's root is "root" defined in a namespace "urn:xyz" with prefix "pfx" say, and the schema document abc.xsd is in the same directory of the xml, the root should appear something like this.

[2] Suppose the same root but it has no namespace attached to to it, the root would appear something like this.

Then you can switch on -v and watch for the validation error messages in std.err.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Xerces: Using the method sax.Counter in xercesSamples.jar from the command line