XSLT检查多个节点中的值

时间:2016-10-18 09:26:17

标签: xml xslt oxygenxml

我有以下XML,我需要使用XSLT基于子节点属性提取param值。 在这种情况下,提取我的学生参与的活动列表:

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>

我有以下两个问题:

  1. 首先显示XSLT&#34;活动:&#34;两次为每个节点做循环。
  2. 不显示活动名称

1 个答案:

答案 0 :(得分:2)

最好使用 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>