Vaithianathan, Shankar (c) wrote:
Guys,

I have a very basic Question on namespace prefixes when using an xpath expression.


the Xalan-C users list is not the appropriate forum for general XML questions.

I have a document,

<foo:A xmlns:foo=”urn:foo.org”>
            <B>123</B>
            <C>456</C>
            <D>789</D>
</foo:A>


If an xpath = “foo:A/foo:C” is applied it doesn’t return anything and obviously it works for “//*/C”. Does every element under A should come with a namespace prefix “foo:”

If the context node is the document node, and the prefix foo is mapped to the URI "urn:foo.org”, then the XPath expression "foo:A/foo:C" will not return any nodes, because the element "C" is not in the namespace "urn:foo.org”.

In the following document, C is in the same namespace as A:

<foo:A xmlns:foo=”urn:foo.org”>
    <B>123</B>
    <foo:C>456</foo:C>
    <D>789</D>
</foo:A>


If the xml is structured as

<A xmlns=”urn:foo.org”>
            <B xmlns:foo=”urn:foo.org”>123</B>
            <C xmlns:foo=”urn:foo.org”>456</C>
            <D xmlns:foo=”urn:foo.org”>789</D>
</foo:A>

The namespace declaration on A defines the default namespace, so all descendant elements of A without a prefix will be in that namespace. Note that the namespace bindings for the foo prefix on the B, C, and D elements are irrelevant.


The xpath = “//*/myfoo:C” [myfoo is the prefix I wish to use while doing the xpath which is passed on through a namespace prefix resolver]


That XPath expression _will_ return the element C.

Both are well-formed xml though.


but they are different documents with respect to the expanded names of the elements.

I am using JDK1.5 XPath API and a custom JAXP1.3 NameSpaceContext implementation.

So why are you asking this question on the C users list? In fact, you should be asking such questions on a general XSLT list, like the Mulberry Technologies XSL list.


Tools like xmlspy give varied results which I guess they are trying to be liberal.

They either have bugs, or are non-conformant. Or they are conformant, and you're not giving us the correct information.


Could some one explain what is wrong in the way the prefixes are declared?

There is nothing wrong with the way the prefixes are declared. I suggest you read a tutorial on XML Namespaces, so you understand what's going on.

Dave

Reply via email to