retain HTML tags after xslt
Clash Royale CLAN TAG#URR8PPP
retain HTML tags after xslt
i have following xslt parsing tree.
<xsl:template match="div[@class='tr-summaryinfo']">
<ul>
<xsl:apply-templates/>
</ul>
</xsl:template>
<xsl:template match="p[@class='tr-summaryitem']">
<li>
<xsl:apply-templates/>
</li>
</xsl:template>
<xsl:template match="body">
<div id="storybodycontent">
<span class="storycontent">
<xsl:copy-of select="node()"/>
</span>
</div>
</xsl:template>
input is:
<html test="http://www.w3.org/1999/xhtml">
<head>
<title>US STOCKS-PepsiCo, oil help extend Wall St rally; S&P at 4-month high</title>
</head>
<body>
<div class="tr-summaryinfo">
<p class="tr-summaryitem">Energy shares jump as oil gains on supply disruptions </p>
<p class="tr-summaryitem">PepsiCo's best day in 7 yrs on strong Q2</p>
<p class="tr-summaryitem">Nordstrom down on disappointing forecast</p>
<p class="tr-summaryitem">S&P 500 Q2 earnings growth seen at 21 pct -TR I/B/E/S</p>
<p class="tr-summaryitem">Tesla gains on plans to open new plant in Shanghai</p>
<p class="tr-summaryitem">Indexes up: Dow 0.56 pct, S&P 0.30 pct, Nasdaq 0.14 pct</p>
</div>
<p class="tr-advisory">Changes comment, adds details, updates prices</p><p class="tr-by">By Amy Caren Daniel</p><p class="tr-story-p1"><span class="tr-dateline">July 10 (Reuters)</span><span class="tr-dl-sep"> - </span>
i want to retain some tags as it is after parsing. BUt my current parsing returns plain text.
<div class="tr-summaryinfo">
and <p class="tr-summaryitem">
being converted as expected, rest of the content looses its tags returned as plain text.
<div class="tr-summaryinfo">
<p class="tr-summaryitem">
<xsl:copy-of select="node()"/>
returns all tags as they are, but vomits transformation.
<xsl:copy-of select="node()"/>
please help.**
1 Answer
1
Since you need to retain some tags as is in the output, you can start with an identity transform template
which copies the input as is to the output.
identity transform template
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
In the template matching body
use <xsl:apply-templates>
instead of <xsl:copy-of>
.
body
<xsl:apply-templates>
<xsl:copy-of>
<xsl:template match="body">
<div id="storybodycontent">
<span class="storycontent">
<xsl:apply-templates />
</span>
</div>
</xsl:template>
The complete XSLT will look like
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" />
<xsl:strip-space elements="*" />
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="body">
<div id="storybodycontent">
<span class="storycontent">
<xsl:apply-templates />
</span>
</div>
</xsl:template>
<xsl:template match="div[@class='tr-summaryinfo']">
<ul>
<xsl:apply-templates />
</ul>
</xsl:template>
<xsl:template match="p[@class='tr-summaryitem']">
<li>
<xsl:apply-templates />
</li>
</xsl:template>
</xsl:stylesheet>
This should give the required output.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.