I’m trying to exclude certain set of html tags from the xml string using XSLT 1.0
Here, currently I’m excluding <a> and <img> tag. For <a> tag, I want to display only text.
Tried XSLT template:
<xsl:template match="*" mode="ExcludeHTMLTags">
<xsl:choose>
<xsl:when test="local-name() = 'a' or local-name() = 'img'">
<xsl:value-of select="text()"/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="node()|@*"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
Calling above template in the below fashion:
<xsl:variable name="guideContent">
<root>
<xsl:apply-templates
select="document(@guideID)/tcm:Component/tcm:Data/tcm:Content/em:GeneralContent/em:Body/node()"
mode="expandXHTML"/>
</root>
</xsl:variable>
<xsl:apply-templates select="msxsl:node-set($guideContent)/node()" mode="ExcludeHTMLTags"/>
Input XML string:
<root>
This is a test message.
<p>Message within p tag</p> click <a href="www.test.com">here</a>.
<img src="/test.jpg" /> Message after image.
<strong>Message within strong</strong>
<link:component id="XXX" ... >My Link</link:component>
<p>Message after link component</p>
</root>
OUTPUT:
<root>
This is a test message.
<p>Message within p tag</p> click here.
Message after image.
<strong>Message within strong</strong>
<link:component id="XXX" ... >My Link</link:component>
<p>Message after link component</p>
</root>
Please suggest what I’m doing wrong and tell the best way.
This transformation:
when applied on this XML document (none provided by the OP !!!):
produces the wanted, correct result: