XLST 与 Namespace, for-each 和 apply-templates

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    xmlns:msfn="http://www.footnotedpro.com/rss" exclude-result-prefixes="msxsl msfn">

    <msxsl:script implements-prefix="msfn" language="C#">
    <![CDATA[
        public string Now()
        {
           System.Globalization.CultureInfo usCulture = new System.Globalization.CultureInfo("en-US");
           return DateTime.Now.ToString();
        }
    ]]>
    </msxsl:script>
   
    <xsl:output method="xml" indent="yes"/>
    <xsl:param name="rows" select="5"/>
    <xsl:param name="linkPrefix">http://www.footnotedpro.com/assets/documents/protected/DL.aspx?fn=</xsl:param>
   
    <xsl:template match="/rss/channel">
        <rss version='2.0'
            xmlns:content='http://purl.org/rss/1.0/modules/content/'
            xmlns:dc='http://purl.org/dc/elements/1.1/'
            xmlns:atom='http://www.w3.org/2005/Atom'
            xmlns:sy='http://purl.org/rss/1.0/modules/syndication/'>
            <channel>
                <title>footnotedpro.com &#187; FootnotedPro</title>
                <atom:link href='http://www.footnotedpro.com/rss' rel='self' type='application/rss+xml' />
                <link>http://www.footnotedpro.com</link>
                <description>Michelle Leder&#039;s guide to what&#039;s hiding in SEC filings</description>
                <lastBuildDate>
                    <xsl:value-of select='msfn:Now()'/>
                </lastBuildDate>
                <generator>http://www.footnotedpro.com</generator>
                <language>en</language>
                <sy:updatePeriod>hourly</sy:updatePeriod>
                <sy:updateFrequency>1</sy:updateFrequency>
                <xsl:for-each select="item">
                    <xsl:if test="position() &lt;= $rows">
                        <item>
                            <title>
                                <xsl:value-of select="title"/>
                            </title>
                            <link>
                                <xsl:value-of select="concat($linkPrefix,PDFfile)"/>
                            </link>
                            <pubDate>
                                <xsl:value-of select="pubDate"/>
                            </pubDate>
                            <dc:creator>Michelle Leder</dc:creator>
                            <description>
                                <xsl:value-of select="description"/>
                            </description>
                            <content:encoded>
                                <xsl:value-of select="content"/>
                            </content:encoded>
                        </item>
                    </xsl:if>                     
                </xsl:for-each>
            </channel>
        </rss>
    </xsl:template>
</xsl:stylesheet>  

变为

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    xmlns:msfn="http://www.footnotedpro.com/rss" exclude-result-prefixes="msxsl msfn"
    xmlns:content='http://purl.org/rss/1.0/modules/content/'
    xmlns:dc='http://purl.org/dc/elements/1.1/'
    xmlns:atom='http://www.w3.org/2005/Atom'
    xmlns:sy='http://purl.org/rss/1.0/modules/syndication/'>

    <msxsl:script implements-prefix="msfn" language="C#">
    <![CDATA[
        public string Now()
        {
           System.Globalization.CultureInfo usCulture = new System.Globalization.CultureInfo("en-US");
           return DateTime.Now.ToString();
        }
    ]]>
    </msxsl:script>
   
    <xsl:output method="xml" indent="yes"/>
    <xsl:param name="rows" select="5"/>
    <xsl:param name="linkPrefix">http://www.footnotedpro.com/assets/documents/protected/DL.aspx?fn=</xsl:param>
   
    <xsl:template match="/rss/channel">
        <rss version='2.0'>
            <channel>
                <title>footnotedpro.com &#187; FootnotedPro</title>
                <atom:link href='http://www.footnotedpro.com/rss' rel='self' type='application/rss+xml' />
                <link>http://www.footnotedpro.com</link>
                <description>Michelle Leder&#039;s guide to what&#039;s hiding in SEC filings</description>
                <lastBuildDate>
                    <xsl:value-of select='msfn:Now()'/>
                </lastBuildDate>
                <generator>http://www.footnotedpro.com</generator>
                <language>en</language>
                <sy:updatePeriod>hourly</sy:updatePeriod>
                <sy:updateFrequency>1</sy:updateFrequency>
                <xsl:apply-templates select='item'/>
            </channel>
        </rss>
    </xsl:template>
    <xsl:template match="item">
        <xsl:if test="position() &lt;= $rows">
            <item>
                <title>
                    <xsl:value-of select="title"/>
                </title>
                <link>
                    <xsl:value-of select="concat($linkPrefix,PDFfile)"/>
                </link>
                <pubDate>
                    <xsl:value-of select="pubDate"/>
                </pubDate>
                <dc:creator>Michelle Leder</dc:creator>
                <description>
                    <xsl:value-of select="description"/>
                </description>
                <content:encoded>
                    <xsl:value-of select="content"/>
                </content:encoded>
            </item>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>  

类别: Uncategorized. 阅读(45). 订阅评论. TrackBack.

Leave a Reply

Be the First to Comment!

avatar
wpDiscuz