XSLT RSS FEED - 结合substring-before和substring-after

时间:2011-02-24 04:50:07

标签: xslt rss

如果这个问题非常简单,我提前道歉,但我似乎找不到解决这个问题的方法。

我需要一种方法来组合xsl中的substring-before和substring-after函数,所以我在RSS feed的description元素中有一个起点和终点。

在每个描述标记中,我想从“主标题”开始提取所有内容,但是一旦到达第一个<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, '&ltb&gt' />



</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> 

2 个答案:

答案 0 :(得分:1)

可能会有所帮助:

<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>
    <xsl:template match="channel">
        <xsl:for-each select="item">
            <xsl:value-of select="
                substring-after(
                    substring-before(
                        substring-after(description, 'Primary Title:'),
                        '&lt;b'
                    ),
                    'b&gt;'
                )
                "/>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

针对您的样本的结果是:

This is my primary titleThis is my primary title

答案 1 :(得分:1)

如果<b>是标记,您将无法使用子字符串匹配找到它,因为标记会被解析器转换为节点。如果不是标记,您将只能将其匹配为子字符串,例如,因为它包含在CDATA部分中(在您的示例中似乎是这种情况)。 / p>

相关问题