【发布时间】:2011-02-24 04:50:07
【问题描述】:
如果这个问题真的很简单,我提前道歉,但我似乎找不到解决这个问题的方法。
我需要一种方法来组合 xsl 中的 substring-before 和 substring-after 函数,因此我在 RSS 提要的描述元素中有一个起点和终点。
在每个描述标签中,我想从“主要标题”开始提取所有内容,但一旦到达第一个 <b> 标签就停止。
我尝试了以下xsl但没有成功
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="channel">
<xsl:for-each select="item">
<xsl:value-of select=substring-after(description, 'Primary Title:' />
<xsl:value-of select=substring-before(description, '<b>' />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
以下是我目前正在使用的 XML。
<rss version="2.0">
<channel>
<item>
<title>Article_110224_081057</title>
<description>
<![CDATA[<div><b>Description:</b>This is my description<b>Primary Title:</b>This is my primary title<b>Second Title:</b>This is my second title title </div>
]]>
</description>
</item>
<item>
<title>Article_110224_081057</title>
<description>
<![CDATA[<div><b>Description:</b>This is my description<b>Other Title:</b>This is my other title<b>Second Title:</b>This is my second title titleb<b>Primary Title:</b>This is my primary title<b> more text </div>
]]>
</description>
</item>
</channel>
</rss>
【问题讨论】:
-
不要将未解析的数据用作可解析的数据。 Atom 正确处理嵌入的 XHTML 内容。 “伪 XHTML”的字符串处理与使用 RegExp 解析相同......但更糟!