XSL使用应用模板和匹配而不是调用模板

时间:2010-04-23 08:16:06

标签: xml xslt apply-templates

我正在尝试从使用调用模板转换到使用applay模板并匹配,但我没有显示任何数据只显示在志愿者标签之间。

当我使用调用模板时它工作正常,但有人建议我使用applay-templates并匹配而不是它不起作用

任何想法如何使这项工作?然后我可以将它应用到我的所有样式表中。

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:key name="volunteers-by-region" match="volunteer" use="region" />

    <xsl:template name="hoo" match="/">
        <html>
            <head>
                <title>Registered Volunteers</title>
                <link rel="stylesheet" type="text/css" href="volunteer.css" />
            </head>
            <body>
                <h1>Registered Volunteers</h1>
                <h3>Ordered by the username ascending</h3>
                <h3>Grouped by the region</h3>

                <xsl:for-each select="folktask/member[user/account/userlevel='2']">

                    <xsl:for-each select="volunteer[count(. | key('volunteers-by-region', region)[1]) = 1]">
                        <xsl:sort select="region" />

                        <xsl:for-each select="key('volunteers-by-region', region)">
                            <xsl:sort select="folktask/member/user/personal/name" />

                            <div class="userdiv">
                                <xsl:apply-templates/>

                                <!--<xsl:call-template name="member_userid">
                                    <xsl:with-param name="myid" select="../user/@id" />
                                </xsl:call-template>

                                <xsl:call-template name="member_name">
                                    <xsl:with-param name="myname" select="../user/personal/name" />
                                </xsl:call-template>-->

                            </div>
                        </xsl:for-each>
                    </xsl:for-each>
                </xsl:for-each>
                <xsl:if test="position()=last()">
                    <div class="count"><h2>Total number of volunteers: <xsl:value-of select="count(/folktask/member/user/account/userlevel[text()=2])"/></h2></div>
                </xsl:if>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="folktask/member">
        <xsl:apply-templates select="user/@id"/>
        <xsl:apply-templates select="user/personal/name"/>
    </xsl:template>

    <xsl:template match="user/@id">
        <div class="heading bold"><h2>USER ID: <xsl:value-of select="." /></h2></div>
    </xsl:template>

    <xsl:template match="user/personal/name">
        <div class="small bold">NAME:</div> 
        <div class="large"><xsl:value-of select="." /></div>
    </xsl:template>


</xsl:stylesheet> 

和我的xml文件

<folktask xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xs:noNamespaceSchemaLocation="folktask.xsd">
    <member>
        <user id="1">
            <personal>
                <name>Abbie Hunt</name>
                <sex>Female</sex>
                <address1>108 Access Road</address1>
                <address2></address2>
                <city>Wells</city>
                <county>Somerset</county>
                <postcode>BA5 8GH</postcode>
                <telephone>01528927616</telephone>
                <mobile>07085252492</mobile>
                <email>adrock@gmail.com</email>
            </personal>
            <account>
                <username>AdRock</username>
                <password>269eb625e2f0cf6fae9a29434c12a89f</password>
                <userlevel>4</userlevel>
                <signupdate>2010-03-26T09:23:50</signupdate>
            </account>
        </user>
        <volunteer id="1">
            <roles></roles>
            <region>South West</region>
        </volunteer>
    </member>
    <member>
        <user id="2">
            <personal>
                <name>Aidan Harris</name>
                <sex>Male</sex>
                <address1>103 Aiken Street</address1>
                <address2></address2>
                <city>Chichester</city>
                <county>Sussex</county>
                <postcode>PO19 4DS</postcode>
                <telephone>01905149894</telephone>
                <mobile>07784467941</mobile>
                <email>ambientexpert@yahoo.co.uk</email>
            </personal>
            <account>
                <username>AmbientExpert</username>
                <password>8e64214160e9dd14ae2a6d9f700004a6</password>
                <userlevel>2</userlevel>
                <signupdate>2010-03-26T09:23:50</signupdate>
            </account>
        </user>
        <volunteer id="2">
            <roles>Van Driver</roles>
            <region>South Central</region>
        </volunteer>
    </member>
    <member>
        <user id="3">
            <personal>
                <name>Skye Saunders</name>
                <sex>Female</sex>
                <address1>31 Anns Court</address1>
                <address2></address2>
                <city>Cirencester</city>
                <county>Gloucestershire</county>
                <postcode>GL7 1JG</postcode>
                <telephone>01958303514</telephone>
                <mobile>07260491667</mobile>
                <email>bigundecided@hotmail.co.uk</email>
            </personal>
            <account>
                <username>BigUndecided</username>
                <password>ea297847f80e046ca24a8621f4068594</password>
                <userlevel>2</userlevel>
                <signupdate>2010-03-26T09:23:50</signupdate>
            </account>
        </user>
        <volunteer id="3">
            <roles>Scaffold Erector</roles>
            <region>South West</region>
        </volunteer>
    </member>
</folktask>

1 个答案:

答案 0 :(得分:2)

问题可能是当您执行 xsl:apply-templates 时,您当前正在 xsl:for-each 循环中运行

<xsl:for-each select="key('volunteers-by-region', region)">

您正在通过志愿者进行循环,因此您当前的背景将成为志愿者。因此,当你想要的是成员元素的细节时,做 xsl:apply-templates 只会匹配志愿者元素中的元素,成员元素是志愿者元素的父元素。因此,您需要告诉 xsl:apply-templates 以匹配父级。这些都应该有效

<xsl:apply-templates select=".." />

<xsl:apply-templates select="parent::*" />