开发者

How to detect content of XML nodes with same name during XSL "apply-template"

It's been a while since I've done any XSL, so forgive me if this is a little confusing.

I have an XML file, whi开发者_高级运维ch I need to transform with XSL. It all works well until I try and detect "category", because there are multiple "category" nodes for each item.

Part of XML file:

<ROOT> 
<blog day="20" id="4" live="201007200947" month="7" monthname="July" year="2010">
    <blog_date><![CDATA[2010-07-20 09:47:00.0]]></blog_date>
    <filepath><![CDATA[2010/20100720_4.htm]]></filepath>
    <title><![CDATA[Blog title 1]]></title>
    <category><![CDATA[Testing]]></category>
    <category><![CDATA[Training]]></category>   
    <author_id><![CDATA[146]]></author_id>
    <keywords/>
    <summary><![CDATA[New British Gas Smart Metering recruits Paul Williams and Alex Egan give their first impressions.]]></summary>
</blog> 

Sample of XSL file

  <xsl:template match="//ROOT">
    <xsl:apply-templates select="//blog
                       [@live &lt;= $ThisDate]
                       [not($AuthorId) or (author_id = $AuthorId)]
                       [not($BlogYear) or (not($BlogMonth) or (@month = $BlogMonth and @year = $BlogYear))]
        [not($BlogCategory) or (translate(translate(category, $uppercase, $lowercase),$specialchar,'') = $BlogCategory)]
                       ">
        <xsl:sort value="blog_date" order="descending"/>
     </xsl:apply-templates>

$BlogCategory is sent in a url, and has all special characters and spaces stripped out (hence the "translate" on "category").

If I filter the XML by category "Testing" then it works fine, but if I sort by category "Training" it returns no values. I know this is because it's only looking at the first node with the name "category", but can anyone suggest a fix?

I've tried a for-each loop in the past, and it works but is not ideal because the position() always equals 1, so adding page navigation within the xsl file is not possible.

Ideally I would like a fix that could stay within the apply-template.

I'm using XSLT 2.0.

Thanks, Kate


You need to change the filter slightly. The following should do the trick as it will select all blog nodes that have a child category whose value translates to the values of $BlogCategory:

category[translate(translate(., $uppercase, $lowercase),$specialchar,'') = $BlogCategory]]
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜