XSLT RSS FEED - Combine substring-before and substring-after

十年热恋 提交于 2019-12-25 03:49:14

问题


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, '&ltb&gt' />



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

回答1:


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).




回答2:


May be this can help:

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

Result against your sample is:

This is my primary titleThis is my primary title


来源:https://stackoverflow.com/questions/5100482/xslt-rss-feed-combine-substring-before-and-substring-after

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!