You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xalan.apache.org by Scott Boag/CAM/Lotus <Sc...@lotus.com> on 2000/01/14 01:18:25 UTC

RE: Simlpe XPointer / XPath / XLocator help

Things get a little harder when you need namespaces (I was hopeing you
wouldn't notice  :-)  ).

You need to implement a class that implements
org.apache.xalan.xpath.xml.PrefixResolver, which has a
getNamespaceForPrefix(String prefix) method and a
getNamespaceForPrefix(String prefix, org.w3c.dom.Node context) method.  I
ought to have a standalone, default version of these, but I don't at the
moment (one could be put together fairly easily by stealing code from other
parts of Xalan... but I won't have time to do this in the next couple of
weeks).

Then you pass the prefix resolver as the last parameter to execute and the
last parameter to initXPath (the prefixes in the XPath need to be resolve
also... remember that the code knows nothing about prefixes... only about
the namespaces they resolve to).

Another way that the Xalan XPath code could be tweaked would be that, if it
didn't have a namespace resolver, it would just use the prefix as the
namespace.  Wouldn't be kosher, but it might work for some circumstances.

-scott




                                                                                                                   
                    Brian Dupras                                                                                   
                    <briand@cente        To:     'Scott Boag/CAM/Lotus' <Sc...@lotus.com>                     
                    ra.com>              cc:                                                                       
                                         Subject:     RE: Simlpe XPointer / XPath / XLocator help                  
                    01/13/00                                                                                       
                    06:49 PM                                                                                       
                                                                                                                   
                                                                                                                   




Awesome!  This is exaclty what I needed to get going.  One problem, though.
I've got the example code working below on an xml doc without namespaces.
As soon as I run it against a doc with namespaces, the xpath selection
breaks.  Below are my 2 xml docs, and their XPath selects.  Is this
operator
error?


Thanks again Scott,

Brian Dupras
briand@centera.com


In both cases, the contextNode is the .getDocumentNode() (<site_view> and
<pp:site_view> respectively)

No namespaces (works fine)
----------------------------------------------
XPath : "./data/page/@rid" = "abdcedfghi234"

<?xml version="1.0" ?>
<site_view rid='urn:uuid:abdcedfghi899'>
  <metadata>
    <editor>Brian Dupras</editor>
    <description>blah blah blah</description>
  </metadata>
  <data>
    <!--Main Page-->
    <page rid='abdcedfghi234' />
  </data>
</site_view>



Namespaces (no nodes selected)
----------------------------------------------
XPath : "./pp:data/pp:page/@rid" = no nodes selected

<?xml version="1.0" ?>
<pp:site_view rid='urn:uuid:abdcedfghi899'
    xmlns:pp='http://medwired.com/namespaces/practiceportal/1.0'
    >
  <pp:metadata>
    <pp:editor>Brian Dupras</pp:editor>
    <pp:description>blah blah blah</pp:description>
  </pp:metadata>
  <pp:data>
    <!--Main Page-->
    <pp:page rid='abdcedfghi234' />
  </pp:data>
</pp:site_view>






Your sample code, changed a bit by me
----------------------------------------------

public NodeList selectNodes(Node contextNode, String str)
           throws SAXException
{
           XPathSupportDefault xpathSupport = new XPathSupportDefault();
           XPath xpath         = new XPath(xpathSupport, null);
           XPathProcessorImpl parser = new
XPathProcessorImpl(xpathSupport);
           parser.initXPath(xpath, str, null);
           XObject list = xpath.execute(xpathSupport, contextNode, null);
           NodeList nl = list.nodeset();

           //this debugout shows that no nodes are selected in the
namespace
example
           debugout("selected " + (nl.getLength()) + " nodes<br>");

           return (nl.getLength() > 0) ? nl : null;
}

public Node selectSingleNode(Node contextNode, String str)
           throws SAXException
{
           NodeList nl = selectNodes(contextNode, str);
           return (nl != null) ? nl.item(0) : null;
}


How I'm calling the above code:
----------------------------------------------
Node theNode = selectSingleNode(xmlelemSiteView, "./data/page/@rid");
debugout("selected: " + ((theNode!=null) ? theNode.getNodeValue() :
"nothing"));