I am working with table entry elements and want to get all preceding entry elements in the same table that pass the following test:
parent::row/preceding-sib
Off the top of my head, I would first use a key that matches entries by their ancestor table:
<xsl:key name="moreRowsEntriesByTable" match="entry[@morerows]"
use="generate-id(ancestor::table[1])" />
Hopefully you can use that to narrow down the set of nodes that you have to process in XPath. But I confess I haven't really understood the condition you're trying to filter by, so I'm not positive that the above is applicable.
Re: "I can't figure out how to include my XPath condition and get a result." I often get frustrated in XSLT 1.0 at the awkwardness of trying to combine a key with a preceding:: or following:: axis. It can't be done succinctly.
You could work it in by inserting the following predicate where needed:
[count(key('moreRowsEntriesByTable', $table-id) | .) =
count(key('moreRowsEntriesByTable', $table-id)]
where $table-id is the id of the current() node's ancestor table:
<xsl:variable name="table-id" select="generate-id(ancestor::table[1])" />
But that may be slower than not using a key at all:
[generate-id(ancestor::table[1]) = $table-id]
You could attach this predicate to each entry
node test in you XPath expression. But again, I'm not sure it would help with performance.
Instead, I would suggest a two-stage transformation. Does your processing context allow that? For that, you can either run two stylesheets, with the output of the first piped to the input of the second; or use the common node-set()
extension function to convert the output of one template to a node set that can serve as input to another template.
In the first transformation, you could add attributes to each table cell to help with the computation, such as...
@row -- row number of this entry in current table (position() of parent::row)
@index -- position of this entry among siblings in the parent::row, taking into
account their colspans if necessary.
@table-id -- generate-id(ancestor::table[1])
The idea would be amortization (if I'm applying that term correctly)... If these values are computed once per entry, in the first stage, instead of many times, in the complicated XPath expression, that could make things quite a bit faster. Maybe there are other attributes that would be more helpful.
You could remove (not copy) these helper attributes in the second transformation.
This is a sketchy suggestion, but maybe it will help point toward a good solution.
If your processor can do the exslt:node-set function or an equivalent such as that provided by msxml then you could attack this procedurally, using a tail-recursive template. Ugly, I know, and completely against what we usually recommend for XSLT, but in this case I think the only way to do this efficiently is to pass information from one row to the next. Assuming that the first row of the table will always have an entry
for every column, then how about something like this:
<xsl:template match="tbody">
<w:tbl>
<xsl:apply-templates select="row[1]">
<xsl:with-param name="rowSpec">
<!-- build the row info structure assuming that the first row has
the right number of entry elements. Nothing spans into the
first row, so they all get @span=0 -->
<xsl:for-each select="row[1]/entry">
<r span="0" />
</xsl:for-each>
</xsl:with-param>
</xsl:apply-templates>
</w:tbl>
</xsl:template>
<xsl:template match="row">
<xsl:param name="rowSpec" />
<xsl:variable name="theRow" select="." />
<w:tr>
<!-- build up the output for this row -->
<xsl:for-each select="exsl:node-set($rowSpec)/r">
<w:tc>
<xsl:choose>
<xsl:when test="@span = 0">
<!-- this row has an entry for the column -->
<xsl:apply-templates select="$theRow/entry[
count(current()/preceding-sibling::r[@span = 0]) + 1]" />
</xsl:when>
<xsl:otherwise>
<!-- this column spans from the previous row -->
<w:tcPr>
<w:vMerge/>
</w:tcPr>
<w:p/>
</xsl:otherwise>
</xsl:choose>
</w:tc>
</xsl:for-each>
</w:tr>
<!-- process the next row with recalculated spans -->
<xsl:apply-templates select="following-sibling::row[1]">
<xsl:with-param name="rowSpec">
<xsl:for-each select="exsl:node-set($rowSpec)/r">
<xsl:choose>
<xsl:when test="@span = 0">
<!-- we had an entry element for this column, use its @morerows
as the next span, or 0 if it doesn't have one -->
<xsl:choose>
<xsl:when test="$theRow/entry[
count(current()/preceding-sibling::r[@span = 0]) + 1]/@morerows">
<r span="{$theRow/entry[
count(current()/preceding-sibling::r[@span = 0]) + 1]/@morerows}" />
</xsl:when>
<xsl:otherwise>
<r span="0" />
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<!-- we didn't have an entry for this column, it was a span from the
previous row - subtract 1 from the span when we pass on to
the next row -->
<r span="{@span - 1}" />
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:with-param>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="entry">
<w:p>
<w:r>
<w:t><xsl:value-of select="." /></w:t>
</w:r>
</w:p>
</xsl:template>
(Wow, that ended up much more complex than I expected when I started it, but I've tested it out and it seems to work correctly). The idea here is that we build a structure that encodes the number of rows that each column still needs to span over at the point where that row is processed. So for the first row we'll have
<r span="0"/>
<r span="0"/>
<r span="0"/>
<r span="0"/>
then for the second it'll be
<r span="2"/>
<r span="1"/>
<r span="0"/>
<r span="0"/>
the third
<r span="1"/>
<r span="0"/>
<r span="0"/>
<r span="0"/>
etc. For each row we then iterate over this structure rather than over the entry
elements themselves.
Edit: now you've changed the question so you need to account for the possibility of column spans as well as row spans it gets much much messier. Since we're committed to using a node-set function, I would think about a two-step approach as hinted at by LarsH, where you first expand out the column spanning entries into real entry
elements (with some attribute to identify them as such) and then process the expanded version of the XML in place of the original.