Remove redundant tags inside <TABLE> tag from XML with XSLT -
i got below data in input.xml file, contains few of <i>
tags outside of <td>
tags creating problem in bi report generation. please xslt code tidy xml. suggestions welcome.
<table border="1" cellspacing="0" cellpadding="0"> <tbody> <tr></tr> <tr> <td valign="top" width="402"> <p> <b>column heading here </b> </p> </td> <td valign="top" width="234"> <p> <b>another heading</b> </p> </td> </tr> <tr> <td valign="top" width="402"> <p> <i> item discount <i></i> </i> </p> <i> <i></i> </i> </td> <i> <i> <td valign="top" width="234"> <p align="center"> <i>%</i> </p> <i> <i></i> </i> </td> <i> <i></i> </i> </i> </i> </tr> <i> <i> <i> <i> <tr> <td valign="top" width="402"> <p> <i> item discount <i></i> </i> </p> <i> <i></i> </i> </td> <i> <i> <td valign="top" width="234"> <i> <p align="center"> <i>%</i> </p> </i> <i> <i></i> </i> </td> <i> <i></i> </i> </i> </i> </tr> <i> <i> <i> <i></i> </i> </i> </i> </i> </i> </i> </i> </tbody> </table>
expected output :(** tags between ** should removed)
<table border="1" cellspacing="0" cellpadding="0"> <tbody> <tr></tr> <tr> <td valign="top" width="402"> <p> <b>column heading here </b> </p> </td> <td valign="top" width="234"> <p> <b>another heading</b> </p> </td> </tr> <tr> <td valign="top" width="402"> <p> <i> item discount <i></i> </i> </p> <i> <i></i> </i> </td> ** <i> <i> ** <td valign="top" width="234"> <p align="center"> <i>%</i> </p> <i> <i></i> </i> </td> ** <i> <i></i> </i> </i> </i> ** </tr> ** <i> <i> <i> <i> ** <tr> <td valign="top" width="402"> <p> <i> item discount <i></i> </i> </p> <i> <i></i> </i> </td> ** <i> <i> ** <td valign="top" width="234"> <i> <p align="center"> <i>%</i> </p> </i> <i> <i></i> </i> </td> ** <i> <i></i> </i> </i> </i> ** </tr> ** <i> <i> <i> <i></i> </i> </i> </i> </i> </i> </i> </i> ** </tbody> </table>
just create template matches <i>
elements have <td>
siblings:
<xsl:template match="i[preceding-sibling::td | following-sibling::td ]"> <xsl:apply-templates/> </xsl:template>
since <xsl:apply-templates>
default behavior copy textual nodes, if, in sample provided, <i>
elements empty, not copy them output.
if want recursive can add children match:
<xsl:template match="i[preceding-sibling::td | following-sibling::td | ancestor::i[preceding-sibling::td | following-sibling::td] ]"> <xsl:apply-templates/> </xsl:template>
update: if want remove <i>
elements have <td>
siblings , <i>
children can use:
<xsl:template match="i[preceding-sibling::td | following-sibling::td | parent::i[preceding-sibling::td | following-sibling::td] ]"> <xsl:apply-templates/> </xsl:template>
this basic xpath. if you're dealing kind of issue, recommend take 1 hour or 2 read xpath. it's worth it.
Comments
Post a Comment