This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: xpathapi question


Hi Sanjay,

> I have had no problem using XPathAPI Java function for various
> purposes on XML data, but when I use XPathAPI on this XSD (Schema),
> I am not getting any result back.
[snip]
> What am I missing? Some NameSpace kind of issue or what?

Yes, I think it's a namespace issue. The XPathAPI.eval() method (with
two arguments) uses the context node passed as the first argument to
resolve the prefix/namespace bindings that you're using within the
XPath that you use as the second argument.

Now, one thing about XPath is that node tests that don't have a prefix
select elements in *no* namespace (rather than the default namespace),
so the only type of XPath that you could use to select these elements
(which are in the XMLSchema namespace) is one that had a prefix, e.g.:

  //xs:element[@name = 'x']

With the two-argument version of eval(), the context node needs to
have the relevant prefix/namespace bindings available to it. You don't
have any elements in your document that have the correct
prefix/namespace association (since you're using the default namespace
for the XML namespace, rather than using the 'xs' prefix).

You could add an namespace declaration to the schema, so that it
looked like:

<schema targetNamespace="http://www.x.com/x";
        xmlns="http://www.w3.org/2001/XMLSchema";
        xmlns:x="http://www.x.com/x";
        xmlns:xs="http://www.w3.org/2001/XMLSchema";
        elementFormDefault="qualified">
...
</schema>

You could then use the document element of the schema as the context
node from which to evaluate the XPath (don't use the root node, as
this doesn't have any namespace bindings available to it):

  obj = XPathAPI.eval(rootNode.getDocumentElement(),
                      "//xs:element[@name = 'x']");
  result = obj.toString();

Alternatively, you could create your own PrefixResolver and use this
as a third argument to the XPathAPI.eval() method, ensuring that:

  resolver.getNamespaceForPrefix("xs")

returned "http://www.w3.org/2001/XMLSchema";.

If that proved too complicated you could use an XPath that didn't have
any prefixes in it, as follows:

  //*[@name = 'x' and local-name() = 'element' and
      namespace-uri() = 'http://www.w3.org/2001/XMLSchema']

I hope that helps,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]