开发者

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

开发者 https://www.devze.com 2023-02-12 21:23 出处:网络
My apologies in advance if this question is really simple, but I can’t seem to find a way around this issue.

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> 


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:'),
                        '&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
0

精彩评论

暂无评论...
验证码 换一张
取 消