简体   繁体   中英

Sort possible on nested structure (xslt1.0)?

Did research here sort via xpath and here Grouping using the Münchian Method but couldn't find how to adapt for my situation sort on 'fieldValue' for 'Label' inside 'userField[23]', inside 'SysStructure_FIELD[146]'

This is my xslt:

 <xsl:stylesheet version="1.0" 
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
   <xsl:strip-space elements="*" /> 
   <xsl:output method="xml" indent="yes" /> 
   <xsl:template match="/Root/STRUCTURESet/STRUCTURE/userField[2]/STRUCTURE_Fields/SysStructure_FIELD">
      <xsl:copy> 
         <xsl:apply-templates select="objectId"/>
            <xsl:apply-templates select="userField">
            <xsl:sort select="fieldLabel[10]/fieldValue" data-type="text" order="ascending"/>
         </xsl:apply-templates>
      </xsl:copy>
   </xsl:template>
   <xsl:template match="@*|node()">
      <xsl:copy>
         <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
    </xsl:template>
</xsl:stylesheet> 

The desired output: to have the fieldValue's Labels sorted is not achieved. Can you help out?

xml in shortened form is below ( 'userField(3)', inside 'SysStructure_FIELD(3)' ) :

<Root version="1-1">
<STRUCTURESet>
    <STRUCTURE id="STRUCTURE_0">
        <objectId class="STRUCTURE">
            <identifier>49</identifier>
            <primarySystem>Sys</primarySystem>
            <label>Sys_Default_Structure</label>
        </objectId>
        <userField>
            <fieldLabel>Description</fieldLabel>
            <fieldValue>Sys Default structure level</fieldValue>
            <fieldType>character</fieldType>
        </userField>
        <userField>
            <fieldLabel>Fields</fieldLabel>
            <STRUCTURE_Fields>
                <SysStructure_FIELD>
                    <objectId class="SysStructure_FIELD">
                        <primarySystem>Sys</primarySystem>
                    </objectId>
                    <userField>
                        <fieldLabel>Hide</fieldLabel>
                        <fieldValue>0</fieldValue>
                        <fieldType>numeric</fieldType>
                    </userField>
                    <userField>
                        <fieldLabel>Label</fieldLabel>
                        <fieldValue>System_value</fieldValue>
                        <fieldType>character</fieldType>
                    </userField>
                    <userField>
                        <fieldLabel>MainReturn</fieldLabel>
                        <fieldValue/>
                        <fieldType>character</fieldType>
                    </userField>
                </SysStructure_FIELD>
                <SysStructure_FIELD>
                    <objectId class="SysStructure_FIELD">
                        <primarySystem>Sys</primarySystem>
                    </objectId>
                    <userField>
                        <fieldLabel>Hide</fieldLabel>
                        <fieldValue>0</fieldValue>
                        <fieldType>numeric</fieldType>
                    </userField>
                    <userField>
                        <fieldLabel>Label</fieldLabel>
                        <fieldValue>Off_Market</fieldValue>
                        <fieldType>character</fieldType>
                    </userField>
                    <userField>
                        <fieldLabel>MainReturn</fieldLabel>
                        <fieldValue/>
                        <fieldType>character</fieldType>
                    </userField>
                </SysStructure_FIELD>
                <SysStructure_FIELD>
                    <objectId class="SysStructure_FIELD">
                        <primarySystem>Sys</primarySystem>
                    </objectId>
                    <userField>
                        <fieldLabel>Hide</fieldLabel>
                        <fieldValue>0</fieldValue>
                        <fieldType>numeric</fieldType>
                    </userField>
                    <userField>
                        <fieldLabel>Label</fieldLabel>
                        <fieldValue>System_Time</fieldValue>
                        <fieldType>character</fieldType>
                    </userField>
                    <userField>
                        <fieldLabel>MainReturn</fieldLabel>
                        <fieldValue/>
                        <fieldType>character</fieldType>
                    </userField>
                </SysStructure_FIELD>
            </STRUCTURE_Fields>
        </userField>
        <userField>
            <fieldLabel>Label</fieldLabel>
            <fieldValue>Sys_Default_Structure</fieldValue>
            <fieldType>character</fieldType>
        </userField>
    </STRUCTURE>
</STRUCTURESet>

Because in the structure there is twice a userField is mentioned, you need to specify exactly what field you want to examine: userField/fieldLabel[text()='Label' instead of userField/fieldLabel[10] Furthermore you need to sort on the same level as the fieldLabel: ../fieldValue is doing the job.

So complete statement is:

    <xsl:template match="/Root/STRUCTURESet/STRUCTURE/userField[2]/STRUCTURE_Fields"> 
<xsl:copy> 
<xsl:apply-templates select="SysStructure_FIELD"> 
<xsl:sort select="userField/fieldLabel[text()='Label']/../fieldValue" /> 
</xsl:apply-templates>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM