测试是否有多个节点具有相同的属性XSLT

时间:2012-12-02 04:02:06

标签: xslt-2.0

XML示例:

    <course>
        <department code="VES">
            <dept_short_name>Visual and Environmental Studies</dept_short_name>
        </department>
        <course_group code="course_group1">Course Group 1</course_group>
        <title>Critical Media Practice: Non Fiction Filmmaking Workshop</title> 
    </course>

    <course>
        <department code="MAT">
            <dept_short_name>Mathematics</dept_short_name>
        </department>
        <course_group code="course_group1">Course Group 1</course_group>
        <title>Critical Media Practice: Non Fiction Filmmaking Workshop</title> 
    </course>

    <course>
        <department code="VES">
            <dept_short_name>Visual and Environmental Studies</dept_short_name>
        </department>
        <course_group code="course_group2">Course Group 2</course_group>
        <title>Critical Media Practice: Non Fiction Filmmaking Workshop</title> 
    </course>
    <course>
        <department code="VES">
            <dept_short_name>Visual and Environmental Studies</dept_short_name>
        </department>
        <course_group code="course_group1">Course Group 1</course_group>
        <title>Critical Media Practice: Non Fiction Filmmaking Workshop</title> 
    </course>

我正在尝试做什么

正如您所看到的,部门@code和course_group @code可能会因每门课程而异。 我想只选择那些部门代码=“VES”的课程,如果他们有多个不同的course_group则调用模板

试过这个代码但是它不起作用 请注意,我有一个参数调用$ department,它已经传递了'VES'值。这个参数它正在工作,我在其他地方使用它没有任何问题。我尝试了以下代码,但它多次调用模板,如果条件为真,我需要只调用一次模板。

<xsl:for-each-group select="course[$department = department/@code]" group-by="course_group[@code]">
            <xsl:if test="count(current-group()) > 1">
                <xsl:call-template name="anchors"/>
            </xsl:if>

</xsl:for-each-group>

感谢您的任何建议

1 个答案:

答案 0 :(得分:0)

  

我想只选择部门code = "VES"的课程   如果他们有多个不同的course_group来电话a   模板

此转化

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
   <xsl:if test="
   distinct-values(/*/course[department
                       [@code eq 'VES']]
                           /course_group/@code
                   )[2]">
     <xsl:call-template name="myTemplate"/>
   </xsl:if>
 </xsl:template>

 <xsl:template name="myTemplate">
   myTemplate is called !
 </xsl:template>
</xsl:stylesheet>

应用于提供的XML文档时:

<t>
    <course>
        <department code="VES">
            <dept_short_name>Visual and Environmental Studies</dept_short_name>
        </department>
        <course_group code="course_group1">Course Group 1</course_group>
        <title>Critical Media Practice: Non Fiction Filmmaking Workshop</title>
    </course>
    <course>
        <department code="MAT">
            <dept_short_name>Mathematics</dept_short_name>
        </department>
        <course_group code="course_group1">Course Group 1</course_group>
        <title>Critical Media Practice: Non Fiction Filmmaking Workshop</title>
    </course>
    <course>
        <department code="VES">
            <dept_short_name>Visual and Environmental Studies</dept_short_name>
        </department>
        <course_group code="course_group2">Course Group 2</course_group>
        <title>Critical Media Practice: Non Fiction Filmmaking Workshop</title>
    </course>
    <course>
        <department code="VES">
            <dept_short_name>Visual and Environmental Studies</dept_short_name>
        </department>
        <course_group code="course_group1">Course Group 1</course_group>
        <title>Critical Media Practice: Non Fiction Filmmaking Workshop</title>
    </course>
</t>

产生了想要的正确结果

   myTemplate is called !

现在我们只需更改

    <course_group code="course_group2">Course Group 2</course_group>

    <course_group code="course_group1">Course Group 1</course_group>

然后转换结果为空 - 完全符合要求