xslt and namespaces

Discussion in 'XML' started by S2, Nov 5, 2010.

  1. S2

    S2 Guest

    hello all, i can't explain this... can someone help me?

    this is my xml:

    <hello>
    <one>
    <paragraph>p1</paragraph>
    </one>
    </hello>


    i'd like to change the <hello> element: i want to give it a namespace
    and add an atribute. what i want is this:


    -------------
    <x:hello y:an_attribute="a value for an_attribute"
    xmlns:x="some_new_namespace" xmlns:y="other_ns">
    <one>
    <paragraph>p1</paragraph>
    </one>
    </x:hello>
    -------------


    the stylesheet i am using is

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

    <xsl:param name="element_localname" />

    <xsl:template match="node()">
    <xsl:choose>
    <xsl:when test="local-name() = $element_localname">
    <xsl:element name="{$element_localname}"
    namespace="some_new_namespace">
    <xsl:attribute name="an_attribute" namespace="other_ns">a
    value for an_attribute</xsl:attribute>
    <xsl:apply-templates select="node()"/>
    </xsl:element>
    </xsl:when>

    <!-- copy the rest as is -->
    <xsl:eek:therwise>
    <xsl:copy>
    <xsl:apply-templates select="node()" />
    </xsl:copy>
    </xsl:eek:therwise>
    </xsl:choose>
    </xsl:template>

    </xsl:stylesheet>
    -------------


    but what i get is

    -------------
    <?xml version="1.0" encoding="UTF-8"?>
    <ns0:hello xmlns:ns0="other_ns" ns0:an_attribute="a value for an_attribute">
    <one>
    <paragraph>p1</paragraph>
    </one>
    </ns0:hello>
    -------------

    can someone explain why?

    thanks!
    S2, Nov 5, 2010
    #1
    1. Advertising

  2. S2 wrote:
    > hello all, i can't explain this... can someone help me?
    >
    > this is my xml:
    >
    > <hello>
    > <one>
    > <paragraph>p1</paragraph>
    > </one>
    > </hello>
    >
    >
    > i'd like to change the <hello> element: i want to give it a namespace
    > and add an atribute. what i want is this:
    >
    >
    > -------------
    > <x:hello y:an_attribute="a value for an_attribute"
    > xmlns:x="some_new_namespace" xmlns:y="other_ns">
    > <one>
    > <paragraph>p1</paragraph>
    > </one>
    > </x:hello>
    > -------------
    >
    >
    > the stylesheet i am using is
    >
    > -------------
    > <xsl:stylesheet version="1.0"
    > xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    > <xsl:eek:utput method="xml" indent="yes"/>
    >
    > <xsl:param name="element_localname" />
    >
    > <xsl:template match="node()">
    > <xsl:choose>
    > <xsl:when test="local-name() = $element_localname">
    > <xsl:element name="{$element_localname}"
    > namespace="some_new_namespace">
    > <xsl:attribute name="an_attribute" namespace="other_ns">a
    > value for an_attribute</xsl:attribute>
    > <xsl:apply-templates select="node()"/>
    > </xsl:element>
    > </xsl:when>
    >
    > <!-- copy the rest as is -->
    > <xsl:eek:therwise>
    > <xsl:copy>
    > <xsl:apply-templates select="node()" />
    > </xsl:copy>
    > </xsl:eek:therwise>
    > </xsl:choose>
    > </xsl:template>
    >
    > </xsl:stylesheet>
    > -------------
    >
    >
    > but what i get is
    >
    > -------------
    > <?xml version="1.0" encoding="UTF-8"?>
    > <ns0:hello xmlns:ns0="other_ns" ns0:an_attribute="a value for
    > an_attribute">
    > <one>
    > <paragraph>p1</paragraph>
    > </one>
    > </ns0:hello>
    > -------------
    >
    > can someone explain why?


    Well which XSLT processor do you use?
    I tried your sample with Saxon 6.5.5 and the output is

    <hello xmlns="some_new_namespace" xmlns:ns0="other_ns"
    ns0:an_attribute="a value for an_attribute">

    <one xmlns="">

    <paragraph>p1</paragraph>

    </one>

    </hello>




    --

    Martin Honnen
    http://msmvps.com/blogs/martin_honnen/
    Martin Honnen, Nov 5, 2010
    #2
    1. Advertising

  3. S2

    S2 Guest

    On 05.11.2010 14.41, Martin Honnen wrote:
    > S2 wrote:
    >> hello all, i can't explain this... can someone help me?
    >>
    >> this is my xml:
    >>
    >> <hello>
    >> <one>
    >> <paragraph>p1</paragraph>
    >> </one>
    >> </hello>
    >>
    >>
    >> i'd like to change the <hello> element: i want to give it a namespace
    >> and add an atribute. what i want is this:
    >>
    >>
    >> -------------
    >> <x:hello y:an_attribute="a value for an_attribute"
    >> xmlns:x="some_new_namespace" xmlns:y="other_ns">
    >> <one>
    >> <paragraph>p1</paragraph>
    >> </one>
    >> </x:hello>
    >> -------------
    >>
    >>
    >> the stylesheet i am using is
    >>
    >> -------------
    >> <xsl:stylesheet version="1.0"
    >> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    >> <xsl:eek:utput method="xml" indent="yes"/>
    >>
    >> <xsl:param name="element_localname" />
    >>
    >> <xsl:template match="node()">
    >> <xsl:choose>
    >> <xsl:when test="local-name() = $element_localname">
    >> <xsl:element name="{$element_localname}" namespace="some_new_namespace">
    >> <xsl:attribute name="an_attribute" namespace="other_ns">a value for
    >> an_attribute</xsl:attribute>
    >> <xsl:apply-templates select="node()"/>
    >> </xsl:element>
    >> </xsl:when>
    >>
    >> <!-- copy the rest as is -->
    >> <xsl:eek:therwise>
    >> <xsl:copy>
    >> <xsl:apply-templates select="node()" />
    >> </xsl:copy>
    >> </xsl:eek:therwise>
    >> </xsl:choose>
    >> </xsl:template>
    >>
    >> </xsl:stylesheet>
    >> -------------
    >>
    >>
    >> but what i get is
    >>
    >> -------------
    >> <?xml version="1.0" encoding="UTF-8"?>
    >> <ns0:hello xmlns:ns0="other_ns" ns0:an_attribute="a value for
    >> an_attribute">
    >> <one>
    >> <paragraph>p1</paragraph>
    >> </one>
    >> </ns0:hello>
    >> -------------
    >>
    >> can someone explain why?

    >
    > Well which XSLT processor do you use?
    > I tried your sample with Saxon 6.5.5 and the output is
    >
    > <hello xmlns="some_new_namespace" xmlns:ns0="other_ns"
    > ns0:an_attribute="a value for an_attribute">
    >
    > <one xmlns="">
    >
    > <paragraph>p1</paragraph>
    >
    > </one>
    >
    > </hello>


    that's the output i want. i am using the eclipse wtp tool. have no idea
    witch implementation that uses. so, changing xslt tool will solve it.
    thanks!
    S2, Nov 8, 2010
    #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. Stylus Studio
    Replies:
    0
    Views:
    658
    Stylus Studio
    Aug 3, 2004
  2. Colin Fox

    XML & XSLT namespaces

    Colin Fox, Jan 11, 2004, in forum: XML
    Replies:
    2
    Views:
    444
    Colin Fox
    Jan 11, 2004
  3. daz_oldham
    Replies:
    2
    Views:
    631
    daz_oldham
    May 23, 2006
  4. Thomas Wittek

    XSLT: Normalizing namespaces

    Thomas Wittek, Aug 30, 2007, in forum: XML
    Replies:
    5
    Views:
    1,174
    Martin Honnen
    Aug 31, 2007
  5. Andy Fish

    namespaces in XSLT

    Andy Fish, Feb 29, 2008, in forum: XML
    Replies:
    3
    Views:
    522
    Joseph Kesselman
    Feb 29, 2008
Loading...

Share This Page