XSL 使用应用模板和匹配而不是调用模板
我正在尝试从使用调用模板过渡到使用应用模板和匹配,但我没有收到仅显示志愿者标签之间内容的任何数据。
当我使用调用模板时,它工作正常,但建议我使用 applay-templates 和 match,但它不起作用
有什么想法如何使其工作吗?然后我可以将它应用到我的所有样式表中。
<?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>[email protected]</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>[email protected]</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>[email protected]</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>
I am trying to make the transition from using call-template to using applay templates and match but i'm not getting any data displayed only what is between the volunteer tags.
When i use call template it works fine but it was suggested that i use applay-templates and match and not it doesn't work
Any ideas how to make this work? I can then applay it to all my stylesheets.
<?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>
and my xml file
<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>[email protected]</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>[email protected]</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>[email protected]</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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题可能是,当您执行 xsl:apply-templates 时,您当前正在 xsl:for-each 循环中运行
您正在循环志愿者,因此您当前的上下文将是一个志愿者元素。因此,当您想要的是成员元素(即志愿者元素的父元素)的详细信息时,执行 xsl:apply-templates 将仅匹配志愿者元素中的元素。因此,您需要告诉 xsl:apply-templates 来匹配父级。其中任何一个应该有效
或者
The issue may be that when you do the xsl:apply-templates you are currently running within a xsl:for-each loop
You are looping through volunteers, and so your current context will be a volunteer element. Thus, doing xsl:apply-templates will only match elements within the volunteer element, when what you want are details of the member element, which is the parent of the volunteer element. Therefore, you need to tell xsl:apply-templates to match the parent. Either of these should work
or