You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-users@xalan.apache.org by "Vaithianathan, Shankar (c)" <Sh...@gateway.com> on 2007/02/14 22:21:02 UTC

XML Namespace Prefix Question

Guys,

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

 

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 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 xpath = "//*/myfoo:C"  [myfoo is the prefix I wish to use while
doing the xpath which is passed on through a namespace prefix resolver]

 

Both are well-formed xml though. 

 

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

 

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

 

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

 

Thanks

Shankar

 


Re: XML Namespace Prefix Question

Posted by David Bertoni <db...@apache.org>.
Vaithianathan, Shankar (c) wrote:
> okay..sorry for posting it here..
> I suppose jdk uses xalan...if not apologize for posting it here..
> Thanks for ur reply thou'

Yes, the JDK ships with Xalan-J, but it does not ship with Xalan-C.  This 
is the Xalan-C users list.

If you have product-specific questions about Xalan-J, post them to the 
Xalan-J user list.  If you have general questions about XPath, XSLT, etc., 
you should probably try the Mulberry Technology XSL list first.

Dave

RE: XML Namespace Prefix Question

Posted by "Vaithianathan, Shankar (c)" <Sh...@gateway.com>.
okay..sorry for posting it here..
I suppose jdk uses xalan...if not apologize for posting it here..
Thanks for ur reply thou'

-----Original Message-----
From: David Bertoni [mailto:dbertoni@apache.org] 
Sent: Wednesday, February 14, 2007 1:40 PM
To: xalan-c-users@xml.apache.org
Subject: Re: XML Namespace Prefix Question

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

Re: XML Namespace Prefix Question

Posted by David Bertoni <db...@apache.org>.
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