Anatol Shiro
Anatol Shiro

Reputation: 3

XSLT complex sorting and grouping

I have an XML file looked like this:

<components>
       <comp>
      <ref>q7</ref>
      <partnumber>foo</partnumber>
   </comp>
   <comp>
      <ref>q1</ref>
      <partnumber>foo</partnumber>
   </comp>
   <comp>
      <ref>q6</ref>
      <partnumber>bar</partnumber>
   </comp>
   <comp>
      <ref>q3</ref>
      <partnumber>bar</partnumber>
   </comp>
</components>

And I need to goup by partnumber and sort by ref, so that output file will look like this:

q1 q7, foo
q3 q6, bar

But I'm getting this output:

q3 q6, bar
q1 q7, foo

Here is my XSL:

<!DOCTYPE xsl:stylesheet [
  <!ENTITY nl  "&#xd;&#xa;">    <!--new line CR, LF, or LF, your choice -->
]>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="text"/>
    <xsl:key name='compspec' match="comp" use="partnumber" />
    <xsl:template match="/components">
        <xsl:for-each select="comp[generate-id(.)=generate-id(key('compspec',partnumber)[1])]">
            <xsl:sort select="ref"/>
            <xsl:for-each select="key('compspec',partnumber)">
                <xsl:sort select="ref"/>
                <xsl:value-of select="ref"/>
                <xsl:text> </xsl:text>
            </xsl:for-each>
            <xsl:text>,</xsl:text>
            <xsl:value-of select="partnumber"/>
            <xsl:text>&nl;</xsl:text>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet> 

I understand why I'm getting this sequence, but I'm pretty new for XSLT and i don't know how to fix it. Whate should I fix in my code? I'm using XSLT 1.0. Thank you!

Upvotes: 0

Views: 281

Answers (2)

michael.hor257k
michael.hor257k

Reputation: 116959

I need to sort groups with equal part number by minimum value of comp/ref

To do this in XSLT 1.0, you will have to make two passes:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="text" encoding="UTF-8"/>

<xsl:key name='compspec' match="comp" use="partnumber" />

<xsl:template match="/components">
    <!-- first pass -->
    <xsl:variable name="groups">
        <xsl:for-each select="comp[generate-id(.)=generate-id(key('compspec',partnumber)[1])]">
            <group name="{partnumber}">
                <xsl:for-each select="key('compspec', partnumber)">
                    <xsl:sort select="ref"/>
                    <value>
                        <xsl:value-of select="ref"/>
                    </value>
                </xsl:for-each>
            </group>
        </xsl:for-each>
    </xsl:variable>
    <!-- output -->
    <xsl:for-each select="exsl:node-set($groups)/group">
        <xsl:sort select="value[1]"/>
        <xsl:for-each select="value">
            <xsl:value-of select="."/>
            <xsl:if test="position()!=last()">
                <xsl:text> </xsl:text>
            </xsl:if>
        </xsl:for-each>
        <xsl:text>, </xsl:text>
        <xsl:value-of select="@name"/>
        <xsl:text>&#10;</xsl:text>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet> 

Note:
In your example, ref contains text values and in the code above it is sorted as text. Therefore, minimum value of comp/ref is actually the first ref value in alphabetical order.

Upvotes: 0

matthias_h
matthias_h

Reputation: 11416

You'll get the desired output if you just delete this line:

 <xsl:sort select="partnumber"/>

in your first for-each loop, because you're sorting alphabetically by partnumber (bar and foo).

Upvotes: 1

Related Questions