2017-08-09 14 views
0

使用XSLT,我需要在一個列中只包含「傑克」去掉一個完整的錶行,我做了它,但它的比賽XSLT刪除錶行當所有的價值

<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

以您目前的表現,實際上../../tr將尋找那些兄弟當前行的「祖父」(即table父),我猜是不是你想要的tr元素。

如果你想刪除行,如果任何列包含單詞傑克,那麼模板應該看起來像這樣。

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

或者,可能是更好的是,有一個模板,用千斤頂將刪除任何行中,像這樣......

<xsl:template match="tr[td/text()='Jack']" /> 
+0

它的工作原理,但是當根節點的xmlns =「金塔:測試: v3「它不起作用。你知道它爲什麼會發生嗎? –

+0

這是一個默認的名稱空間聲明。名稱空間中的元素與不在名稱空間中的元素不同,即使它們看起來具有相同的名稱。例如,請參閱https://stackoverflow.com/questions/34758492/xslt-transform-doesnt-work-until-i-remove-root-node。 –