Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8113409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:49:10+00:00 2026-06-06T02:49:10+00:00

I’m trying to add a value (in this case the string I Added This)

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T02:49:13+00:00Added an answer on June 6, 2026 at 2:49 am

    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>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Does anyone know how can I replace this 2 symbol below from the string
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.