Grouping XML Elements

I am new to XML and am not sure how to sovle this issue.

I have an XML file that contains the following:

<TRADE>

<ABC>xxxxx</ABC>

<ABC type="U"/>

<XYZ />

<XYZ year="06"/>

<XYZ month="02"/>

<XYZ day="15"/>

</TRADE>

 

I basically want to group the elements as such:

<ABC type="U">xxxxx</ABC>

<XYZ year="06" month="02" day="15"/>

 

There are several other instances of elements being organized as such.

Thanks

Bob R.

 

 

 

 



Answer this question

Grouping XML Elements

  • justindrocco

    The stylesheet below will work for the sample input document you provided. However, I am not sure how you want to merge contents of several elements with the same name in the general case.

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <
    xsl:template match="/">
    <
    xsl:for-each select="TRADE/*">
    <
    xsl:variable name="curName" select="name()"/>
    <
    xsl:if test="not(preceding::*[name() = $curName])">
    <
    xsl:copy>
    <
    xsl:copy-of select="../*[name() = $curName]/@*"/>
    <
    xsl:copy-of select="../*[name() = $curName]/text()"/>
    </
    xsl:copy>
    </
    xsl:if>
    </
    xsl:for-each>
    </
    xsl:template>
    </
    xsl:stylesheet>

    Best regards,
    Anton


  • Pradeepvmca

    If you wish to use a XSL to convert from original XML format to another,

    look at examples in

    http://www.topxml.com/xsltStylesheets/xslt_XML_to_XML.asp

    They show you how to convert elements into Attributes.

    I am also new to XML/XSL. just completed my first XSL project with help from this forum.

    Perhaps someone else may give you more specific tips.

    Good luck.


  • Grouping XML Elements