如何在xsl模板中匹配此OR?

时间:2012-12-27 15:47:03

标签: xslt

在我的Sharepoint fldtypes_custom.xsl文件中,我有这个代码,它完美无缺。但是,我想在三个或四个相似的字段上使用相同的代码。

有没有办法可以在同一个模板中匹配名为status1status2status3的字段?现在我必须有这个代码块的三个副本,唯一的区别是fieldref名称。我想联合代码。

<xsl:template match="FieldRef[@Name='status1']" mode="body">
    <xsl:param name="thisNode" select="."/>
    <xsl:variable name="currentValue" select="$thisNode/@status1" />
    <xsl:variable name="statusRating1">(1)</xsl:variable>
    <xsl:variable name="statusRating2">(2)</xsl:variable>
    <xsl:variable name="statusRating3">(3)</xsl:variable>

    <xsl:choose>
        <xsl:when test="contains($currentValue, $statusRating1)">
            <span class="statusRatingX statusRating1"></span>
        </xsl:when>
        <xsl:when test="contains($currentValue, $statusRating2)">
            <span class="statusRatingX statusRating2"></span>
        </xsl:when> 
        <xsl:when test="contains($currentValue, $statusRating3)">
            <span class="statusRatingX statusRating3"></span>
        </xsl:when> 
        <xsl:otherwise>
            <span class="statusRatingN"></span>
        </xsl:otherwise>                    
    </xsl:choose>
</xsl:template> 

1 个答案:

答案 0 :(得分:2)

  

有没有办法可以匹配名为status1 OR status2,OR status3的字段   在同一个模板中?

使用

<xsl:template match="status1 | status2 | status3">
  <!-- Your processing here -->
</xsl:template>

但是,我从提供的代码中看到,字符串"status1""status2""status3"不是元素名称 - 它们是可能的Name元素的FieldRef属性的值。

在这种情况下,你的tempalte可能是

<xsl:template match="FieldRef
     [@Name = 'status1' or @Name = 'status2' or @Name = 'status3']">
  <!-- Your processing here -->
</xsl:template>

如果Name属性有许多可能的值,可以使用以下缩写

<xsl:template match="FieldRef
     [contains('|status1|status2|staus3|', concat('|',@Name, '|'))]">
  <!-- Your processing here -->
</xsl:template>