0

I am new to xslt and I am wondering whether it is possible to compare the value of "@userNameKey" and the value of <xsl:value-of select="./text()"/> in example below?

 <xsl:if test="@userNameKey='??????'">
 <xsl:attribute name="selected">true</xsl:attribute>
 </xsl:if>
 <xsl:value-of select="./text()"/>

Basically, I just want to replace the questionmarks with the following fragment: <xsl:value-of select="./text()"/> but there is an issue with the double quotes. Should I use escape characters (if yes, what are they?) or there is a better solution?

4

2 回答 2

2

If you specifically want to compare against the value of the first text node child of the current element (which is what <xsl:value-of select="./text()"/> gives you), then use

<xsl:if test="@userNameKey=string(text())">

At first sight

<xsl:if test="@userNameKey=text()">

may seem more obvious, but this is subtly different, returning true if the userNameKey matches any one of the text node children of the current element (not necessarily the first one).

But if (as I suspect you really mean) you want to compare the userNameKey against the complete string value of the element even if that consists of more than one text node, then use

<xsl:if test="@userNameKey=.">

Remember that text() is a node set containing all the text node children of the context node, and if you're not sure you need to use it (e.g. when you want to process each separate text node individually) then you probably don't.

于 2013-06-27T11:00:24.070 回答
0

You should be able to do just this...

<xsl:if test="@userNameKey=./text()">
   <xsl:attribute name="selected">true</xsl:attribute>
</xsl:if>

In fact, the ./ is not needed here, so you can just do this

<xsl:if test="@userNameKey=text()">
   <xsl:attribute name="selected">true</xsl:attribute>
</xsl:if>
于 2013-06-27T10:36:21.363 回答