Simple XSL Namespace Question

Discussion in 'XML' started by joe.osowski@gmail.com, Apr 22, 2005.

  1. Guest

    OK, I hate to ask, but I seem to have run across one of those issues
    where there answer is so grossly obvious, I can't see it.

    Scenario:

    XML:

    <?xml version="1.0" encoding="utf-8"?>
    <Report>
    <user>
    <lastname>Smith</lastname>
    </user>
    </Report>

    XSL:

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:eek:utput method="text" encoding="utf-8" />

    <xsl:template match="/">
    <xsl:value-of select="Report/user/lastname" />
    </xsl:template>
    </xsl:stylesheet>

    Outputs:

    Smith

    Scenario 2: (adding a namespace)

    <?xml version="1.0" encoding="utf-8"?>
    <Report xmlns="http://schema.somedomain.com/Schema">
    <user>
    <lastname>Smith</lastname>
    </user>
    </Report>

    XSL: (same)

    Outputs: (null)

    How / where do I add the namespace reference in the XSL to make this
    work?

    Thanks
     
    , Apr 22, 2005
    #1
    1. Advertising

  2. writes:

    > OK, I hate to ask, but I seem to have run across one of those issues
    > where there answer is so grossly obvious, I can't see it.


    [details and examples snipped.]

    > Scenario 2: (adding a namespace)
    > ...
    > How / where do I add the namespace reference in the XSL to make this
    > work?
    >
    > Thanks


    Two places: (1) somewhere in the document, declare a prefix
    for the namespace http://schema.somedomain.com/Schema, and
    (2) in the XPath expression, use that prefix for the
    elements in that namespace.

    <xsl:stylesheet version="1.0"
    xmlns:x="http://schema.somedomain.com/Schema"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:eek:utput method="text" encoding="utf-8" />

    <xsl:template match="/">
    <xsl:value-of select="x:Report/x:user/x:lastname" />
    </xsl:template>
    </xsl:stylesheet>

    Don't try to use the default namespace: XPath is defined not
    to honor any declarations for the default namespace: if a
    name has no prefix, it's assumed to have no namespace.
    (Don't ask me why; I've never quite understood. I believe
    it seemed like a good idea at the time, to the responsible
    people.)

    I hope this helps.

    -C. M. Sperberg-McQueen
    World Wide Web Consortium
     
    C. M. Sperberg-McQueen, Apr 22, 2005
    #2
    1. Advertising

  3. Don't try to use the default namespace: XPath is defined not
    to honor any declarations for the default namespace: if a
    name has no prefix, it's assumed to have no namespace.
    (Don't ask me why; I've never quite understood. I believe
    it seemed like a good idea at the time, to the responsible
    people.)

    One reason is that if you bind NCNames to a namespace then it is no
    longer possible to access elements in no-namespace (as it's not possible
    to bind no-namespace to a prefix)

    Xpath2 which does allow you to specify namespaces for unprefixed
    element names and function names in Xpaths needs to put all the core
    functions in a namespace so that you can still access the core functions
    even if the default function namespace is changed. Xpath1 functions are
    in no-namespace.

    David
     
    David Carlisle, Apr 22, 2005
    #3
  4. Joe Guest

    Thank you, that worked perfectly.
     
    Joe, Apr 24, 2005
    #4
    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. Èý¹â
    Replies:
    1
    Views:
    574
    William F. Robertson, Jr.
    Jul 29, 2003
  2. Replies:
    0
    Views:
    5,137
  3. Vijay singh
    Replies:
    1
    Views:
    436
    Martin Honnen
    Nov 4, 2004
  4. Replies:
    1
    Views:
    3,613
    A. Bolmarcich
    May 27, 2005
  5. Anonymous
    Replies:
    3
    Views:
    534
    Ron Natalie
    Aug 18, 2003
Loading...

Share This Page