开发者

How to compare XML element with XSL variable

I am using xslt to transform an xml document to html for use in an email. I need to compare xml elements with another xml element value开发者_开发技巧 so that I know what format to give the value. Basically I have an xml structure as such:

<main>
    <comparer>1</comparer>
    <items>
        <item>
            <name>blarg</name>
            <values>
                <value>1</value>
                <value>2</value>
            </values>
    </items>
</main>

The item information is being used to build a table:

<table>
    <tr>
        <td>blarg</td>
        <td>1</td>
        <td>2</td>
    </tr>
</table>

What I need to be able to do is use xsl to compare the item values with the 'comparer' node value and if they are equal then bold the cell in the table otherwise the cell value i snot bolded. I need to accomplish this without the use of javascript so it has to be done in xsl. Right now, I am looking at using a xsl:variable then attempting to use the xsl:when to do the compare. Unfortunately, I am having little luck. This is what I have just started playing with for each row in the table:

<xsl:variable name="compare" select="//main/comparer" />

...    

<xsl:for-each select="value">
    <td>
        <xsl:choose>
            <xsl:when test=". = $compare">
                <b>
                    <xsl:value-of select="."/>
                </b>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="."/>.
            </xsl:otherwise>
        </xsl:choose>
    </td>
</xsl:for-each>

*Note: I left out most of the xsl for brevity. I'm just trying to focus on my issue.


I figured it out after some trial and error. Alejandro's answer appears that it would work, but I do not have the luxury of restructuring the xsl to make use of templating. Here is what I used to solve my issue:

<xsl:variable name="compare" select="//main/comparer" />

...    

<xsl:for-each select="value">
    <td>
        <xsl:choose>
            <xsl:when test="contains(., $expireDate)">
                <b>
                    <xsl:value-of select="."/>
                </b>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="."/>.
            </xsl:otherwise>
        </xsl:choose>
    </td>
</xsl:for-each>


This stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="main">
        <table>
            <xsl:apply-templates select="items"/>
        </table>
    </xsl:template>
    <xsl:template match="item">
        <tr>
            <xsl:apply-templates/>
        </tr>
    </xsl:template>
    <xsl:template match="name|value">
        <td>
            <xsl:apply-templates/>
        </td>
    </xsl:template>
    <xsl:template match="value/text()[.=/main/comparer]">
        <b>
            <xsl:value-of select="."/>
        </b>
    </xsl:template>
</xsl:stylesheet>

Output:

<table>
    <tr>
        <td>blarg</td>
        <td>
            <b>1</b>
        </td>
        <td>2</td>
    </tr>
</table>

Note: Pattern matching and node set comparison.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜