Home > Enterprise >  Sorting Remaining XML Nodes after Processing the First Node
Sorting Remaining XML Nodes after Processing the First Node

Time:08-19

My XML data comes from a Civil Engineering Program. The first Alignment node will always be the node that needs to be processed first using one apply-template. I am reverse sorting the /Project/Alignment/Elements/Line nodes on the value of /Project/Alignment/Elements/Line/Start/Reference/stat/@internalStat. This is working as desired. The remaining Alignment nodes all get the same set of apply-template. But the order of these will not be in any particular order. I am trying to sort the Alignments in reverse order again based upon the value of /Project/Alignment/Elements/Line/Start/Reference/stat/@internalStat which is always in the first Alignment/Elements/Line of these Alignment nodes. I am not trying to sort the Element/Line nodes.

<?xml version="1.0" encoding="iso-8859-1" ?>
<Project name="1HanoverRd-XS-BB" lastRevisedDate="8/10/2022 2:58:03 PM" lastRevisedBy="caddc">
    <Alignment name="1" style="HA-BLIN-TC" lastRevisedDate="2/9/2022 11:19:02 AM" lastRevisedBy="bman" isParent="true">
        <Elements>
            <Line style="HA-BLIN-TC" length="337.988884834" direction="0.559501706945" elementNumber="1">
                <Start name="1000">
                    <stat internalStat="0.00"/>
                    <Reference referenceAlignment="1" offset="0.00">
                        <stat internalStat="0.00"/>
                    </Reference>
                </Start>
                <End name="1001">
                    <stat internalStat="337.988884834"/>
                    <Reference referenceAlignment="1" offset="0.00">
                        <stat internalStat="337.988884834"/>
                    </Reference>
                </End>
            </Line>
            <Line style="HA-BLIN-TC" length="336.238764501" direction="0.549311892543" elementNumber="2">
                <Start name="1001">
                    <stat internalStat="337.988884834"/>
                    <Reference referenceAlignment="1" offset="0.00">
                        <stat internalStat="337.988884834"/>
                    </Reference>
                </Start>
                <End name="1002">
                    <stat internalStat="674.227649335"/>
                    <Reference referenceAlignment="1" offset="0.00">
                        <stat internalStat="674.227649335"/>
                    </Reference>
                </End>
            </Line>
        </Elements>
    </Alignment>
    <Alignment name="PGL-40" style="TO-PGL" lastRevisedDate="2/11/2022 1:49:36 PM" lastRevisedBy="caddc">
        <Elements>
            <Line style="TO-PGL" length="11.437230886" direction="2.210057009471" elementNumber="1">
                <Start name="10059">
                    <stat internalStat="0.00"/>
                    <legalReference referenceAlignment="1" offset="0.00">
                        <stat internalStat="337.988884834"/>
                    </legalReference>
                </Start>
                <End name="10060">
                    <stat internalStat="11.437230886"/>
                </End>
            </Line>
            <Line style="TO-PGL" length="5.508728998" direction="2.203594650521" elementNumber="2">
                <Start name="10060">
                    <stat internalStat="11.437230886"/>
                </Start>
                <End name="10061">
                    <stat internalStat="16.945959883"/>
                </End>
            </Line>
            <Line style="TO-PGL" length="6.873889519" direction="2.137758414137" elementNumber="3">
                <Start name="10061">
                    <stat internalStat="16.945959883"/>
                </Start>
                <End name="10062">
                    <stat internalStat="23.819849403"/>
                </End>
            </Line>
            <Line style="TO-PGL" length="0.473255186" direction="3.127941832720" elementNumber="4">
                <Start name="10062">
                    <stat internalStat="23.819849403"/>
                </Start>
                <End name="10063">
                    <stat internalStat="24.293104589"/>
                </End>
            </Line>
        </Elements>
    </Alignment>
    <Alignment name="PGL" style="TO-PGL" lastRevisedDate="2/11/2022 1:49:36 PM" lastRevisedBy="caddc">
        <Elements>
            <Line style="TO-PGL" length="28.413105073" direction="2.280335519518" elementNumber="1">
                <Start name="2039">
                    <stat internalStat="0.00"/>
                    <Reference referenceAlignment="1" offset="-0.000000993">
                        <stat internalStat="1874.641459042"/>
                    </Reference>
                </Start>
                <End name="2040">
                    <stat internalStat="28.413105073"/>
                </End>
            </Line>
            <Line style="TO-PGL" length="6.614064212" direction="2.273069807099" elementNumber="2">
                <Start name="2040">
                    <stat internalStat="28.413105073"/>
                </Start>
                <End name="2041">
                    <stat internalStat="35.027169286"/>
                </End>
            </Line>
            <Line style="TO-PGL" length="0.176436818" direction="3.659009435956" elementNumber="3">
                <Start name="2041">
                    <stat internalStat="35.027169286"/>
                </Start>
                <End name="2042">
                    <stat internalStat="35.203606104" externalStat="35.203606104" externalStatName=""/>
                </End>
            </Line>
        </Elements>
    </Alignment>
</Project>

My stylesheet uses the following.

<xsl:for-each select="HorizontalAlignment">
    <xsl:choose>
        <xsl:when test="position()=1">
            <xsl:apply-templates mode="reach">
                <xsl:sort select="descendant-or-self::*/HorizontalElements/Start/station/@externalStation" data-type="number" order="descending"/>
            </xsl:apply-templates>
        </xsl:when>
        <xsl:otherwise>
            <xsl:for-each select="HorizontalElements">
                <xsl:apply-templates mode="surfaceLine"/>
            </xsl:for-each>
        </xsl:otherwise>
    </xsl:choose>
</xsl:for-each>

Everything I have tried to use for sorting in the otherwise block either does nothing or causes the transformation to fail to create the output HTML document.

I hope someone can help. Sorry the example XML is larger than most, but I wanted to include enough data tp present the issue. I eliminated many nodes and attributes from the original documents and truncated a number of the node names.

CodePudding user response:

As noted in the comments, this is quite confusing. Perhaps the following minimal example can help you get on the right track:

XML

<Project>
    <Alignment>
        <Elements>
            <Line>
                <Start>
                    <Reference>
                        <stat internalStat="1.01"/>
                    </Reference>
                </Start>
            </Line>
            <Line>
                <Start>
                    <Reference>
                        <stat internalStat="1.02"/>
                    </Reference>
                </Start>
            </Line>
        </Elements>
    </Alignment>
    <Alignment>
        <Elements>
            <Line>
                <Start>
                    <Reference>
                        <stat internalStat="2.01"/>
                    </Reference>
                </Start>
            </Line>
            <Line>
                <Start/>
            </Line>
            <Line>
                <Start/>
            </Line>
            <Line>
                <Start/>
            </Line>
        </Elements>
    </Alignment>
    <Alignment>
        <Elements>
            <Line>
                <Start>
                    <Reference>
                        <stat internalStat="3.01"/>
                    </Reference>
                </Start>
            </Line>
            <Line>
                <Start/>
            </Line>
            <Line>
                <Start/>
            </Line>
        </Elements>
    </Alignment>
</Project>

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/Project">
    <output>
        <part1>
            <!-- sort the Lines of the first Alignment -->
            <xsl:for-each select="Alignment[1]/Elements/Line">
                <xsl:sort select="Start/Reference/stat/@internalStat" data-type="number" order="descending"/>
                <line internalStat="{Start/Reference/stat/@internalStat}"/>
            </xsl:for-each>
        </part1>
        <part2>
            <!-- sort the other Alignments by the fist Line's data  -->
            <xsl:for-each select="Alignment[position() > 1]">
                <xsl:sort select="Elements/Line[1]/Start/Reference/stat/@internalStat" data-type="number" order="descending"/>
                <alignment internalStat="{Elements/Line[1]/Start/Reference/stat/@internalStat}"/>
            </xsl:for-each>
        </part2>
    </output>
</xsl:template>

</xsl:stylesheet>

Result

<?xml version="1.0" encoding="UTF-8"?>
<output>
    <part1>
        <line internalStat="1.02"/>
        <line internalStat="1.01"/>
    </part1>
    <part2>
        <alignment internalStat="3.01"/>
        <alignment internalStat="2.01"/>
    </part2>
</output>
  • Related