Sumathi
Sumathi

Reputation: 83

Comment the parent element based on child element string in xslt

My Input XML is like below section is appear under part.

<part type="backmatter">
<section counter="yes" level="1">
<title>Credits<target id="page1301"/></title>
<section counter="yes" level="2">
<title>Chapter 1</title>
<para><link idref="c001_t003">Table 1-3</link> Adapted from Sidle DM</para>
......
......
</section>
</section>
<section counter="yes" level="1">
<title>Index<target id="page1321"/></title>
<section counter="yes" level="2">
<title>A</title>
<listing type="dash">
<litem><para>Abbé lip switch flap</para>
</litem>
<litem><para>Abdomen</para>
......
......
</section>
</section>
</part>

Output should be,

<part type="backmatter">
<section counter="yes" level="1">
<title>Credits<target id="page1301"/></title>
<section counter="yes" level="2">
<title>Chapter 1</title>
<para><link idref="c001_t003">Table 1-3</link> Adapted from Sidle DM</para>
......
......
</section>
</section>
<!--<section counter="yes" level="1">
<title>Index<target id="page1321"/></title>
<section counter="yes" level="2">
<title>A</title>
<listing type="dash">
<litem>
<para>Abbé lip switch flap</para></litem>
<litem><para>Abdomen</para>
......
......
</section>
</section>-->
</part>

My xslt is,

<xsl:template match="section">        
<xsl:choose>
<xsl:when test="following-sibling::title[contains(., 'Index')]">
<xsl:text disable-output-escaping="yes">&lt;!--</xsl:text>
<xsl:copy><xsl:apply-templates select="node() | @*"/>  </xsl:copy>
<xsl:text disable-output-escaping="yes">--&gt;</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:copy><xsl:apply-templates select="node() | @*"/>  </xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template> 

I want to comment the "section" whenever title contain 'Index' string and the remaining section should be as it is in xml. Could you please help us to resolve this issue.

Upvotes: 1

Views: 76

Answers (1)

fafl
fafl

Reputation: 7385

It almost works, you just need to change your condition a bit:

<xsl:choose>
  <xsl:when test="contains(title, 'Index')">
    <xsl:text disable-output-escaping="yes">&lt;!--</xsl:text>
    <xsl:copy><xsl:apply-templates select="node() | @*"/></xsl:copy>
    <xsl:text disable-output-escaping="yes">--&gt;</xsl:text>
  </xsl:when>
  <xsl:otherwise>
    <xsl:copy><xsl:apply-templates select="node() | @*"/></xsl:copy>
  </xsl:otherwise>
</xsl:choose>

Upvotes: 2

Related Questions