xml-general mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Scott Boag/CAM/Lotus" <Scott_B...@lotus.com>
Subject Re: Simlpe XPointer / XPath / XLocator help
Date Thu, 13 Jan 2000 22:46:29 GMT

You can certainly do what you want with Xalan's XPath.  The XPath module is
meant to be used stand-alone, so you don't necessarily have to bring in the
XSLT engine, if that is a concern.

The Xalan XPath interface needs some work, but is functional.  Here's a
full example:

import org.xml.sax.*;
import java.io.*;
import org.apache.xerces.parsers.*;
import org.w3c.dom.*;
import org.apache.xalan.xpath.*;
import org.apache.xalan.xpath.xml.*;

class SimpleXPathExample
{

  public SimpleXPathExample()
  {
    super();
  }

  /**
   * Hide the XPath interface in this function.
   * @param contextNode The node to start searching from.
   * @param str A valid XPath string.
   * @return The first node found that matches the XPath, or null.
   */
  public static Node selectSingleNode(Node contextNode, String str)
    throws SAXException
  {
    // Since we don't have a XML Parser involved here, install some default
support
    // for things like namespaces, etc.
    XPathSupportDefault xpathSupport = new XPathSupportDefault();

    // Create the XPath object.
    XPath xpath = new XPath(xpathSupport, null);

    // Create a XPath parser.
    XPathProcessorImpl parser = new XPathProcessorImpl(xpathSupport);
    parser.initXPath(xpath, str, null);

    // Execute the XPath, and have it return the result
    XObject list = xpath.execute(xpathSupport, contextNode, null);

    // Have the XObject return it's result as a NodeSet.
    NodeList nl = list.nodeset();

    // Return the first node, or null
    return (nl.getLength() > 0) ? nl.item(0) : null;
  }

  // Do a simple test
  public static void main(String[] args)
    throws SAXException
  {
    DOMParser parser = new DOMParser();
    Reader xmlReader;

    String tempstr = "<doc><rw a=\"1\"><d>7</d></rw><rw a
=\"2\"><d>0.54</d></rw></doc>";
    xmlReader = new InputStreamReader(new
ByteArrayInputStream(tempstr.getBytes()));
    InputSource src = new InputSource(xmlReader);
    try
    {
      parser.parse(src);
    }
    catch(SAXException e)
    {
      System.out.println("Failed to parse input in OpenXMLDocumentRead");
    }
    catch(IOException e)
    {
      System.out.println("I/O Exception occured when attempting to parse
input file/string");
    }
    Node root = parser.getDocument().getDocumentElement();

    Node d = selectSingleNode(root, "/doc/rw[@a='1']/d");
    try
    {
      System.out.println("node name is " + d.getNodeName());
    }
    catch (NullPointerException e)
    {
      System.out.println("Exception - Returned null");
    }

  }

}

-scott




                                                                                         
                         
                    Brian Dupras                                                         
                         
                    <briand@cente        To:     "Xml-Apache (E-mail)" <general@xml.apache.org>
                   
                    ra.com>              cc:     (bcc: Scott Boag/CAM/Lotus)          
                            
                                         Subject:     Simlpe XPointer / XPath / XLocator help
                     
                    01/13/00                                                             
                         
                    02:08 PM                                                             
                         
                    Please                                                               
                         
                    respond to                                                           
                         
                    general                                                              
                         
                                                                                         
                         
                                                                                         
                         




Back when I was using XML4J, I was able to do XPointer (the older spec)
fairly easily.  But with xerces, I can't get
com.ibm.xml.xpointer.XPointer.point() to work with anything more that
"root()"

Am I doing something wrong, or is it that the XPointer stuff is too old to
work proplerly with xerces?


Or, is there a xerces / xalan way of doing what I need?  My needs are
simple:
  "Get me the first <myns:page> node under the <myns:data> node under the
root node"
    XPointer (old): "root().child(1,myns:data).child(1,myns:page)"
  "Get me the value of the attribute named 'foo' under a specific node"
    XPointer (old): "attr(foo)"

(I've tried this with and without namespaces inthe xml document.)


My needs will grow, so I don't whan to have to walk the tree manually for
each bit of data that I need.  Is XPath what I want?  XLocator?  I guess I
don't fully understand the definition of each of these specs.


sample xml:

<?xml version="1.0" ?>
<pp:site_view rid='urn:uuid:abdcedfghi899'
    xmlns:pp='http://ourserver/namespaces/pp/1.0'
    xmlns:xlink='http://www.w3.org/XML/XLink/0.9'
    xmlns:html='http://www.w3.org/1999/xhtml'
    >
  <pp:metadata>
    <pp:editor>Brian Dupras</pp:editor>
    <pp:description>blah blah blah</pp:description>
  </pp:metadata>
  <pp:data>
    <!--Main Page-->
    <pp:page rid='urn:uuid:abdcedfghi234' />
  </pp:data>
</pp:site_view>


My first goal is to get the value of the "rid" attribute of the <pp:page>
labeled Main Page in the above example.

Thanks,

Brian Dupras
Centera Information Systems, Inc.
phone           303.939.0200 x294
fax        303.939.0111
web        http://www.centera.com
email           briand@centera.com





Mime
View raw message