在XSLT中匹配两个字段并获取当前值

时间:2013-08-03 22:22:55

标签: xslt

在我的XSLT中,我有这个函数,它成功匹配/或/和两列。 FieldRef完全匹配。

我的问题是,$currentValue似乎永远不会与我正在测试的内容相同(我正在测试的内容似乎是一个空白字符串)。

我在哪里错了?

<!-- Convert the Fields into a status icons    -->
<xsl:template match="FieldRef[@Name='YesNo']|FieldRef[@Name='TrueFalse']" mode="body">
    <xsl:param name="thisNode" select="." />
    <xsl:variable name="currentValue" select="$thisNode/@*[name()=current()/@Name]" />

    <xsl:choose>
        <xsl:when test="$currentValue='Yes'">
            <span class="yesno yes"><xsl:value-of select="$currentValue" /></span>
        </xsl:when>
        <xsl:when test="$currentValue='No'">
            <span class="yesno no"><xsl:value-of select="$currentValue" /></span>
        </xsl:when>
        <xsl:when test="$currentValue='True'">
            <span class="yesno yes"><xsl:value-of select="$currentValue" /></span>
        </xsl:when>
        <xsl:when test="$currentValue='False'">
            <span class="yesno no"><xsl:value-of select="$currentValue" /></span>
        </xsl:when>     
        <xsl:otherwise>
            <span class="yesnoN"><xsl:value-of select="$currentValue" /></span>
        </xsl:otherwise>
    </xsl:choose>

</xsl:template> 

我知道的一件事是,如果我这样做

<xsl:variable name="thisName" select="./@Name" /> select="./@Name" />

然后尝试使用字段本身的名称(而不是其值)进行匹配。

我该怎么办?

1 个答案:

答案 0 :(得分:1)

啊,经过几个小时和几小时后,这里是:

这两行是关键:

<xsl:param name="thisNode" select="."/>
<xsl:variable name="currentValue" select="$thisNode/@*[name()=current()/@Name]" />

这是整个函数,它读取两个不同的列并将值应用于其中一个:

<xsl:template match="FieldRef[@Name='YesNo1']|FieldRef[@Name='YesNo2']" mode="body">
    <xsl:param name="thisNode" select="."/>

    <xsl:variable name="currentValue" select="$thisNode/@*[name()=current()/@Name]" />
    <xsl:variable name="yesvalue">Yes</xsl:variable>
    <xsl:variable name="novalue">No</xsl:variable>

    <xsl:choose>
        <xsl:when test="contains($currentValue, $yesvalue)">
            <span class="yesno yes"><xsl:value-of select="$currentValue" /></span>
        </xsl:when>
        <xsl:when test="contains($currentValue, $novalue)">
            <span class="yesno no"><xsl:value-of select="$currentValue" /></span>
        </xsl:when>
    <xsl:otherwise>
            <span class="yesnoN"><xsl:value-of select="$currentValue" /></span>
        </xsl:otherwise>
    </xsl:choose>

</xsl:template> 

some other examples of matching on multiple match fields here