Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
599 views
in Technique[技术] by (71.8m points)

xslt - How to match this OR that in an xsl template?

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.

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> 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Is there a way I can match fields named status1 OR status2, OR status3 in the same template?

Use:

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

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.

In this case, your template could be:

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

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...