Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT sort in conjunction with preceding-sibling::

Tags:

sorting

xslt

Within a foreach loop i want to use preceding-sibling::

<for-each..>
    <xsl:sort select="type"/>
    <xsl:when test="preceding-sibling::data[1]/type != type

the problem is that "type" within the foreach is compared with a unsorted preceding-sibling e.g.

data1/type = 1 
data2/type = 2
data3/type = 1

would compare in the second loop silbling=2 (original unsorted) and type=1 (since it is sorted)

is there a way around it?

UPDATE: my intention is the following

before             after
data/type2         type1 value1
data/type1         type1 value2 
data/type1         and speaking in HTML a spacer here (I compare type2:value to preceding-sibling value
data/type2         type2 value1
                   type2 value2

I have an unsorted list of addresses where the type is a town and I need a HTML Table sorted by town and to do some stuff depending on the values and other fields (that part is working, but since the comparison with preceding-sibling is not working in a sorted for-each, I got the problem

like image 754
RRZ Europe Avatar asked May 12 '11 15:05

RRZ Europe


1 Answers

This solution now works for me:

    <xsl:variable name="sortedcopy">
      <xsl:for-each select="node1/node2/node3/data">
        <xsl:sort select="type" order="ascending"/>
        <xsl:copy-of select="current()"/>
      </xsl:for-each>
    </xsl:variable>
    <xsl:variable name="relItems" select="MSXML:node-set($sortedcopy)" />
    <xsl:for-each select="$relItems/data">
      <xsl:if test="not(preceding-sibling::data[1]/id = id)">
        <hr/>
      </xsl:if>
      <xsl:value-of select="val"/>
    </xsl:for-each>
like image 103
RRZ Europe Avatar answered Sep 28 '22 00:09

RRZ Europe