Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xslt sort numbers

I'm trying to sort some number but I'm having someproblem with the sort.

  1. The rep_Tool/@ToolStnNo is a node that give me a number of a tool in any order.

For example:

    ...
    <ToolStnNo>
    10
    </ToolStnNo>
    ...
    <ToolStnNo>
    12
    </ToolStnNo>
    ...
    <ToolStnNo>
    3
    </ToolStnNo>
    ...
    <ToolStnNo>
    11
    </ToolStnNo>
    ...
    <ToolStnNo>
    2
    </ToolStnNo>
    ...
    <ToolStnNo>
    4
    </ToolStnNo>
    ...
    <ToolStnNo>
    1
    </ToolStnNo>
    ...
    <ToolStnNo>
    6
    </ToolStnNo>
    ...
    <ToolStnNo>
    5
    </ToolStnNo>
    ...
    <ToolStnNo>
    8
    </ToolStnNo>
    ...
    <ToolStnNo>
    7
    </ToolStnNo>
    ...

The result table that I'm getting is:

10   --- > This 10 dont be here
1
2
3
4
5
6
7
8
11
12
13

And the right is:

1
2
3
4
5
6
7
8
10   --- > This 10 should be here
11
12
13
    <xsl:for-each select="MillSetupSheetAttr">
        <xsl:for-each select="MillOperation"> 
            <xsl:sort select="rep_Tool/@ToolStnNo" data-type="number"/>
            <tr>
                <td><font><xsl:value-of select="rep_Tool/@ToolStnNo"/></font></td>
            </tr>
        </xsl:for-each>
    </xsl:for-each>

Note: the actual structure of the relevant part of the xml document is:

<MillSetupSheetAttr>
    <MillOperation>
        <rep_Tool ToolStnNo="10" .../>
    </MillOperation>
    <MillOperation>
        <rep_Tool ToolStnNo="1" .../>
    </MillOperation>
    <MillOperation>
        <rep_Tool ToolStnNo="2" .../>
    </MillOperation>
</MillSetupSheetAttr>   
like image 550
Bruno Dias Vasconcelos Avatar asked Aug 05 '26 12:08

Bruno Dias Vasconcelos


1 Answers

The XML you have shown in your question is not representative of your actual XML (and it is important to show a representative example in your question, because otherwise when the files disappear from drop-box, the context of this question could become lost).

This would be more representative of your XML:

<Data>
  <MillSetupSheetAttr>
    <MillOperation>
        <rep_Tool ToolStnNo="10" .../>
    </MillOperation>
    <MillOperation>
        <rep_Tool ToolStnNo="1" .../>
    </MillOperation>
  </MillSetupSheetAttr>  
  <MillSetupSheetAttr>
    <MillOperation>
        <rep_Tool ToolStnNo="12" .../>
    </MillOperation>
    <MillOperation>
        <rep_Tool ToolStnNo="3" .../>
    </MillOperation>
  </MillSetupSheetAttr> 
</Data>

You have multiple MillSetupSheetAttr elements, and the issue you are having is because you currently have a nested xsl:for-each...

<xsl:for-each select="MillSetupSheetAttr">
    <xsl:for-each select="MillOperation"> 
        <xsl:sort select="rep_Tool/@ToolStnNo" data-type="number"/>

This means it will sort MillOperation elements within each MillSetupSheetAttr element separately. So, you will get the sorted MillOperation for the first MillSetupSheetAttr first, followed by the sorted MillOperation for the second MillSetupSheetAttr.

To solve this, you should combine the two xsl:for-each statements into one:

<xsl:for-each select="MillSetupSheetAttr/MillOperation"> 
    <xsl:sort select="rep_Tool/@ToolStnNo" data-type="number"/>
    <tr>
       <td><font><xsl:value-of select="rep_Tool/@ToolStnNo"/></font></td>
    </tr>
</xsl:for-each>

This will sort all MillOperation elements in one go.

like image 51
Tim C Avatar answered Aug 08 '26 10:08

Tim C



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!