XSLT RSS FEED - 组合前子串和后子串 [英] XSLT RSS FEED - Combine substring-before and substring-after
问题描述
如果这个问题真的很简单,我提前道歉,但我似乎找不到解决这个问题的方法.
我需要一种方法来组合 xsl 中的 substring-before 和 substring-after 函数,以便我在 RSS 提要的描述元素中有一个起点和终点.
在每个描述标签中,我想从主要标题"开始提取所有内容,但一旦到达第一个 <b>
标签就停止.
我尝试了以下 xsl 没有成功
<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:模板></xsl:stylesheet>
<小时>
以下是我目前正在使用的 XML.
<频道><项目><title>文章_110224_081057</title><说明><![CDATA[<div><b>描述:</b>这是我的描述<b>主标题:</b>这是我的主标题<b>第二标题:</b>这是我的第二个标题</div>]]></描述></项目><项目><title>文章_110224_081057</title><说明><![CDATA[<div><b>描述:</b>这是我的描述<b>其他标题:</b>这是我的另一个标题<b>第二个标题:</b>这是我的第二个头衔b<b>主要头衔:</b>这是我的主要头衔<b>更多文字
]]></描述></项目></频道></rss>
如果 是一个标签,你将无法使用子字符串匹配找到它,因为标签得到被解析器变成节点.如果它不是标记,例如因为它包含在 CDATA 部分(在您的示例中似乎就是这种情况),则您只能将其作为子字符串进行匹配.
My apologies in advance if this question is really simple, but I can’t seem to find a way around this issue.
I need a way to combine the substring-before and substring-after function in xsl so I have a start and end point within a description element of an RSS feed.
In each description tag I want to extract everything from ‘Primary Title’ onwards, but stop as soon as it reaches the first <b>
tag.
I tried the following xsl without much success
<?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>
Below is the XML I am currently working with.
<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>
If the <b>
is a tag, you won't be able to find it using substring matching, because tags get turned into nodes by the parser. You'll only be able to match it as a substring if it isn't a tag, for example because it was contained in a CDATA section (which appears to be the case in your example).
这篇关于XSLT RSS FEED - 组合前子串和后子串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!