I’ve been wrestling with the correct syntax for this and it boils down to:
How do I test to see if the parent node’s attribute has a certain value?
I’m transforming some XHTML. I template match a <tr> in order to reformat the colspan attributes of some of the cells in that row. To add further confidence that this will only occur in certain tables, I need to check that the <table> the <tr> belongs to has a particular id attribute value.
<xsl:template match="tr">
<tr>
<xsl:choose>
<xsl:when test="(count(td[@colspan='2'])=2 and count(td)=3)">
<td colspan="1">
<xsl:copy-of select="td[1]/node()" />
</td>
<td colspan="4">
<xsl:copy-of select="td[2]/node()" />
</td>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="@*|node()" />
</xsl:otherwise>
</xsl:choose>
</tr>
</xsl:template>
Here is the code I have so far. I either need to add further “ands” to my when test or another xsl:if or xsl:when to check the table attribute. For this instance, lets have the table id=”Transformable”.
To clarify, i only wish to do the above transformation when the table that the <tr> belongs to has an id of “Transformable”.
Any help would be greatly appreciated.
I guess you can use
parentaxis in your template match definition.Update: For complex nesting (if
tableis not a direct parent oftr) – you can useancestoraxis.