0

Using XSLT, I need to remove a complete table row when a column contains only "Jack", I made it but it remove all the rows after the match

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="tr">
    <xsl:if test="../../tr/td[text()='Jack']">
        <xsl:call-template name="ident" />
    </xsl:if>
</xsl:template>    

<table>
    <th>
        <td>Contestant</td>
        <td>Score</td>
        <td>Country</td>
    </th>
    <tr>
        <td>Jack</td>
        <td>0.00</td>
        <td>AUS</td>
    </tr>
    <tr>
        <td>Jill</td>
        <td>-</td>
        <td>-</td>
    </tr>
</table>

1 Answers1

1

With your current expression, ../../tr will actually look for tr elements that are sibling of the current row's "grandparent" (i.e the parent of table) which I guess is not what you want.

If you want to remove the row if any column contains the word jack, then the template should look like this.

<xsl:template match="tr">
    <xsl:if test="not(td[text()='Jack'])">
        <xsl:call-template name="ident" />
    </xsl:if>
</xsl:template>    

Or, probably better still, have a template that removes any row with jack in, like so...

<xsl:template match="tr[td/text()='Jack']" />
Tim C
  • 70,053
  • 14
  • 74
  • 93
  • It works but when the root node has xmlns="urn:test:v3" it does not work. do you know why it happend? – Fred Duarte Aug 09 '17 at 19:34
  • That is a default namespace declaration. An element in a namespace is different to an element that is not in a namespace even if they appear to have the same name. See https://stackoverflow.com/questions/34758492/xslt-transform-doesnt-work-until-i-remove-root-node for example. – Tim C Aug 09 '17 at 21:49