Remove Some
Tags From XML Weather Feed
Solution 1:
As already noted by others, the wanted transformation is impossible, unless the CDATA section (that makes the markup just regular, plain, one-dimensional text) is removed.
This complete transformation:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match=
"br[preceding-sibling::node()
[self::* or self::text()[normalize-space()]
][1]
[self::br]
or
following-sibling::node()
[self::* or self::text()[normalize-space()]
][1]
[self::br]
]"/>
</xsl:stylesheet>
when applied on the XML document obtained after removing the CDATA section and correcting numerous malformedness errors:
<description>
<b>Wednesday</b>
<br />
<img src="/images/icons/fcast_30/mostly_cloudy.gif"/>
<br /> Mostly cloudy<br /> -1°C - 14°C
<br /><br />
<b>Thursday</b>
<br />
<img src="/icons/fcast_30/frost_then_sunny.gif"/>
<br /> Frost then sunny<br /> 1°C - 13°C
<br /><br />
<b>Friday</b>
<br />
<img src="/images/icons/fcast_30/rain.gif"/>
<br /> Rain<br /> 2°C - 11°C
<br /><br />
<b>Saturday</b>
<br />
<img src="/images/icons/fcast_30/clearing_shower.gif"/>
<br /> Clearing shower<br /> 0°C - 12°C
<br /><br />
</description>
produces the wanted, correct result:
<description>
<b>Wednesday</b>
<br/>
<img src="/images/icons/fcast_30/mostly_cloudy.gif"/>
<br/> Mostly cloudy<br/> -1°C - 14°C
<b>Thursday</b>
<br/>
<img src="/icons/fcast_30/frost_then_sunny.gif"/>
<br/> Frost then sunny<br/> 1°C - 13°C
<b>Friday</b>
<br/>
<img src="/images/icons/fcast_30/rain.gif"/>
<br/> Rain<br/> 2°C - 11°C
<b>Saturday</b>
<br/>
<img src="/images/icons/fcast_30/clearing_shower.gif"/>
<br/> Clearing shower<br/> 0°C - 12°C
</description>
Explanation:
The identity rule copies "as-is" every matched node, for which it is selected for execution.
There is a single template overriding the identity template. It matches any
br
whose first sibling (either preceding-sibling or following-sibling) that isn't a whitespace-only text node, is also abr
.This overriding template has empty body, which effectively "deletes" any matched
br
element from the output.
Solution 2:
First of all, DON'T put XML inside a CDATA section unless you are forced to do so by other parties.
Assuming that you have moved the XML content to outside of a CDATA section, apply an XSLT style-sheet with the identity transform and this template ....
<xsl:template match="br[
preceding-sibling::node()[self::br] |
following-sibling::node()[self::br] ]" />
This template will remove adjacent <br> elements. If there is any text between the two <br>s, even just white space, the <br>s will not get removed.
Note
This solution is wrong. See Dimitre's answer.
Post a Comment for "Remove Some
Tags From XML Weather Feed"