Does your schema define a namespace? If not ignore the rest of my email. If
so, "/message" won't work...you need to use namespace prefixes like
"/xy:message" assuming you define xy to be a prefix for your namespace.
"message" doesn't mean "element 'message' in my default namespace" it means
"element 'message' with no namespace" and if message is defined as part of
a namespace by your xsd, it won't match that.

Also, I don't think the prefix defined in the schema for your namespace
will carry over to xpath commands on documents using that schema...I find
it helpful to add an explicit prefix definition to my documents, something
like the "xmlns:xy" in:

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

(Or you could instead supply the prefix association in your code.)





                                                                                
                                         
                      Kevin Prichard                                            
                                         
                      <[EMAIL PROTECTED]        To:       [EMAIL PROTECTED]     
                               
                      >                        cc:                              
                                         
                                               Subject:  Does XPath API work 
for XSD-validated XML?                      
                      04/23/2003 03:15                                          
                                         
                      PM                                                        
                                         
                      Please respond to                                         
                                         
                      xerces-j-user                                             
                                         
                                                                                
                                         
                                                                                
                                         




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
[EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]







---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to