XSLT 检查多个节点中的值
XSLT checking values in multiple nodes
我有以下 XML,我需要使用 XSLT 根据子节点属性提取参数值。
在这种情况下,提取我的学生参与的活动列表:
XML
<students>
<student id="1000020001"/>
<student id="1000020002"/>
</students>
<activities>
<activity name="yoga beginners" start="2016-10-12" end="2016-12-17">
<members>
<member id="1000020001"/>
</members>
</activity>
<activity name="yoga intermediate" start="2017-10-12" end="2017-12-17">
<members>
<member id="1000020001"/>
<member id="1000020002"/>
</members>
</activity>
</activities>
我想创建一个 XSLT 来显示我的学生参与的活动,在本例中我有:
XSLT
每个学生:
<xsl:for-each select="/activities/activity">
<b>Activity:</b>:
<xsl:call-template name="extractActivities">
<xsl:with-param name="student-id" select="@id"/>
</xsl:call-template>
</xsl:for-each>
<xsl:template name="extractActivities">
<xsl:param name="student-id"/>
<xsl:if test="$student-id = /activities/activity/members/member/@id">
<xsl:value-of select="@name"/>
</xsl:if>
</xsl:template>
我有以下两个问题:
- 第一个 XSLT 显示 "Activity:" 两次,就像对每个节点执行循环一样。
- 没有显示activity名字
最好使用 key:
来解析交叉引用
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="UTF-8" />
<xsl:key name="act" match="activity" use="members/member/@id" />
<xsl:template match="/root">
<html>
<body>
<xsl:for-each select="students/student">
<h3>Student: <xsl:value-of select="@id" /></h3>
<xsl:for-each select="key('act', @id)">
<b>Activity: </b><xsl:value-of select="@name" />
<br/>
</xsl:for-each>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
应用于以下格式正确的输入:
XML
<root>
<students>
<student id="1000020001"/>
<student id="1000020002"/>
</students>
<activities>
<activity name="yoga beginners" start="2016-10-12" end="2016-12-17">
<members>
<member id="1000020001"/>
</members>
</activity>
<activity name="yoga intermediate" start="2017-10-12" end="2017-12-17">
<members>
<member id="1000020001"/>
<member id="1000020002"/>
</members>
</activity>
</activities>
</root>
结果将是:
<html>
<body>
<h3>Student: 1000020001</h3>
<b>Activity: </b>yoga beginners<br>
<b>Activity: </b>yoga intermediate<br>
<h3>Student: 1000020002</h3>
<b>Activity: </b>yoga intermediate<br>
</body>
</html>
我有以下 XML,我需要使用 XSLT 根据子节点属性提取参数值。 在这种情况下,提取我的学生参与的活动列表:
XML
<students>
<student id="1000020001"/>
<student id="1000020002"/>
</students>
<activities>
<activity name="yoga beginners" start="2016-10-12" end="2016-12-17">
<members>
<member id="1000020001"/>
</members>
</activity>
<activity name="yoga intermediate" start="2017-10-12" end="2017-12-17">
<members>
<member id="1000020001"/>
<member id="1000020002"/>
</members>
</activity>
</activities>
我想创建一个 XSLT 来显示我的学生参与的活动,在本例中我有:
XSLT 每个学生:
<xsl:for-each select="/activities/activity">
<b>Activity:</b>:
<xsl:call-template name="extractActivities">
<xsl:with-param name="student-id" select="@id"/>
</xsl:call-template>
</xsl:for-each>
<xsl:template name="extractActivities">
<xsl:param name="student-id"/>
<xsl:if test="$student-id = /activities/activity/members/member/@id">
<xsl:value-of select="@name"/>
</xsl:if>
</xsl:template>
我有以下两个问题:
- 第一个 XSLT 显示 "Activity:" 两次,就像对每个节点执行循环一样。
- 没有显示activity名字
最好使用 key:
来解析交叉引用XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="UTF-8" />
<xsl:key name="act" match="activity" use="members/member/@id" />
<xsl:template match="/root">
<html>
<body>
<xsl:for-each select="students/student">
<h3>Student: <xsl:value-of select="@id" /></h3>
<xsl:for-each select="key('act', @id)">
<b>Activity: </b><xsl:value-of select="@name" />
<br/>
</xsl:for-each>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
应用于以下格式正确的输入:
XML
<root>
<students>
<student id="1000020001"/>
<student id="1000020002"/>
</students>
<activities>
<activity name="yoga beginners" start="2016-10-12" end="2016-12-17">
<members>
<member id="1000020001"/>
</members>
</activity>
<activity name="yoga intermediate" start="2017-10-12" end="2017-12-17">
<members>
<member id="1000020001"/>
<member id="1000020002"/>
</members>
</activity>
</activities>
</root>
结果将是:
<html>
<body>
<h3>Student: 1000020001</h3>
<b>Activity: </b>yoga beginners<br>
<b>Activity: </b>yoga intermediate<br>
<h3>Student: 1000020002</h3>
<b>Activity: </b>yoga intermediate<br>
</body>
</html>