Xpath: select namespace

Tjerk Wolterink

Usenet Archive
Joined
Sep 18, 2004
Messages
218
Reaction score
0

Martin Honnen

Usenet Archive
Joined
Apr 8, 2000
Messages
6,174
Reaction score
0
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.
 

David Carlisle

Usenet Archive
Joined
Jan 5, 2000
Messages
609
Reaction score
0
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

No members online now.

Forum statistics

Threads
548,042
Messages
3,104,223
Members
248,131
Latest member
Tonypshaw1

Latest Threads

Top