Re: [xsl] XPath to find first cell in first row of table

Subject: Re: [xsl] XPath to find first cell in first row of table
From: "G. Ken Holman g.ken.holman@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 22 Sep 2016 02:52:08 -0000
At 2016-09-22 01:15 +0000, Mark Giffin m1879@xxxxxxxxxxxxx wrote:
Content-Transfer-Encoding: 7bit

I have an XML table like below from which I am constructing a roughly matching XSL-FO table, and I need to insert something extra only in the first cell of the first row, as indicated:

   <table>
     <title>My title</title>
     <tgroup cols="3">
       <colspec colname="C1" />
       <colspec colname="C2" />
       <colspec colname="C3" />
       <thead>
         <row>
           <entry> text </entry>
           <entry> text </entry>
           <entry> text </entry>
         </row>
       </thead>
       <tbody>
         <row>
           <entry> <!-- insert something extra here only --> text </entry>
           <entry> text </entry>
           <entry> text </entry>
         </row>
         <row>
           <entry> text </entry>
           <entry> text </entry>
           <entry> text </entry>
         </row>
       </tbody>
     </tgroup>
   </table>

There are templates matching each element: table, tbody, row, entry, etc. In the <entry> template, how might I check if the current entry is the first one in the first row? Or is there a better way?

<xsl:template match="entry">
<xsl:if test="MAGICAL XPATH that tells if it's the first cell in the first row">
<fo:block> insert special things </fo:block>
</xsl:if>
<fo:block xsl:use-attribute-sets="entry">
...
</fo:block>
</xsl:template>

Graydon's answer is good, but my guess is that you are missing in the template above the <fo:table-cell> in the match for entry. If so, then the block created by Graydon would end up outside of the table cell.


If you inadvertently did leave it out, then I might use something like:

 <xsl:template match="entry">
  <fo:table-cell>
    <xsl:if test="not(preceding-sibling::entry) and
                  not(../preceding-sibling::row) and
                  ../parent::tbody">
      <fo:block> insert special things </fo:block>
    </xsl:if>
    <fo:block xsl:use-attribute-sets="entry">
      ...
    </fo:block>
  </fo:table-cell>
 </xsl:template>

(because of tables inside of tables I didn't use the ancestor:: axis above)

Axis walking is usually pretty quick. A key table could also be used, but I'm not convinced it would be any faster since it involves getting built in the first place:

 <xsl:key match="tbody/row[1]/entry[1]" use="generate-id(.)"
          name="firstCells"/>

 <xsl:template match="entry">
  <fo:table-cell>
    <xsl:if test="key('firstCells',generate-id(.))">
      <fo:block> insert special things </fo:block>
    </xsl:if>
    <fo:block xsl:use-attribute-sets="entry">
      ...
    </fo:block>
  </fo:table-cell>
 </xsl:template>

I hope this is helpful.

. . . . . . Ken

--
Check our site for free XML, XSLT, XSL-FO and UBL developer resources |
Streaming hands-on XSLT/XPath 2 training @US$45: http://goo.gl/Dd9qBK |
Crane Softwrights Ltd. _ _ _ _ _ _ http://www.CraneSoftwrights.com/s/ |
G Ken Holman _ _ _ _ _ _ _ _ _ _ mailto:gkholman@xxxxxxxxxxxxxxxxxxxx |
Google+ blog _ _ _ _ _ http://plus.google.com/+GKenHolman-Crane/posts |
Legal business disclaimers: _ _ http://www.CraneSoftwrights.com/legal |

Current Thread