Skip to content Skip to sidebar Skip to footer

Remove Some
Tags From Xml Weather Feed

I'm trying to format a weather feed on my site via an RSS XML, which is easily acheived. The trouble is that it renders cascading down the page, and I want to have it running acros

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:stylesheetversion="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:outputomit-xml-declaration="yes"indent="yes"/><xsl:strip-spaceelements="*"/><xsl:templatematch="node()|@*"><xsl:copy><xsl:apply-templatesselect="node()|@*"/></xsl:copy></xsl:template><xsl:templatematch=
 "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 /><imgsrc="/images/icons/fcast_30/mostly_cloudy.gif"/><br /> Mostly cloudy<br /> -1&#176;C - 14&#176;C
        <br /><br /><b>Thursday</b><br /><imgsrc="/icons/fcast_30/frost_then_sunny.gif"/><br /> Frost then sunny<br /> 1&#176;C - 13&#176;C
        <br /><br /><b>Friday</b><br /><imgsrc="/images/icons/fcast_30/rain.gif"/><br /> Rain<br /> 2&#176;C - 11&#176;C
        <br /><br /><b>Saturday</b><br /><imgsrc="/images/icons/fcast_30/clearing_shower.gif"/><br /> Clearing shower<br /> 0&#176;C - 12&#176;C
        <br /><br /></description>

produces the wanted, correct result:

<description><b>Wednesday</b><br/><imgsrc="/images/icons/fcast_30/mostly_cloudy.gif"/><br/> Mostly cloudy<br/> -1°C - 14°C
            <b>Thursday</b><br/><imgsrc="/icons/fcast_30/frost_then_sunny.gif"/><br/> Frost then sunny<br/> 1°C - 13°C
            <b>Friday</b><br/><imgsrc="/images/icons/fcast_30/rain.gif"/><br/> Rain<br/> 2°C - 11°C
            <b>Saturday</b><br/><imgsrc="/images/icons/fcast_30/clearing_shower.gif"/><br/> Clearing shower<br/> 0°C - 12°C
            </description>

Explanation:

  1. The identity rule copies "as-is" every matched node, for which it is selected for execution.

  2. 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 a br.

  3. 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:templatematch="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"