XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition (594 page)

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
13.93Mb size Format: txt, pdf, ePub

Examples

Consider the source document:


Expression
Result
namespace-uri(/)
“”
namespace-uri(/*)
“http://ibm.com/ebiz”
namespace-uri(/*/@security)
“”
namespace-uri(/*/@xml:id)
“http://www.w3.org/XML/1998/namespace”
namespace-uri(/*/namespace::my)
“”

Usage

Let's start with some situations where you
don't
need this function.

If you want to test whether the context node belongs to a particular namespace, the best way to achieve this is using a
NameTest
of the form
prefix:*
. For example, to test (in XSLT) whether the current element belongs to the
http://ibm.com/ebiz
namespace, write:

 

If you want to find the namespace URI corresponding to a given prefix the best solution is to use namespace nodes. You might need to do this if namespace prefixes are used in attribute values: the XSLT standard itself uses this technique in attributes such as
extension-element-prefixes
, and there is no reason why other XML document types should not do the same. If you have an attribute
@value
, which you know takes the form of a namespace-qualified name (a
QName
), you can get the associated namespace URI using the expression:

namespace-uri-for-prefix(substring-before(@value, ‘:’), .)

The
namespace-uri()
function, by contrast, is useful in display contexts, where you just want to display the namespace URI of the current node, and also if you want to do more elaborate tests. For example, you may know that there is a whole family of namespaces whose URIs all begin with
urn:schemas.biztalk
, and you may want to test whether a particular element is in any one of these. You can achieve this by writing:

if (starts-with(namespace-uri(), ‘urn:schemas.biztalk’)) then …

See Also

local-name()
on page 824

name()
on page 835

namespace-uri-for-prefix

The function
namespace-uri-for-prefix()
returns the namespace URI corresponding to a given namespace prefix in the in-scope namespaces of a particular element node.

Changes in 2.0

This function is new in XPath 2.0. Together with
in-scope-prefixes()
, it provides a replacement for the namespace axis, which is deprecated in XPath 2.0.

Signature

Argument
Type
Meaning
prefix
xs:string?
The namespace prefix whose corresponding namespace URI is required, or the zero-length string to get the default namespace URI. If the argument is an empty sequence, it is treated as a zero-length string.
element
element()
The element node to be examined to find an in-scope namespace declaration for this prefix.
Result
xs:anyURI?
The namespace URI corresponding to the given prefix
.

Effect

The in-scope namespaces for an element are represented in the data model as namespace nodes, and the behavior of this function is therefore described in terms of a search of the namespace nodes.

This function searches the namespace nodes of the given element. If it finds a namespace node whose name matches the given prefix, then it returns the string value of this namespace node, as an
xs:anyURI
value. If it doesn't find one, then it returns the empty sequence.

Example

The following example shows the in-scope namespace URIs for every element in a source document.

Source

Note that this includes a namespace undeclaration for the
soap
namespace, as permitted by XML Namespaces 1.1.


      xmlns:soap=“http://schemas.xmlsoap.org/soap/envelope/”

      xmlns:xs=“http://www.w3.org/2001/XMLSchema”>

  

    

                xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

                xmlns:soap=“”>

      Hello

    

  


Stylesheet

   

     

              namespaces=“{for $n in in-scope-prefixes(.)

                                    return namespace-uri-for-prefix($n, .)}”/>

     

   

Output

(Reformatted for legibility.) Note the absence of the SOAP namespace from the inner elements.

         namespaces=“http://www.w3.org/XML/1998/namespace

         http://schemas.xmlsoap.org/soap/envelope/

         http://www.w3.org/2001/XMLSchema”/>

         namespaces=“http://www.w3.org/XML/1998/namespace

         http://schemas.xmlsoap.org/soap/envelope/

         http://www.w3.org/2001/XMLSchema”/>

         namespaces=“http://www.w3.org/XML/1998/namespace

         http://www.w3.org/2001/XMLSchema

         http://www.w3.org/2001/XMLSchema-instance

         http://example.com/soapdemo”/>

         namespaces=“http://www.w3.org/XML/1998/namespace

         http://www.w3.org/2001/XMLSchema

         http://www.w3.org/2001/XMLSchema-instance

         http://example.com/soapdemo”/>

Other books

Tournament of Losers by Megan Derr
90_Minutes_to_Live by JournalStone
Private Party by Graeme Aitken
Dead City - 01 by Joe McKinney
No Stopping for Lions by Joanne Glynn
Drawing Conclusions by Donna Leon
Kiss of the Rose by Kate Pearce
The Marriage Trap by Jennifer Probst