代码之家  ›  专栏  ›  技术社区  ›  AdRock

使用apply templates and match而不是call template的xsl

  •  0
  • AdRock  · 技术社区  · 14 年前

    我正在尝试从使用调用模板过渡到使用applay模板并进行匹配,但是我没有得到任何只显示志愿者标记之间的数据。

    当我使用调用模板时,它工作得很好,但有人建议我使用applay模板并匹配,而不是不工作

    有什么办法让这工作吗?然后我可以将它应用到我的所有样式表中。

    <?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 回复  |  直到 14 年前
        1
  •  2
  •   Tim C    14 年前

    问题可能是当你做 xsl:apply模板 您当前正在 对于每个

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

    你在志愿者中循环,所以你当前的上下文将是一个志愿者元素。这样,做 xsl:apply模板 只匹配志愿者元素中的元素,而您需要的是成员元素的详细信息,成员元素是志愿者元素的父元素。因此,你需要告诉 xsl:apply模板 以匹配父对象。这两个都可以

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

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