get namaspaces for element

T

Tommy Frost

Hi,

I'm trying to figure out the xpath-expression for getting all relevant
namespaces of an element, but I don't wangle it.

When I have this document:

<A xmlns1>
<B xmlns2></B>
<C xmlns3>
<D>xxx</D>
<E></E>
</C>
</A>

....I need an expression which gives me the namespaces for <D>, that is
xmlns1 xmlns3

or even better, an expression which has the result
<D xmlns1 xmlns2>xxx</D><E></E>

BTW I'm not using xpath within xsl...I just need the plain expression.

Thanks.
 
M

Martin Honnen

Tommy said:
I'm trying to figure out the xpath-expression for getting all relevant
namespaces of an element, but I don't wangle it.

When I have this document:

<A xmlns1>
<B xmlns2></B>
<C xmlns3>
<D>xxx</D>
<E></E>
</C>
</A>

What is that above? It is not even well-formed.
...I need an expression which gives me the namespaces for <D>, that is
xmlns1 xmlns3

or even better, an expression which has the result
<D xmlns1 xmlns2>xxx</D><E></E>

I can't follow what you want as the initial example is not even
well-formed, if you want to know the namespace URI of a node you can use
the XPath 1.0 function namespace-uri().
 
T

Tommy Frost

Martin said:
What is that above? It is not even well-formed.

Sorry...I mixed it up a little.

<?xml version="1.0"?>
<A xmlns1>
<B xmlns2/>
<C xmlns3>
<D>xxx</D>
</C>
if you want to know the namespace URI of a node you can use
the XPath 1.0 function namespace-uri().

No, I want all namespaces that effect a certain element, e.g. <D> -> xmlns1,
xmlns3
 
R

Richard Tobin

No, I want all namespaces that effect a certain element, e.g. <D> -> xmlns1,
xmlns3

What form do you want them in? The namespace axis gives you the
namespace nodes that apply to an element (e.g. //D/namespace::*), but
how you do something with those nodes depends on what you're using
XPath in.

-- Richard
 
P

Patrick TJ McPhee

% >> <?xml version="1.0"?>
% >> <A xmlns1>
% >> <B xmlns2/>
% >> <C xmlns3>
% >> <D>xxx</D>
% >> </C>
% >> </A>
% >
% > That is not well-formed. Are you sure you understand XML namespaces at
% > all?
% > http://www.w3.org/TR/REC-xml-names/
%
% As far as I know, wellformed means:
% - xml-declaration

[not required]

% - at least one element
% - one root-element

- every attribute has a value, in quotes

Let's guess at what you mean

<A xmlns='data:text/plain,my-namespace-1'>
<B xmlns='data:text/plain,my-namespace-2'>
<C xmlns='data:text/plain,my-namespace-3'>
<D>xxx</D>
</C>
</B>
</A>

and you want to know what name spaces apply to D. The answer is that
only 'data:text/plain,my namespace 3' does. In XSLT, you might get
at it like this

<xsl:template match='*[local-name() = "D"'>
<xsl:text>Namespace for D is </xsl:text>
<xsl:value-of select='concat(namespace-uri(), ".")'/>
</xsl:template>

If your goal is to force this to appear as an xmlns in the declaration of D,
I'm not sure it's possible to do that consistently.
 
T

Tommy Frost

That is not well-formed. Are you sure you understand XML namespaces at

I was told by mail that people might think, that I consider xmlns1 etc. as
"real" namespaces -> No - they're just short forms of real ones. Thought
this was clear.
 
T

Tommy Frost

What form do you want them in? The namespace axis gives you the
namespace nodes that apply to an element (e.g. //D/namespace::*), but
how you do something with those nodes depends on what you're using
XPath in.

I'm using a java-prog which awaits an xpath-expression and a xml-doc. The
matching nodes are returned.
"//D/namespace::*" doesn't work, the result is empty...btw "//D" only works,
when there are no namespaces that apply to D...strange
 
R

Richard Tobin

Tommy Frost said:
"//D/namespace::*" doesn't work, the result is empty...btw "//D" only works,
when there are no namespaces that apply to D...strange

//D will only work when D is in no namespace, that is, when there is
no default namespace. If D is in a namespace because there is a
default namespace (i.e. xmlns="something"), you will have to bind a
prefix to refer to it in the XPath expression. You will need
something like //foo:D and //foo:D/namespace::*, but I don't know how
you bind a prefix in the system you're using.

-- Richard
 
P

Patrick TJ McPhee

% > That is not well-formed. Are you sure you understand XML namespaces at
% > all?

% I was told by mail that people might think, that I consider xmlns1 etc. as
% "real" namespaces -> No - they're just short forms of real ones. Thought
% this was clear.

That was clear. It's not at all clear what you mean, though, so how about
a short but complete example showing what you want as input and what
you want as output?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top