XSLT: how to copy element without namespace?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • daydreaming
    New Member
    • Jan 2008
    • 12

    XSLT: how to copy element without namespace?

    Hi again,

    I have one more question to inquiry from you guys that if I want to want to use XSLT copying from xml containing namespace to a new xml without namespace. How can I do it. I have written XSLT for this routine, but it still did not work as what I want yet, so can anyone give me some suggestion to work through it. Thank you in advance.


    Source XML file.

    <?xml version="1.0"?>
    <order xmlns="http://test.com/OrderAdd" name="OrderAdd" >
    <orderheader>
    <name>Jame</name>
    <surname>Cool </surname>
    <product>
    <id>001</id>
    <name>book</name>
    <qty>3</qty>
    </product>
    <shiptoaddres s>
    <name>John Good</name>
    <name>IT company</name>
    </shiptoaddress>
    </orderheader>
    </order>


    Existing XSLT file.

    <?xml version="1.0" encoding="UTF-8" ?>
    <xsl:styleshe et version="1.0" xmlns:xsl="http ://www.w3.org/1999/XSL/Transform" xmlns:t="http://test.com/OrderAdd">

    <xsl:template match="t:order" >
    <gapi>
    <xsl:apply-templates/>
    </gapi>
    </xsl:template>

    <xsl:template match="t:*">
    <orderheader>
    <xsl:copy-of select="@*|node ()" />
    </orderheader>
    </xsl:template>

    </xsl:stylesheet>


    Current result.

    <?xml version="1.0" encoding="UTF-8"?>
    <gapi xmlns:t="http://test.com/OrderAdd">
    <orderheader>
    <name xmlns="http://test.com/OrderAdd">Jame</name>
    <surname xmlns="http://test.com/OrderAdd">Cool</surname>
    <product xmlns="http://test.com/OrderAdd">
    <id>001</id>
    <name>book</name>
    <qty>3</qty>
    </product>
    <shiptoaddres s xmlns="http://test.com/OrderAdd">
    <name>John Good</name>
    <name>IT company</name>
    </shiptoaddress>
    </orderheader>
    </gapi>

    how to make to the following result?

    <?xml version="1.0" encoding="UTF-8"?>
    <gapi xmlns="http://test.com/OrderAdd">
    <orderheader>
    <name>Jame</name>
    <surname>Cool </surname>
    <product>
    <id>001</id>
    <name>book</name>
    <qty>3</qty>
    </product>
    <shiptoaddres s>
    <name>John Good</name>
    <name>IT company</name>
    </shiptoaddress>
    </orderheader>
    </gapi>
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    1. In your stylesheet node add the following: exclude-result-prefixes="t"
    2. Change your recursive template to use apply-templates instead of copy-of, except for attributes which you still copy.
    3. Declare elements dynamically, using <xsl:element> and local-name() function encased in braces.

    [code=xml]
    <xsl:element name="{local-name()}">
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
    </xsl:element>
    [/code]

    Comment

    • daydreaming
      New Member
      • Jan 2008
      • 12

      #3
      Thank you very much, jkmyoung.

      Comment

      Working...