Michael Boal
Michael Boal

Reputation: 3

XSLT-1.0: How to sort a child node group and keep its hierarchy

I've spent so much time on this and thought I would reach out for a possible solution. This is my XML that I have simplified for testing.

<Data>
    <Superbill>
        <Attributes>
        </Attributes>
        <Children>
            <ListByMatter>
                <ListByMatter>
                    <Children>
                        <ListofProfDetailTime>
                            <ListofProfDetailTime>
                                <Attributes>
                                    <id>1</id>
                                    <Date>2011-02-11</Date>
                                </Attributes>
                            </ListofProfDetailTime>
                            <ListofProfDetailTime>
                                <Attributes>
                                    <id>2</id>
                                    <Date>2010-11-02</Date>
                                </Attributes>
                            </ListofProfDetailTime>
                            <ListofProfDetailTime>
                                <Attributes>
                                    <id>11</id>
                                    <Date>2015-09-12</Date>
                                </Attributes>
                            </ListofProfDetailTime>
                            <ListofProfDetailTime>
                                <Attributes>  
                                    <id>1</id>
                                    <Date>2013-11-12</Date>
                                </Attributes>
                            </ListofProfDetailTime>
                        </ListofProfDetailTime>
                    </Children>
                </ListByMatter>
            </ListByMatter>
        </Children>
    </Superbill>
</Data>

I have written this below for my transformation but can't figure out how to replace the old nodes from my ordered results I'm getting in the original hierarchy. I'm using this website http://xsltransform.net/ to quickly do changes but I'm sure I'm missing something simple or it's going to be way more complicated than expected. I've just starting learning this recently by myself and need some guidance.

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
    <xsl:strip-space elements="*"/>

    <xsl:template match="/">
      <xsl:copy>
        <xsl:apply-templates select="//ListofProfDetailTime/ListofProfDetailTime">
          <xsl:sort select="Attributes/Date" order="ascending"/>
        </xsl:apply-templates>
      </xsl:copy>
   </xsl:template>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates />
    </xsl:copy>
  </xsl:template>

</xsl:transform>

Upvotes: 0

Views: 723

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 116992

Not sure what exactly your question is. If you're trying to preserve the original hierarchy, and only change the sort order of the inner ListofProfDetailTime elements, then change this:

    <xsl:template match="/">
      <xsl:copy>
        <xsl:apply-templates select="//ListofProfDetailTime/ListofProfDetailTime">
          <xsl:sort select="Attributes/Date" order="ascending"/>
        </xsl:apply-templates>
      </xsl:copy>
   </xsl:template>

to:

<xsl:template match="Children/ListofProfDetailTime">
    <xsl:copy>
        <xsl:apply-templates select="ListofProfDetailTime">
            <xsl:sort select="Attributes/Date" order="ascending"/>
        </xsl:apply-templates>
    </xsl:copy>
</xsl:template>

The way you have it now, your template matches the / root node and from there applies templates directly to the ListofProfDetailTime descendants - so that the intermediate levels are never processed.

Upvotes: 1

Related Questions