删除一些< br />来自XML天气馈送的标记 [英] Remove some <br /> tags from XML weather feed
问题描述
我试图通过RSS XML格式化我的网站上的天气信息,这很容易实现。麻烦的是,它呈现在页面之上,我想让它在页面上运行。
从我可以收集的信息看,我似乎需要摆脱< br />< br />
$ b $
$ b
XML源代码如下所示: b
< description>
<![CDATA [< b>星期三< / b>
< br />
< img src =/ images / icons / fcast_30 / mostly_cloudy.gif>
< br />多云< br /> -1&#176; C - 14&#176; C
< br />< br />
< b>星期四< / b>
< br />
< img src =/ icons / fcast_30 / frost_then_sunny.gif>
< br />弗罗斯特然后晴天< br /> 1&#176; C - 13&#176; C
< br />< br />
< b>星期五< / b>
< br />
< img src =/ images / icons / fcast_30 / rain.gif>
< br />雨< br /> 2&#176; C - 11&#176; C
< br />< br />
< b>星期六< / b>
< br />
< img src =/ images / icons / fcast_30 / clearing_shower.gif>
< br />清洁淋浴< br /> 0&#176; C-12&#176; C
< br />< br /> ]>
< / description>
我的输出XSL代码看起来有点像这样:
< xsl:for-each select =rss / channel>
< xsl:value-of select =(item / title)[2]/>
< xsl:value-of select =substring((item / description)[1],30,15)disable-output-escaping =yes/>
< xsl:value-of select =(item / description)[2]disable-output-escaping =yes/>
< / xsl:for-each>
输出结果如下所示:
周三
IMG.gif
大部分阴天
-1°C - 14°C
星期四
IMG.gif
弗罗斯特然后晴天
1° C - 13°C
Friday
IMG.gif
雨
2°C - 11°C
周六
IMG.gif
结算淋浴
0°C - 12°C
我会如何去除< br />< br />< / code>标记,以便它们彼此相邻,但留下其他单数
< br>
tags?
正如其他人所指出的那样,想要的转换是不可能的,除非CDATA部分(这使得标记只是普通的,简单的,一维文本)被移除。
$ b
完整的转换 :
< xsl:stylesheet version =1.0xmlns:xsl =http:// www .w3.org / 1999 / XSL /变换>
< xsl:output omit-xml-declaration =yesindent =yes/>
< xsl:template match =node()| @ *>
< xsl:copy>
< xsl:apply-templates select =node()| @ */>
< / xsl:copy>
< / xsl:template>
br [前置sibling :: node()
[self :: *或self :: text()[normalize-space ()]
[1]
[self :: br]
或
following-sibling :: node()
[self :: * or self: :text()[normalize-space()]
] [1]
[self :: br]
]/>
< / xsl:stylesheet>
应用于删除CDATA部分并纠正许多格式错误错误后获得的XML文档:
< description>
< b>星期三< / b>
< br />
< img src =/ images / icons / fcast_30 / mostly_cloudy.gif/>
< br />多云< br /> -1&#176; C - 14&#176; C
< br />< br />
< b>星期四< / b>
< br />
< img src =/ icons / fcast_30 / frost_then_sunny.gif/>
< br />弗罗斯特然后晴天< br /> 1&#176; C - 13&#176; C
< br />< br />
< b>星期五< / b>
< br />
< img src =/ images / icons / fcast_30 / rain.gif/>
< br />雨< br /> 2&#176; C - 11&#176; C
< br />< br />
< b>星期六< / b>
< br />
< img src =/ images / icons / fcast_30 / clearing_shower.gif/>
< br />清洁淋浴< br /> 0&#176; C-12&#176; C
< br />< br />
< / description>
产生想要的正确结果
: < description>
< b>星期三< / b>
< br />
< img src =/ images / icons / fcast_30 / mostly_cloudy.gif/>
< br />多云< br /> -1°C - 14°C
< b>星期四< / b>
< br />
< img src =/ icons / fcast_30 / frost_then_sunny.gif/>
< br />弗罗斯特然后晴天< br /> 1°C - 13°C
< b>星期五< / b>
< br />
< img src =/ images / icons / fcast_30 / rain.gif/>
< br />雨<峰; br /> 2°C - 11°C
< b>星期六< / b>
< br />
< img src =/ images / icons / fcast_30 / clearing_shower.gif/>
< br />清洁淋浴< br /> 0°C - 12°C
< / description>
解释:
-
身份规则 a> 复制原样每个匹配的节点,并为其选择执行。
有一个模板覆盖身份模板。它匹配任何不是纯空白文本节点的第一兄弟(前兄弟或后兄弟)的 -
这个重写模板有空的主体,可以有效地删除任何匹配的
br
元素。
br
一个 br
。
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 across the page.
From what I can gather, it looks as though I need to get rid of the <br /><br />
tags so they will align next to each other.
The XML source looks like this:
<description>
<![CDATA[ <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>
My output XSL code look a little like this:
<xsl:for-each select="rss/channel">
<xsl:value-of select="(item/title)[2]"/>
<xsl:value-of select="substring((item/description)[1],30,15)" disable-output-escaping="yes"/>
<xsl:value-of select="(item/description)[2]" disable-output-escaping="yes"/>
</xsl:for-each>
And the output renders to look like this:
Wednesday
IMG.gif
Mostly cloudy
-1°C - 14°C
Thursday
IMG.gif
Frost then sunny
1°C - 13°C
Friday
IMG.gif
Rain
2°C - 11°C
Saturday
IMG.gif
Clearing shower
0°C - 12°C
How would I go about removing the <br /><br />
tags after each day so they align next to each other, but leaving the other singular <br>
tags?
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.
这篇关于删除一些< br />来自XML天气馈送的标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!