Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace All Values In Table Column

I'm trying to add a value (in this case the string "I Added This") to all -nodes within a specific column if the column does contain a certain value (in this case the text "I Want You") using XSLT 2.0.

This means, if I have the following table:

<table>
        <tr>
            <th>header value</th>
            <th>I Want You</th>
            <th>header value</th>
        </tr>

        <tr>
            <td>value 1</td>
            <td>value 2</td>
            <td>value 3</td>
        </tr>

        <tr>
            <td>value 4</td>
            <td>value 5</td>
            <td>value 6</td>
        </tr>
</table>

the output should be this after I apply the xslt script:

<table>
        <tr>
            <th>header value</th>
            <th>I Want You</th>
            <th>header value</th>
        </tr>

        <tr>
            <td>value 1</td>
            <td>I Added This value 2</td>
            <td>value 3</td>
        </tr>

        <tr>
            <td>value 4</td>
            <td>I Added This value 5</td>
            <td>value 6</td>
        </tr>
</table>

To achieve that I wrote many different stylesheets but I didn't even came close:

Copying the entire table:

<xsl:template match="node()|@*">
    <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>

This gets me the values of the column I want to change - but from there I don't know how to prepend each td-value with the phrase "I Added This Value"

<xsl:template match="//th[contains(lower-case(.), 'I Want You')]">

    <xsl:variable name="tdPos" select="count(preceding-sibling::td)+2"/>

    <xsl:for-each select="current()/parent::*/following-sibling::tr">
        <xsl:value-of select="./td[$tdPos]/text()"/>
    </xsl:for-each>
</xsl:template>

Any hints that might point me into the right direction are greatly appreciated!

like image 920
user1451602 Avatar asked Dec 28 '25 21:12

user1451602


2 Answers

One problem worth noting is that your "contains" test is not quite right

<xsl:template match="//th[contains(lower-case(.), 'I Want You')]"> 

It probably be the following

<xsl:template match="//th[contains(lower-case(.), lower-case('I Want You'))]"> 

However, it might be better all round if you parameterise your search values, although you wouldn't be able to use such parameter in the template match. Instead you would just match any cell....

<xsl:template match="tr/*">

Then you could test if equivalent column on a preceding row has the value you want

<xsl:if 
   test="../preceding-sibling::tr/*[$position][contains(., $match)]">

(lower-case() command removed for brevity)

Here $position will be a variable containing the current cell position, and $match is the variable you are looking for.

Try the following XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="html" indent="yes"/>
   <xsl:param name="match" select="'I Want You'" />
   <xsl:param name="add" select="'I Added This'" />

   <xsl:template match="tr/*">
      <xsl:variable name="position" select="count(preceding-sibling::*) + 1"/>          <xsl:copy>
         <xsl:apply-templates select="@*"/>
         <xsl:if test="../preceding-sibling::tr/*[$position][contains(lower-case(.), lower-case($match))]">
            <xsl:value-of select="concat($add, ' ')" />
         </xsl:if>
         <xsl:apply-templates select="text()"/>
      </xsl:copy>
   </xsl:template>

   <xsl:template match="@*|node()">
      <xsl:copy>
         <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
   </xsl:template>
</xsl:stylesheet>

When applied to your XML, the following is produced

<table>
<tr>
   <th>header value</th>
   <th>I Want You</th>
   <th>header value</th>
</tr>
<tr>
   <td>value 1</td>
   <td>I Added This value 2</td>
   <td>value 3</td>
</tr>
<tr>
   <td>value 4</td>
   <td>I Added This value 5</td>
   <td>value 6</td>
</tr>
</table>

And when applied to this XML

<table>
   <tr>
      <th>header value</th>
      <th>header value</th>
      <th>header value</th>
   </tr>
   <tr>
      <td>value 1</td>
      <td>I Want You</td>
      <td>value 2</td>
   </tr>
   <tr>
      <td>value 3</td>
      <td>value 4</td>
      <td>value 5</td>
   </tr>
</table>

The following is output

<table>
<tr>
   <th>header value</th>
   <th>header value</th>
   <th>header value</th>
</tr>
<tr>
   <td>value 1</td>
   <td>I Want You</td>
   <td>value 2</td>
</tr>
<tr>
   <td>value 3</td>
   <td>I Added This value 4</td>
   <td>value 5</td>
</tr>
</table>
like image 84
Tim C Avatar answered Dec 30 '25 22:12

Tim C


You were on the right lines - you just needed to do a condition as you output nodes so that, in the case of TD nodes, check if the cousin TH node at the same index featured the required text.

<xsl:template match="*|@*">
    <xsl:variable name='curr_pos' select='position()' />
    <xsl:copy>
        <xsl:apply-templates select='@*' />
        <xsl:if test='name() = "td" and /table/tr[1]/th[$curr_pos] = "I Want You"'>prefix - </xsl:if>
        <xsl:value-of select='text()' />
        <xsl:apply-templates select='*' />
    </xsl:copy>
</xsl:template>

You can run it at this playground session.

like image 31
Mitya Avatar answered Dec 30 '25 23:12

Mitya