Reputation: 69
I want to add a attribute type="bottom line" to the row whose following sibling has entry with table notes,I did it..But the question is that if there is merged columns I have to add type="bottom line" to that entries also, and this adding should stop when entry count are equal.
Input XML
<table>
<row>
<entry id="1"></entry>
<entry id="2"></entry>
<entry id="3" merged="1"></entry>
<entry id="4"></entry>
<entry id="5"></entry>
</row>
<row>
<entry id="1"></entry>
<entry id="2"></entry>
<entry id="3" merged="1"></entry>
<entry id="4"></entry>
<entry id="5"></entry>
</row>
<row>
<entry id="6" merged="1"></entry>
<entry id="7"></entry>
<entry id="8"></entry>
<entry id="9"></entry>
</row>
<row>
<entry id="10"></entry>
<entry id="11"></entry>
<entry id="12"></entry>
<entry id="13"></entry>
<entry id=“14"></entry>
</row>
<row>
<entry id="15"></entry>
<entry id="16"></entry>
<entry id="17"></entry>
</row>
<row>
<entry type="table notes">test</entry>
<entry type="table notes">test</entry>
<entry type="table notes">test</entry>
</row>
</table>
Expected OUTPUT
<table>
<row>
<entry id="1"></entry>
<entry id="2"></entry>
<entry id="3" merged="1"></entry>
<entry id="4"></entry>
<entry id="5"></entry>
</row>
<row>
<entry id="1"></entry>
<entry id="2"></entry>
<entry id="3" merged="1" type="bottom line"></entry>
<entry id="4"></entry>
<entry id="5"></entry>
</row>
<row>
<entry id="6" merged="1" type="bottom line"></entry>
<entry id="7"></entry>
<entry id="8"></entry>
<entry id="9"></entry>
</row>
<row>
<entry id="10"></entry>
<entry id="11"></entry>
<entry id="12"></entry>
<entry id="13"></entry>
<entry id="14"></entry>
</row>
<row>
<entry id="15" type="bottom line"></entry>
<entry id="16" type="bottom line"></entry>
<entry id="17" type="bottom line"></entry>
</row>
<row>
<entry type="table notes">test</entry>
<entry type="table notes">test</entry>
<entry type="table notes">test</entry>
</row>
</table>
The XSLT I wrote so far:
for add bottom line to table notes
bottom line
for merged entries
<xsl:template match="row/entry[@merged]" mode="merged">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:attribute name="type">bottom line</xsl:attribute>
<xsl:apply-templates/>
</xsl:copy>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="row[last()-1]">
<xsl:if test="count(./entry) > count(./preceding-sibling::row/entry)">
<xsl:apply-templates mode="merged"/>
</xsl:if>
</xsl:template>
Please help me to resolve this issue. thanks..
Upvotes: 0
Views: 79
Reputation: 5432
XSLT:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output method="xml" indent="yes"/>
<!-- identity transform template -->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<!-- change the desired elements -->
<xsl:template match="row[following-sibling::*[1]/entry/@type = 'table notes']/entry | entry[@merged]">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:attribute name="type">bottom line</xsl:attribute>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
The first template is the identity transform template to copy all the attributes and nodes.
The second template adds the @type
to the desired elements.
Upvotes: 1