XSLT RSS FEED - Combine substring-before and substring-after
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).
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:'),
'<b'
),
'b>'
)
"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Result against your sample is:
This is my primary titleThis is my primary title
精彩评论