get prefix for namspace in xslt/xpath 1.0

T

Tim Hallwyl

Is it possible to resolve a namspace-uri to the corresponding prefix
using XPath 1.0?

For example if I have obtained a namespace-uri a an element in one
document and I need to know the prefix used at some element in an other
document, using XPath?

Thanks.
 
B

Bjoern Hoehrmann

* Tim Hallwyl wrote in comp.text.xml:
Is it possible to resolve a namspace-uri to the corresponding prefix
using XPath 1.0?

For example if I have obtained a namespace-uri a an element in one
document and I need to know the prefix used at some element in an other
document, using XPath?

I am not really sure what you are asking, but keep in mind that there
might not be a declared prefix for some namespace name, the names are
generally arbitrary, the same prefix may be used for different name-
space names in different places in one document, and there may be many
prefixes for a given namespace name. In http://www.w3.org/TR/xpath you
can read up on the namespace axis, namespace nodes, and the name()
function, which might give you what you are looking for.
 
T

Tim Hallwyl

Bjoern Hoehrmann skrev:
* Tim Hallwyl wrote in comp.text.xml:

I am not really sure what you are asking, but keep in mind that there
might not be a declared prefix for some namespace name,

I am sorry I was not clear on this, but I am guaranteed that the
namespace-uri is declared in both documents.
the names are generally arbitrary, the same prefix may be used for
different name- space names in different places in one document, and
there may be many prefixes for a given namespace name.

Exactly. Let me reformulate my question: Provided an XPath to some
element and a namespace-uri that somehow is guaranteed to be declared
with a prefix for the element that the XPath points at, how do get that
prefix?
In http://www.w3.org/TR/xpath you can read up on the namespace axis,
namespace nodes, and the name() function, which might give you what
you are looking for.

Thank you for the pointers. I have been there before, but I just could
not get it working. It turns out I made the mistake of trying with the
the default namespace uri -- my bad. So to answer my own question, based
on your pointers:

Given an XPath to some element, e.g.
/bpel:process/bpel:sequence/bpel:reply

and a namespace-uri, e.g.
http://schemas.xmlsoap.org/wsdl/

I can get the (first) prefix declared using:

name(/bpel:process/bpel:sequence/bpel:reply/namespace::*[self::node() =
'http://beepell.com/samples/proxy/definitions'][1])

If anyone know a more elegant method, let me know. Thanks.
 
J

Joseph Kesselman

Tim said:
Is it possible to resolve a namspace-uri to the corresponding prefix
using XPath 1.0?

The obvious way would be to use the namespace axis to query all the
namespace nodes in scope at a given location and then examine those to
see which ones (note the plural!) bind a prefix to the URI you're
interested in.
 
D

Dimitre Novatchev

Given an XPath to some element, e.g.
/bpel:process/bpel:sequence/bpel:reply

and a namespace-uri, e.g.
http://schemas.xmlsoap.org/wsdl/

I can get the (first) prefix declared using:

name(/bpel:process/bpel:sequence/bpel:reply/namespace::*[self::node() =
'http://beepell.com/samples/proxy/definitions'][1])

If anyone know a more elegant method, let me know. Thanks.


In case you need just one prefix that is in scope for a node

$nd

then the following XPath expression provides it and does not look too bad:

name($nd/namespace::*[.='$strNamespaceURI'][1])

This may actually be quite efficient, too, as a clever XPath implementation
will stop when the first such node is found.


Cheers,
Dimitre Novatchev

Tim Hallwyl said:
Bjoern Hoehrmann skrev:
* Tim Hallwyl wrote in comp.text.xml:

I am not really sure what you are asking, but keep in mind that there
might not be a declared prefix for some namespace name,

I am sorry I was not clear on this, but I am guaranteed that the
namespace-uri is declared in both documents.
the names are generally arbitrary, the same prefix may be used for
different name- space names in different places in one document, and
there may be many prefixes for a given namespace name.

Exactly. Let me reformulate my question: Provided an XPath to some
element and a namespace-uri that somehow is guaranteed to be declared
with a prefix for the element that the XPath points at, how do get that
prefix?
In http://www.w3.org/TR/xpath you can read up on the namespace axis,
namespace nodes, and the name() function, which might give you what you
are looking for.

Thank you for the pointers. I have been there before, but I just could not
get it working. It turns out I made the mistake of trying with the the
default namespace uri -- my bad. So to answer my own question, based on
your pointers:

Given an XPath to some element, e.g.
/bpel:process/bpel:sequence/bpel:reply

and a namespace-uri, e.g.
http://schemas.xmlsoap.org/wsdl/

I can get the (first) prefix declared using:

name(/bpel:process/bpel:sequence/bpel:reply/namespace::*[self::node() =
'http://beepell.com/samples/proxy/definitions'][1])

If anyone know a more elegant method, let me know. Thanks.
 
D

Dimitre Novatchev

name($nd/namespace::*[.='$strNamespaceURI'][1])


must be:

name($nd/namespace::*[.= $strNamespaceURI ] [1] )

Dimitre Novatchev said:
Given an XPath to some element, e.g.
/bpel:process/bpel:sequence/bpel:reply

and a namespace-uri, e.g.
http://schemas.xmlsoap.org/wsdl/

I can get the (first) prefix declared using:

name(/bpel:process/bpel:sequence/bpel:reply/namespace::*[self::node() =
'http://beepell.com/samples/proxy/definitions'][1])

If anyone know a more elegant method, let me know. Thanks.


In case you need just one prefix that is in scope for a node

$nd

then the following XPath expression provides it and does not look too bad:

name($nd/namespace::*[.='$strNamespaceURI'][1])

This may actually be quite efficient, too, as a clever XPath
implementation will stop when the first such node is found.


Cheers,
Dimitre Novatchev

Tim Hallwyl said:
Bjoern Hoehrmann skrev:
* Tim Hallwyl wrote in comp.text.xml:
Is it possible to resolve a namspace-uri to the corresponding prefix
using XPath 1.0?

For example if I have obtained a namespace-uri a an element in one
document and I need to know the prefix used at some element in an other
document, using XPath?

I am not really sure what you are asking, but keep in mind that there
might not be a declared prefix for some namespace name,

I am sorry I was not clear on this, but I am guaranteed that the
namespace-uri is declared in both documents.
the names are generally arbitrary, the same prefix may be used for
different name- space names in different places in one document, and
there may be many prefixes for a given namespace name.

Exactly. Let me reformulate my question: Provided an XPath to some
element and a namespace-uri that somehow is guaranteed to be declared
with a prefix for the element that the XPath points at, how do get that
prefix?
In http://www.w3.org/TR/xpath you can read up on the namespace axis,
namespace nodes, and the name() function, which might give you what you
are looking for.

Thank you for the pointers. I have been there before, but I just could
not get it working. It turns out I made the mistake of trying with the
the default namespace uri -- my bad. So to answer my own question, based
on your pointers:

Given an XPath to some element, e.g.
/bpel:process/bpel:sequence/bpel:reply

and a namespace-uri, e.g.
http://schemas.xmlsoap.org/wsdl/

I can get the (first) prefix declared using:

name(/bpel:process/bpel:sequence/bpel:reply/namespace::*[self::node() =
'http://beepell.com/samples/proxy/definitions'][1])

If anyone know a more elegant method, let me know. Thanks.
 
T

Tim Hallwyl

Dimitre Novatchev skrev:
Given an XPath to some element, e.g.
/bpel:process/bpel:sequence/bpel:reply

and a namespace-uri, e.g.
http://schemas.xmlsoap.org/wsdl/

I can get the (first) prefix declared using:

name(/bpel:process/bpel:sequence/bpel:reply/namespace::*[self::node() =
'http://beepell.com/samples/proxy/definitions'][1])

If anyone know a more elegant method, let me know. Thanks.


In case you need just one prefix that is in scope for a node

$nd

then the following XPath expression provides it and does not look too bad:

name($nd/namespace::*[.='$strNamespaceURI'][1])

This may actually be quite efficient, too, as a clever XPath implementation
will stop when the first such node is found.

Thanks Dimitre! It does look more elegant, so I will use it. However, I
am not sure why it might be more efficient. Besides the usage of
variables, the only difference is replacing self::node() with .

name($nd/namespace::*[self::node() = $ns][1])
name($nd/namespace::*[.= $ns][1])

Does the usage of . instead of self::node() cause a more efficient
processing in some implementations? The specification says "A location
step of . is short for self::node()." Did I missed something (again)?

Thanks

--tim
 
D

Dimitre Novatchev

Tim Hallwyl said:
Dimitre Novatchev skrev:
Given an XPath to some element, e.g.
/bpel:process/bpel:sequence/bpel:reply

and a namespace-uri, e.g.
http://schemas.xmlsoap.org/wsdl/

I can get the (first) prefix declared using:

name(/bpel:process/bpel:sequence/bpel:reply/namespace::*[self::node() =
'http://beepell.com/samples/proxy/definitions'][1])

If anyone know a more elegant method, let me know. Thanks.


In case you need just one prefix that is in scope for a node

$nd

then the following XPath expression provides it and does not look too
bad:

name($nd/namespace::*[.='$strNamespaceURI'][1])

This may actually be quite efficient, too, as a clever XPath
implementation will stop when the first such node is found.

Thanks Dimitre! It does look more elegant, so I will use it. However, I am
not sure why it might be more efficient. Besides the usage of variables,
the only difference is replacing self::node() with .

name($nd/namespace::*[self::node() = $ns][1])
name($nd/namespace::*[.= $ns][1])

Does the usage of . instead of self::node() cause a more efficient
processing in some implementations? The specification says "A location
step of . is short for self::node()." Did I missed something (again)?


Tim, you are right.

I was comparing this solution to what Joseph Kesselman proposed:

"The obvious way would be to use the namespace axis to query all the
namespace nodes in scope at a given location and then examine those to
see which ones (note the plural!) bind a prefix to the URI you're
interested in."

What he proposed ammounts to getting all namespace nodes in scope and then
see *which ones* (note the plural!) have the namespace-uri of interest.

What I specified is just get the first prefix bound to the required
namespace-uri and do not care (stop!) for any other such prefixes.


Cheers,
Dimitre Novatchev
 
J

Joe Kesselman

Dimitre said:
What I specified is just get the first prefix bound to the required
namespace-uri and do not care (stop!) for any other such prefixes.

Which may or may not be what you want, depending on why you're
retrieving this information.
 

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
474,008
Messages
2,570,271
Members
46,874
Latest member
CyberGateway

Latest Threads

Top