2012-12-27 61 views
2

在我的Sharepoint fldtypes_custom.xsl文件中,我有這個代碼,它完美的工作。但是,我想在三個或四個相似的字段上使用相同的代碼。如何匹配xsl模板中的這個OR?

有沒有一種方法可以在同一個模板中匹配status1status2或OR status3字段?現在我必須有這個代碼塊的三個副本,其中唯一的區別是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> 
+0

類似到http://stackoverflow.com/q/616427/2047725 – w5m

+0

只想鏈接兩個爲未來讀者的利益 – w5m

+0

啊,臨屋區那麼。我衷心贊同鏈接問題。 – bgmCoder

回答

2

有沒有一種方法我可以匹配評爲同一模板STATUS1或狀態2,OR STATUS3 領域?

使用

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

然而,我從所提供的代碼中看到,這些字符串"status1""status2""status3"不是元素名稱 - 他們的只是可能的值FieldRef元素的Name屬性。

在這種情況下,您的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> 
+0

謝謝,Dimitre。這正是我所期待的。現在,在'test'代碼中,我也將字段的值變成類似這樣的變量:''Is there一種在那裏使用通配符來獲取基於'fieldref'匹配的值的方法? – bgmCoder

+0

@BGM,如果保證'$ thisNode'中包含的元素只有一個hame以字符串'「status」開頭的屬性,那麼執行:'$ thisNode/@ * [starts-with(name(), 'status')]'。目前的問題已經得到完全解答,所以請*接受*並提出任何新問題作爲......一個新問題。 –

+0

對不起 - 沒有必要討厭我。無論如何,我會標記它,無論你是否添加了其他評論,因爲你確實回答了。 – bgmCoder