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

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

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

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. Wolfgang Lipp
    Replies:
    1
    Views:
    549
    Patrick TJ McPhee
    Jan 30, 2004
  2. Wolfgang Lipp
    Replies:
    0
    Views:
    632
    Wolfgang Lipp
    Jan 28, 2004
  3. Hugh Sparks
    Replies:
    8
    Views:
    730
    Hugh Sparks
    Jul 30, 2004
  4. Replies:
    0
    Views:
    618
  5. Jyrki Keisala

    Elements within elements

    Jyrki Keisala, Jun 8, 2005, in forum: XML
    Replies:
    5
    Views:
    636
    William Park
    Jun 15, 2005
  6. Replies:
    2
    Views:
    641
  7. Gerald Aichholzer
    Replies:
    2
    Views:
    3,186
    Gerald Aichholzer
    Jun 27, 2006
  8. Replies:
    2
    Views:
    550
    Martin Honnen
    Feb 11, 2007
Loading...