This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: AW: XSL equivalent to SQL "unique"


Hi Oliver,

> Perhaps Jeni will have a break and post some explanations on how to
> solve problems like yours ;-)

Well, I'm on a break, but what could I possibly add to the solution
that you gave?

Only, I guess, that this is an example of a difficult grouping problem
where really you want to reuse the information about the *sorted*
unique values, rather than having to sort them again for every row.

This is one of those times that the node-set() extension function
really comes into its own.  So you could declare a top-level $cols
variable with:

<xsl:variable name="cols-rtf">
   <!-- get the uniquely named child elements of the set elements -->
   <xsl:for-each select="//set/*[count(.|key('cols',name())[1]) = 1]">
      <!-- sort the child elements by their name -->
      <xsl:sort select="name()" />
      <!-- create col elements to hold that information -->
      <col name="{name()}" />
   </xsl:for-each>
</xsl:variable>

<!-- turn $cols-rtf into a node set with an extension function
     (substitute prefix of your chosen processor) -->
<xsl:variable name="cols" select="exsl:node-set($cols-rtf)" />

With that, then the various xsl:for-eaches for generating the column
headers and the row data don't have to sort, they can just use the
$cols variable and the name attribute from it, e.g.

<xsl:template match="set">
   <tr>
      <th><xsl:value-of select="@name" /></th>
      <xsl:variable name="entries" select="*" />
      <xsl:for-each select="$cols">
         <td>
            <xsl:value-of select="$entries[name() = current()/@name]" />
         </td>
      </xsl:for-each>
   </tr>
</xsl:template>

Of course this isn't portable. So roll on XSLT 2.0... or support for
EXSLT ;)

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]