tags:

views:

58

answers:

4

Using XSLT I'm trying to work out a way to only emit a HTML table row when a value is different from the last iteration of the for-each loop. Essentially I want to make the table appear to have grouped headers, by only writing out the header once it changes.

I'm having a bit of trouble conceptually working out how I could do that considering you can't change the value of a variable once it's been defined.

<xsl:for-each select="//databaseConstraint/constraint">

    <xsl:variable name="caption" select="@caption" />

    <tr>
        <th colspan="2"><xsl:value-of select="$caption" /></th>
    </tr>

    ...

</xsl:for-each >

Update I've tried below, but I get an error: NodeTest expected here

<xsl:for-each select="//databaseConstraint/constraint">

    <xsl:variable name="caption" select="@caption" />

    <xsl:if test="not(@caption = preceding-sibling::@caption)">
    <tr>
        <th colspan="2">
            <xsl:value-of select="$caption" />
        </th>
    </tr>
    </xsl:if>

    ...

</xsl:for-each >
A: 

That's not easy to do using XSLT 1.0. If your implementation supports XSLT 2.0 features, use xsl:for-each-group. If you're stuck with 1.0, check out this Muenchian implementation using keys.

Frédéric Hamidi
Thanks. It's supposed to work in most browsers so does that rule out XSLT 2.0?
Matthew Lock
Yes, it does. To this day, neither Internet Explorer nor Firefox support XSLT 2.0.
Frédéric Hamidi
A: 

You might try to compare the current node (or one of its attributes) with the preceding node using preceding or preceding-sibling, and display the table only if current != preceding. See XPath Axes.

devio
Would you mind showing me how to do that? I tried above edit, but it fails.
Matthew Lock
@Matthew: good rule of thumb: when you say that something fails or doesn't work, tell what it did.
LarsH
@LarsH I did, see my mention in the edit
Matthew Lock
+2  A: 
<xsl:if test="not(@caption = preceding-sibling::@caption)">

This would test whether the caption is equal to a caption attribute that is a sibling of the context node, i.e. a sibling of the constraint element that you're processing. But I suspect that it "fails" with a syntax error, because the caption step has two axes: preceding-sibling::, and attribute:: (which @ is short for).

What you probably want is

<xsl:if test="not(@caption = preceding-sibling::constraint[1]/@caption)">

This will do what you want, it's simpler than Muenchian, and it's probably speedy enough, if the browser's XPath implementation is decent, because it only needs to test one other node, rather than all preceding constraint nodes.

If this strategy is not fast enough for your purposes, e.g. if you have a lot of data, you can use Muenchian grouping, as @Frédéric said.

Addition: the [1] is an abbreviation for [position() = 1]. What it means here is that on the right-hand side of the =, we only have the @caption of constraint immediately preceding the current element. If we omitted the [1], we would be comparing the current element's @caption value with the @captions of all preceding sibling constraint elements.

It's important to realize that the XPath = operator operates on node sets (when given a chance), not just on single values or nodes. So A = B, where A and B are nodesets, returns true if there is any member of nodeset A that is equal to any member of nodeset B. This is rather like a join in SQL. It's a powerful operation, but you have to be aware of what it's doing.

One other detail... Why does the [1] yield the constraint element immediately preceding the current one, instead of the first one in the document? Because position() reflects the direction of the current axis, which in this case is preceding-sibling. As the XPath spec says,

An axis that only ever contains the context node or nodes that are before the context node in document order is a reverse axis. Thus, the ancestor, ancestor-or-self, preceding, and preceding-sibling axes are reverse axes; all other axes are forward axes. ...

The proximity position of a member of a node-set with respect to an axis is defined to be the position of the node in the node-set ordered in document order if the axis is a forward axis and ordered in reverse document order if the axis is a reverse axis. The first position is 1.

HTH.

LarsH
+1 for syntax error explanation.
Alejandro
Whoever voted this answer down, please have the decency to leave a constructive comment.
LarsH
I got downvoted too for *sketching* a possible solution. "tried something which did not work" => vote--; thanks for elaborating the same idea.
devio
@LarsH this worked nicely! Would you mind explaining further why there is a [1] for my own learning. Thanks.
Matthew Lock
@Matthew, I added an explanation as requested.
LarsH
+2  A: 

I'm trying to work out a way to only emit a HTML table row when a value is different from the last iteration of the for-each loop

This answers the question literally. In case you want to perform grouping learn about Muenchian grouping.

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
  <xsl:variable name="vConstraints"
       select="//databaseConstraint/constraint"/>
  <table>
      <xsl:for-each select="$vConstraints">
        <xsl:variable name="vCaption" select="@caption" />
        <xsl:variable name="vPos" select="position()"/>

        <xsl:if test="not($vCaption = $vConstraints[$vPos -1]/@caption)">
            <tr>
                <th colspan="2"><xsl:value-of select="$vCaption" /></th>
            </tr>
        </xsl:if>
      </xsl:for-each>
  </table>
 </xsl:template>
</xsl:stylesheet>

when applied on the following XML document:

<t>
 <a>
 <databaseConstraint>
   <constraint caption="Simple Constraint"/>
 </databaseConstraint>
 <databaseConstraint>
   <constraint caption="Simple Constraint"/>
 </databaseConstraint>
 </a>
 <b>
 <databaseConstraint>
   <constraint caption="Complex Constraint"/>
 </databaseConstraint>
 </b>
</t>

produces the wanted, correct result:

<table>
   <tr>
      <th colspan="2">Simple Constraint</th>
   </tr>
   <tr>
      <th colspan="2">Complex Constraint</th>
   </tr>
</table>

Do note:

  1. The transformation works even in the case when the <constraint> elements are not siblings, or even if some of them are ancestors/descendents of each other.

  2. In this case it is necessary to collect all <constraint> elements in a variable and to rememper the position() of the current <constraint> element so that it is possible to compare it with the <constraint> element at the previous position in the node-set.

Dimitre Novatchev
+1 I think that as a fair literally answer, this is a good answer.
Alejandro
This is very useful. Will keep it in mind.
Matthew Lock
@Matthew: You are welcome :)
Dimitre Novatchev