XSLT - Re-arrange the XML element order

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

    XSLT - Re-arrange the XML element order

    Hi all,

    I would like to know that is it possible to move xml element up or down in element tree.

    The input xml.

    <ROOT>
    <A1>A</A1>
    <B1>B</B1>
    <C1>C</C1>
    <D1>D</D1>
    </ROOT>

    The required output xml.

    <ROOT>
    <A1>A</A1>
    <D1>D</D1>
    <B1>B</B1>
    <C1>C</C1>
    </ROOT>

    Can anyone provide me some XSLT example to do this such of thing? Thank you for any help in advance.
  • delirio
    New Member
    • Feb 2008
    • 2

    #2
    Great question, oftem people need to reorganize their XML for further processing, and this is a great oportunity to clarify available techniques.

    You can achieve what you're asking simply by using xsl:copy and/or copy-of, although I doubt your goal in practice is as simple as the example you've posted in this thread.

    So, if you need to restructure the children of your root element and you're confident about the simplicity of the XML you're trying to restructure, then you'd do something like this:

    [HTML]<xsl:template match="/ROOT">
    <xsl:copy>
    <xsl:copy-of select="A1"/>
    <xsl:copy-of select="D1"/>
    <xsl:copy-of select="B1"/>
    <xsl:copy-of select="C1"/>
    </xsl:copy>
    </xsl:template>[/HTML]

    The above code will output what you've asked for. Now, if you have a much larger document, and what you're really after is mooving some of the nodes around while maintaining an abtract hierearchy, you could use the technique in example code below.

    [HTML]<xsl:styleshe et xmlns:xsl="http ://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:strip-space elements="*"/>

    <xsl:template match="*">
    <xsl:apply-templates select="self::* " mode="copy"/>
    </xsl:template>

    <xsl:template match="A1">
    <xsl:apply-templates select="self::* " mode="copy"/>
    <xsl:apply-templates select="../D1" mode="copy"/>
    </xsl:template>


    <xsl:template match="B1">
    <xsl:apply-templates select="self::* " mode="copy"/>
    <xsl:apply-templates select="../C1" mode="copy"/>
    </xsl:template>

    <xsl:template match="D1"/>
    <xsl:template match="C1"/>

    <xsl:template match="*" mode="copy">
    <xsl:copy>
    <xsl:apply-templates/>
    </xsl:copy>
    </xsl:template>

    <xsl:template match="text()">
    <xsl:value-of select="."/>
    </xsl:template>
    </xsl:stylesheet>[/HTML]

    In this case you are copying an abstract document structure and repositioning some of the elements in strategic places.

    Hope this helps.

    This is a great topic so I'll be posting the content on my blog so more people can benefit from this example.
    XSLT By Example

    Miguel de Melo
    =============== =============
    XSLT By Example

    Comment

    • daydreaming
      New Member
      • Jan 2008
      • 12

      #3
      Thank you so much for your help, Miguel.

      Comment

      Working...