Accessing elements with namespaces

Discussion in 'XML' started by johkar, Mar 13, 2008.

  1. johkar

    johkar Guest

    Given that all the nodes in my XML document have the "dsml" namespace,
    what is the XSL syntax to access the elements or apply templates?
    Below is a snippet:

    <dsml:batchResponse xmlns:dsml="urn:eek:asis:names:tc:DSML:2:0:core"
    xmlns="urn:eek:asis:names:tc:DSML:2:0:core" xmlns:xsd="http://www.w3.org/
    2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <dsml:searchResponse>
    <dsml:searchResultEntry>
    <dsml:attr name="mail">
    <dsml:value></dsml:value>
    </dsml:attr>
    </dsml:searchResultEntry>
    </dsml:batchResponse>
    johkar, Mar 13, 2008
    #1
    1. Advertising

  2. johkar wrote:
    > Given that all the nodes in my XML document have the "dsml" namespace,
    > what is the XSL syntax to access the elements or apply templates?
    > Below is a snippet:
    >
    > <dsml:batchResponse xmlns:dsml="urn:eek:asis:names:tc:DSML:2:0:core"
    > xmlns="urn:eek:asis:names:tc:DSML:2:0:core" xmlns:xsd="http://www.w3.org/
    > 2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    > <dsml:searchResponse>
    > <dsml:searchResultEntry>
    > <dsml:attr name="mail">
    > <dsml:value></dsml:value>
    > </dsml:attr>
    > </dsml:searchResultEntry>
    > </dsml:batchResponse>


    In the XSLT stylesheet you need to bind a prefix to the namespace URI
    and use that prefix in XPath expressions and match patterns. Often the
    same prefix as in the XML input is used but that is not necessary.
    <xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0"
    xmlns:dsml="urn:eek:asis:names:tc:DSML:2:0:core">

    <xsl:template match="dsml:batchResponse">
    ...
    </xsl:template>

    </xsl:stylesheet>

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Mar 13, 2008
    #2
    1. Advertising

  3. johkar wrote:
    > Given that all the nodes in my XML document have the "dsml" namespace,
    > what is the XSL syntax to access the elements or apply templates?


    Define a prefix in your stylesheet which maps to that namespace, and use
    that prefix in your XPaths, like so:

    <xsl:stylesheet ... xmlns:dsml="urn:eek:asis:names:tc:DSML:2:0:core" ...>
    ...
    <xsl:template match="dsml:batchResponse">
    <xsl:apply-templates select="//dsml:value"/>
    </xsl:template>
    ...

    <xsl:stylesheet>

    and so on.

    Standard pointer to the many tutorials/articles on
    http://www.ibm.com/xml, since this is a question that any decent XSLT
    tutorial should have answered.

    --
    Joe Kesselman / Beware the fury of a patient man. -- John Dryden
    Joseph Kesselman, Mar 13, 2008
    #3
  4. johkar

    johkar Guest

    On Mar 13, 10:38 am, Martin Honnen <> wrote:
    > johkar wrote:
    > > Given that all the nodes in my XML document have the "dsml" namespace,
    > > what is the XSL syntax to access the elements or apply templates?
    > > Below is a snippet:

    >
    > > <dsml:batchResponse xmlns:dsml="urn:eek:asis:names:tc:DSML:2:0:core"
    > > xmlns="urn:eek:asis:names:tc:DSML:2:0:core" xmlns:xsd="http://www.w3.org/
    > > 2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    > >    <dsml:searchResponse>
    > > <dsml:searchResultEntry>
    > >      <dsml:attr name="mail">
    > >          <dsml:value></dsml:value>
    > >      </dsml:attr>
    > > </dsml:searchResultEntry>
    > > </dsml:batchResponse>

    >
    > In the XSLT stylesheet you need to bind a prefix to the namespace URI
    > and use that prefix in XPath expressions and match patterns. Often the
    > same prefix as in the XML input is used but that is not necessary.
    >    <xsl:stylesheet
    >      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    >      version="1.0"
    >      xmlns:dsml="urn:eek:asis:names:tc:DSML:2:0:core">
    >
    >      <xsl:template match="dsml:batchResponse">
    >         ...
    >      </xsl:template>
    >
    >    </xsl:stylesheet>


    Thanks very much...and quick too.
    johkar, Mar 13, 2008
    #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. Hugh Sparks
    Replies:
    8
    Views:
    542
    Hugh Sparks
    Jul 30, 2004
  2. Replies:
    0
    Views:
    515
  3. Replies:
    2
    Views:
    530
  4. Gerald Aichholzer
    Replies:
    2
    Views:
    2,482
    Gerald Aichholzer
    Jun 27, 2006
  5. Replies:
    2
    Views:
    437
    Martin Honnen
    Feb 11, 2007
Loading...

Share This Page