0
votes

I'm trying to patch my .ODT document (change values in table) with xslt. I know how to change value of only one cell in table, but how to fill whole column?

This is example how to change one value from the table (row=3, col=3)

<xsl:template match="table:table[@table:name='1 level']/table:table-row[position()=3]/table:table-cell[position()=3]">
        <xsl:apply-templates select="." mode="replace">
            <xsl:with-param name="value" select="TEST"/>
        </xsl:apply-templates>
    </xsl:template>

Before patching:

      A   B   C  
 --- --- --- --- 
  1   1   1   1  
  2   1   1   1  
  3   1   1   1  
  4   1   1   1  

After patching:


      A   B    C    
 --- --- --- ------ 
  1   1   1   1     
  2   1   1   1     
  3   1   1   TEST  
  4   1   1   1     

How fill whole column by changing data in loop? I have xml list, that comes from file by <xsl:variable name="data" select="document('template.xml')/root"/>:

<?xml version="1.0" encoding="UTF-8" ?>
<root>
<marketDatas>
    <marketData>
        <countDeals>10</countDeals>
    </marketData>
    <marketData>
        <countDeals>20</countDeals>
    </marketData>
    <marketData>
        <countDeals>30</countDeals>
    </marketData>
</marketDatas>
<root>

and want change row C1-C3 by value from marketDatas list. Expected output:


      A   B     C     
 --- --- --- -------- 
  1   1   1   10   
  2   1   1   20   
  3   1   1   30   
  4   1   1   1  
1
For addition, I use xslt version 1.0 - MaximumQuiet
Where is the data for the replaced column (not row) coming from? - michael.hor257k
Sorry, that's still not a minimal reproducible example. Sow us the input XML and clarify where is this "xml list" coming from. - michael.hor257k
@michael.hor257k i tried again :) xml file source added - MaximumQuiet
Bur where is the original table? (I am getting tired of this.) - michael.hor257k

1 Answers

0
votes

You do not have to change much. If you just want to change your last table column modify your code to:

<xsl:template match="table:table[@table:name='1 level']/table:table-row/table:table-cell[position() = last()]">
    <xsl:variable name="currRow" select="count(parent::table:table-row/preceding-sibling::table:table-row) + 1"/>
    <xsl:apply-templates select="." mode="replace">
        <xsl:with-param name="value" select="$data/root/marketDatas/marketData[$currRow]/countDeals"/>
    </xsl:apply-templates>
</xsl:template>

This should match on every row of the table at the last cell. If you always want to change the 3rd row dont replace you 'position() = 3'. Note that this can change other rows too if you are working with spans. If your last table-cell has a span of 2 (so its technically the last table-cell but starts one column earlier) it will also get replaced.

  • Updated version: this is not tested, but it should work. Consider implementing a fallback if $value is not declared or empty (string-length($value) = 0).