Xpath: select namespace

T

Tjerk Wolterink

M

Martin Honnen

Tjerk said:
IU have the following xsl root element:

<xsl:stylesheet version="1.0"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:page="http://www.wolterinkwebdesign.com/xml/page"
xmlns:xc="http://www.wolterinkwebdesign.com/xml/xcontent">


Well how do i select the xhtml namespace with xpath
I need it to use in a xpath expression like this:

*[namespace-uri(.) == namespace::xc]

But namespace::xc' should be the namespace of xhtml.

You need to declare a namespace prefix for XHTML e.g.

<xsl:stylesheet version="1.0"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:page="http://www.wolterinkwebdesign.com/xml/page"
xmlns:xc="http://www.wolterinkwebdesign.com/xml/xcontent">

then you can use that prefix e.g.
<xsl:template match="xhtml:*">
to match elements in that namespace.
 
D

David Carlisle

Tjerk Wolterink said:
IU have the following xsl root element:

<xsl:stylesheet version="1.0"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:page="http://www.wolterinkwebdesign.com/xml/page"
xmlns:xc="http://www.wolterinkwebdesign.com/xml/xcontent">


Well how do i select the xhtml namespace with xpath
I need it to use in a xpath expression like this:

*[namespace-uri(.) == namespace::xc]

But namespace::xc' should be the namespace of xhtml. Pleas help

since it's a constant you don't need to extract it from the source
you can just use
*[namespace-uri(.) = 'http://www.w3.org/1999/xhtml']


although as I said earlier it's simpler and more efficient (probably) to
write that as
h:*
and add
xmlns:h="http://www.w3.org/1999/xhtml"
to your xsl:stylesheet.


even in the xc case

*[namespace-uri(.) = namespace::xc]

is rather dangerous as it forces a dependency on the prefixes used in
the source, whereas normally in XPath a source document can use any
prefix.

given your xsl:stylesheet above then the Xpath

xc:*

selects any element in the namespace
http://www.wolterinkwebdesign.com/xml/xcontent
which is what you want.

However

*[namespace-uri(.) = namespace::xc]

selects any element whose namespace is bound to the prefix xc
in the source file. (So it might match MathML or XHTML or SVG or any
other namespace, if that namespace happens to be bound to the prefix xc
in the document)

David
 

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

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top