Xpath: select namespace

Discussion in 'XML' started by Tjerk Wolterink, Nov 17, 2004.

  1. 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
    Tjerk Wolterink, Nov 17, 2004
    #1
    1. Advertising

  2. Tjerk Wolterink wrote:

    > 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.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Nov 17, 2004
    #2
    1. Advertising

  3. Tjerk Wolterink <> writes:

    > 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
    David Carlisle, Nov 17, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marvin_123456

    "Memory leak" in javax.xml.xpath.XPath

    Marvin_123456, Jul 29, 2005, in forum: Java
    Replies:
    4
    Views:
    1,964
    jan V
    Jul 29, 2005
  2. Alastair Cameron
    Replies:
    1
    Views:
    7,391
    SQL Server Development Team [MSFT]
    Jul 8, 2003
  3. Anna
    Replies:
    0
    Views:
    519
  4. goog
    Replies:
    0
    Views:
    504
  5. palmiere
    Replies:
    1
    Views:
    396
    Erwin Moller
    Feb 9, 2004
Loading...

Share This Page