You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Kevin Prichard <xm...@prichard.org> on 2003/04/23 21:15:37 UTC

Does XPath API work for XSD-validated XML?

Quick question-

Does the XPath API provide XPath value and node 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

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@prichard.org


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org