HELP - can't add namespace using XSL

Discussion in 'XML' started by n.phelge, Sep 17, 2005.

  1. n.phelge

    n.phelge Guest

    I'm trying to validate XML which doesn't include a namespace, so I've
    written a schema and I'm attempting to add a namespace to the original
    XML using XSLT. The original XML looks like this:

    <orderlist>
    <order>
    <header>
    <ordernum>1313</ordernum>
    </header>
    </order>
    <order>
    <header>
    <ordernum>1314</ordernum>
    </header>
    </order>
    </orderlist>

    and I would like to be transformed to the following:

    <orderlist xmlns="urn:MyOrder-schema">
    <order>
    <header>
    <ordernum>1313</ordernum>
    </header>
    </order>
    <order>
    <header>
    <ordernum>1314</ordernum>
    </header>
    </order>
    </orderlist>

    However, every approach I've taken to writing an XSLT to do this
    results in a blank namespace for the next-level elements, such as the
    following:

    <orderlist xmlns="urn:MyOrder-schema">
    <order xmlns="">
    <header>
    <ordernum>1313</ordernum>
    </header>
    </order>
    <order xmlns="">
    <header>
    <ordernum>1314</ordernum>
    </header>
    </order>
    </orderlist>

    This empty namespace results in a failed validation. Is there an
    approach to adding a namespace to the top-most element of an XML
    fragment using XSL such that the namespace of the next-level elements
    aren't affected? I really need to be able to validate this XML, so any
    help is greatly appreciated.

    Thanks in advance
     
    n.phelge, Sep 17, 2005
    #1
    1. Advertising

  2. n.phelge wrote:

    > I'm trying to validate XML which doesn't include a namespace, so I've
    > written a schema and I'm attempting to add a namespace to the original
    > XML using XSLT. The original XML looks like this:
    >
    > <orderlist>
    > <order>
    > <header>
    > <ordernum>1313</ordernum>
    > </header>
    > </order>
    > <order>
    > <header>
    > <ordernum>1314</ordernum>
    > </header>
    > </order>
    > </orderlist>
    >
    > and I would like to be transformed to the following:
    >
    > <orderlist xmlns="urn:MyOrder-schema">
    > <order>
    > <header>
    > <ordernum>1313</ordernum>
    > </header>
    > </order>
    > <order>
    > <header>
    > <ordernum>1314</ordernum>
    > </header>
    > </order>
    > </orderlist>


    Should simply work with

    <xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0"
    xmlns="urn:MyOrder-schema">


    <xsl:template match="*">
    <xsl:element name="{local-name()}">
    <xsl:apply-templates select="@* | node()" />
    </xsl:element>
    </xsl:template>

    where you then need to decide how you want to transform attributes, if
    you want to simply copy them you could add

    <xsl:template match="@*">
    <xsl:copy-of select="." />
    </xsl:template>

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Sep 17, 2005
    #2
    1. Advertising

  3. n.phelge

    Joris Gillis Guest

    Tempore 15:32:19, die Saturday 17 September 2005 AD, hinc in foro {comp.text.xml} scripsit n.phelge <>:

    > Is there an
    > approach to adding a namespace to the top-most element of an XML
    > fragment using XSL such that the namespace of the next-level elements
    > aren't affected?


    Yes, this is exactly what happens in your example.
    The 'xmlns=""' isn't added to the elements; it was already there, by default, hidden.


    What you need to do is change the namespace of each and every node in the tree. An identity transform is what you need:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:eek:utput method="xml" indent="yes"/>

    <xsl:template match="/">
    <xsl:apply-templates select="node()" mode="put_in_new_namespace">
    <xsl:with-param name="new_namespace">urn:MyOrder-schema</xsl:with-param>
    </xsl:apply-templates>
    </xsl:template>

    <xsl:template match="*" mode="put_in_new_namespace" priority="1">
    <xsl:param name="new_namespace"/>
    <xsl:element name="{local-name()}" namespace="{$new_namespace}">
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates select="node()" mode="put_in_new_namespace">
    <xsl:with-param name="new_namespace" select="$new_namespace"/>
    </xsl:apply-templates>
    </xsl:element>
    </xsl:template>

    <xsl:template match="node()" mode="put_in_new_namespace">
    <xsl:copy-of select="."/>
    </xsl:template>

    </xsl:stylesheet>


    regards,
    --
    Joris Gillis (http://users.telenet.be/root-jg/me.html)
    Don't send spam. I don't like it and it is illegal.
     
    Joris Gillis, Sep 17, 2005
    #3
  4. n.phelge

    Joris Gillis Guest

    Tempore 15:42:34, die Saturday 17 September 2005 AD, hinc in foro {comp.text.xml} scripsit Martin Honnen <>:

    > Should simply work with
    >
    > <xsl:stylesheet
    > xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    > version="1.0"
    > xmlns="urn:MyOrder-schema">
    >
    >
    > <xsl:template match="*">
    > <xsl:element name="{local-name()}">
    > <xsl:apply-templates select="@* | node()" />
    > </xsl:element>
    > </xsl:template>


    Hey, that's clever... never thought that would work;-)

    regards,
    --
    Joris Gillis (http://users.telenet.be/root-jg/me.html)
    Don't send spam. I don't like it and it is illegal.
     
    Joris Gillis, Sep 17, 2005
    #4
  5. n.phelge

    n.phelge Guest

    Thanks for the quick response, guys! I knew there had to be a
    straightforward way of doing it.
     
    n.phelge, Sep 17, 2005
    #5
    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:
    573
    William F. Robertson, Jr.
    Jul 29, 2003
  2. Replies:
    1
    Views:
    3,613
    A. Bolmarcich
    May 27, 2005
  3. Replies:
    5
    Views:
    615
  4. Peng Yu
    Replies:
    0
    Views:
    644
    Peng Yu
    Sep 14, 2008
  5. Deep Mehta via .NET 247
    Replies:
    2
    Views:
    428
    Dave A
    May 31, 2005
Loading...

Share This Page