【问题标题】:XSLT RSS FEED - Combine substring-before and substring-afterXSLT RSS FEED - 结合 substring-before 和 substring-after
【发布时间】: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, '&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> 

【问题讨论】:

  • 不要将未解析的数据用作可解析的数据。 Atom 正确处理嵌入的 XHTML 内容。 “伪 XHTML”的字符串处理与使用 RegExp 解析相同......但更糟!

标签: xslt rss


【解决方案1】:

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

【讨论】:

  • 是的,这似乎可以解决问题。我很惊讶这个答案的速度有多快,谢谢大家,我真的很感激。但是,我刚刚意识到我的 xml 'Primary Title:' 和 'Title:' 中有两个相似的标签,如果 Primary Title 出现在第一位,那么是否有办法获取标题之间的内容。 主标题:主标题文本标题:标题文本
【解决方案2】:

这可能会有所帮助:

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

【讨论】:

  • 是的,这似乎可以解决问题。我很惊讶这个问题得到了这么快的答复,谢谢大家,我真的很感激。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-18
  • 2014-04-08
  • 1970-01-01
相关资源
最近更新 更多