extracting XML attributes and inserting as new elements using XSLT

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Edsel
    New Member
    • Feb 2008
    • 2

    extracting XML attributes and inserting as new elements using XSLT

    Hi all,

    Can anyone help with this? I would like to extract the attributes from a XML node and then transform them back to XML as elements using XSLT?

    <unsorted>
    <office title="Lowe Worldwide" ceo="Stephen" address ="23 Sloane Avenue" city="London" country="USA" postcode="SW3 3PP" tel="0208 999 9999" homepage="http://www.testing.com " logoCol= "10" />
    </unsorted>

    to

    <unsorted>
    <office>
    <title><![CDATA[Lowe Worldwide]]></title>
    <ceo><![CDATA[Stephen]]></ceo>
    <address><![CDATA[23 Sloane Avenue]]></address>
    <city><![CDATA[London]]></city>
    <country><![CDATA[USA]]></country>
    <postcode><![CDATA[SW3 3PP]]></postcode>
    <telephone><![CDATA[+0208 999 9999]]></telephone>
    <homepage><![CDATA[http://www.testing.com]]></homepage>
    <logoCol><![CDATA[10]]></logoCol>
    </office>
    </unsorted>
  • Edsel
    New Member
    • Feb 2008
    • 2

    #2
    Originally posted by Edsel
    Hi all,

    Can anyone help with this? I would like to extract the attributes from a XML node and then transform them back to XML as elements using XSLT?

    <unsorted>
    <office title="Lowe Worldwide" ceo="Stephen" address ="23 Sloane Avenue" city="London" country="USA" postcode="SW3 3PP" tel="0208 999 9999" homepage="http://www.testing.com " logoCol= "10" />
    </unsorted>

    to

    <unsorted>
    <office>
    <title><![CDATA[Lowe Worldwide]]></title>
    <ceo><![CDATA[Stephen]]></ceo>
    <address><![CDATA[23 Sloane Avenue]]></address>
    <city><![CDATA[London]]></city>
    <country><![CDATA[USA]]></country>
    <postcode><![CDATA[SW3 3PP]]></postcode>
    <telephone><![CDATA[+0208 999 9999]]></telephone>
    <homepage><![CDATA[http://www.testing.com]]></homepage>
    <logoCol><![CDATA[10]]></logoCol>
    </office>
    </unsorted>
    Thanks guys, I have actually sort it out now. Use the following if you need an answer:

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <xsl:styleshe et xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:output indent="yes" />
    <xsl:strip-space elements="*" />
    <xsl:template match="*">
    <xsl:copy>
    <xsl:if test="//office">
    <xsl:for-each select="@*">
    <xsl:element name="{name()}" >
    <xsl:value-of select="." />
    </xsl:element>
    </xsl:for-each>
    </xsl:if>
    <xsl:apply-templates />
    </xsl:copy>
    </xsl:template>
    </xsl:stylesheet>

    Comment

    Working...