logo       

XPath API work for XSD-validated XML?: msg#00028

Subject: XPath API work for XSD-validated XML?
Quick question-

Does the XPath API provide XPath value extraction for XSD-validated
documents?

I'm able to successfully parse an XSD-validated XML file into a DOM
Document, but then the document appears to be empty.  Is there another way
to go about this?  My "working" demo code below.  The program's output is
prepended, and the sample XML and XSD files I'm working with are below.

Thanks, Kevin Prichard

PS. In case the mailer breaks the code up too much, here's a link to the
sourcecode: http://generalpublic.org/Tester.java

---- Output ----
getDT: null
Number of selected nodes: 0
[message: null]

---- Program ----
import java.util.*;
import java.io.*;
import java.lang.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import org.apache.xpath.objects.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;

public class Tester implements org.xml.sax.ErrorHandler
{

    Document _doc = null;

    // Constructor //
    public Tester( BufferedReader buffer,
                   String base_DTD_URI )
    {
        DocumentBuilderFactory factory = null;
        DocumentBuilder docBuilder     = null;
        try {
            String JAXP_SCHEMA_LANGUAGE =
"http://java.sun.com/xml/jaxp/properties/schemaLanguage";;
            String W3C_XML_SCHEMA       =
"http://www.w3.org/2001/XMLSchema";;
            factory = DocumentBuilderFactory.newInstance();
            factory.setValidating( true );    // false, until we get the
DTD archive working
            factory.setNamespaceAware( true );    // false, until we get
the DTD archive working
            try {
                factory.setAttribute( JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA
);
            }
            catch (IllegalArgumentException x) {
                // Happens if the parser does not support JAXP 1.2
                System.err.println("Problem initializing factory with XML
Schema attributes: "+x.getMessage());
                System.exit(-1);
            }
            docBuilder = factory.newDocumentBuilder();
        } catch (ParserConfigurationException parserConfigErr) {
            System.err.println("Error in Tester startup...");
        }

        try {
            InputSource src = new InputSource( buffer );
            src.setSystemId( base_DTD_URI );
            docBuilder.setErrorHandler( this );
            Document doc = docBuilder.parse( src );
            System.out.println( "getDT: "+doc.getDoctype() );
            NodeList nodes = org.apache.xpath.XPathAPI.selectNodeList(
doc, "/message" );
            System.out.println( "Number of selected
nodes: "+nodes.getLength() );
            System.out.println( doc.getDocumentElement().toString() );
            this._doc = doc;
            // fini!
        } catch (SAXException parseExcep) {
            // A parsing error occurred; the xml input is not valid
            System.out.println( "Hmm! Some kinda error while
parsing: "+parseExcep.getMessage() );
        } catch (TransformerException transExcep) {
            System.out.println( "Hmm3! A transformer-related error while
XPathing: "+transExcep.getMessage() );
        } catch (IOException ioExcep) {
            System.out.println( "Hmm2! IOException while
parsing: "+ioExcep.getMessage() );
        }
    }


    // Implement the org.xml.sax.ErrorHandler interface.
    public void error( SAXParseException exception ) {
        System.out.println( "----- error -----" );
        System.out.println( exception.getMessage() );
        System.out.println( "At: line "+exception.getLineNumber()+
                            ", col "+exception.getColumnNumber() );
    }

    public void fatalError( SAXParseException exception ) {
        error( exception );
        System.exit(0);
    }

    public void warning( SAXParseException exception ) {
        error(exception);
    }

    public static void main(String[] argv) throws Exception {
        BufferedReader rdr = new BufferedReader( new FileReader( argv[0]
) );
        Tester test = new Tester( rdr, argv[1] );
    }

}


---- XML file ----
<?xml version="1.0"?>

<message
xmlns="http://192.168.105.125";
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
xsi:schemaLocation="http://192.168.105.125 ttest.xsd">

<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
<time_stamp> April 22, 2003, at 12:35 PM ET </time_stamp>
</message>

---- XSD file ----
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema";
targetNamespace="http://192.168.105.125";
xmlns="http://192.168.105.125";
elementFormDefault="qualified">

<xs:element name="message">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="to" type="xs:string"/>
        <xs:element name="from" type="xs:string"/>
        <xs:element name="heading" type="xs:string"/>
        <xs:element name="body" type="xs:string"/>
        <xs:element name="time_stamp" type="xs:string"/>
      </xs:sequence>
    </xs:complexType>
</xs:element>

</xs:schema>


Kevin Prichard
xml@xxxxxxxxxxxx

-----------------------------------------------------
xml-interest: A list for discussing XML technologies in the Java Platform.
To post, mailto:xml-interest@xxxxxxxxxxxx
Archives at: http://archives.java.sun.com/xml-interest.html
To unsubscribe, mailto:listserv@xxxxxxxxxxxx the following message;
signoff xml-interest.



<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

Recently Viewed:
linux.arklinux....    user-groups.lin...    kde.usability/2...    ietf.ipp/2002-0...    mail.spam.spamc...    os.netbsd.devel...    audio.cd-record...    text.unicode.de...    php.documentati...    games.fps.halfl...    window-managers...    suse.oracle.gen...    bug-tracking.gn...    video.dvdrip.us...    xfree86.cvs/200...    java.netbeans.m...    network.argus/2...    culture.sf.kill...    debian.ports.al...    freebsd.questio...    qplus.devel/200...    handhelds.palm....   
Home | blog view | USPTO Patent Archive | advertise | OSDir is an inevitable website. super tiny logo

Free Magazines

Cisco News
Receive a free quarterly e-newsletter with exclusive articles on how Cisco IT uses its own products and solutions to enable the business.
subscribe

Systems Management News, the newspaper for IT systems administration and data center managers! Each issue of Systems Management News is chock-full of news and analysis to help you understand what's happening in your field.
subscribe

The Enterprise Newsweekly eWeek is the essential technology information source for builders of e-business.
subscribe

Oracle Magazine Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world's largest enterprise software company.
subscribe

Total Telecom Total Telecom is "The Economist of the communications industry".
subscribe