如何在 xsl 模板中匹配这个或那个? [英] How to match this OR that in an xsl template?
问题描述
在我的 Sharepoint fldtypes_custom.xsl
文件中,我有这个代码,它运行良好.但是,我想在三个或四个相似的字段上使用相同的代码.
In my Sharepoint fldtypes_custom.xsl
file, I have this code, which works perfectly. However, I want to use the same code on three or four similar fields.
有没有一种方法可以匹配同一个模板中名为 status1
OR status2
, OR status3
的字段?现在我必须拥有这段代码的三个副本,唯一的区别是 fieldref
名称.我想合并代码.
Is there a way I can match fields named status1
OR status2
, OR status3
in the same template? Right now I have to have three copies of this block of code where the only difference is the fieldref
name. I would like to consolodate the code.
<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>
推荐答案
有没有办法匹配名为 status1 OR status2, OR status3 的字段在同一个模板中?
Is there a way I can match fields named status1 OR status2, OR status3 in the same template?
使用:
<xsl:template match="status1 | status2 | status3">
<!-- Your processing here -->
</xsl:template>
但是,我从提供的代码中看到,字符串 "status1"
、"status2"
和 "status3"
不是元素名称——它们只是 FieldRef
元素的 Name
属性的可能值.
However, I see from the provided code, that the strings "status1"
, "status2"
and "status3"
aren't element names -- they are just possible values of the Name
attribute of the FieldRef
element.
在这种情况下,您的模板可能是:
<xsl:template match="FieldRef
[@Name = 'status1' or @Name = 'status2' or @Name = 'status3']">
<!-- Your processing here -->
</xsl:template>
如果 Name
属性有多种可能的值,可以使用以下缩写:
In case there are many possible values for the Name
attribute, one can use the following abbreviation:
<xsl:template match="FieldRef
[contains('|status1|status2|staus3|', concat('|',@Name, '|'))]">
<!-- Your processing here -->
</xsl:template>
这篇关于如何在 xsl 模板中匹配这个或那个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!