XSLT variable not working
I have below xsl code but it is not working, could anybody please guide me.
<xsl:variable name="indent">
<xsl:if test="@type='Text'">
<xsl:if test="@required='yes'">
<xsl:variable name="indent" select="'return ValidateText(this)'" />
</xsl:if>
<asp:TextBox id="{@id}" onkeypress="{$indent}" runat="server" />
开发者_如何学运维 </xsl:if>
</xsl:variable>
I need to assign return ValidateText(this)
onkeypress even if inside xml the required is yes.
It's hard to get exactly what you are after but I'd make the following guess
<xsl:if test="@type='Text'>
<asp:TextBox id="{@id}" runat="server" >
<xsl:if test="@required='yes'">
<xsl:attribute name="onkeypress">
<xsl:text>return ValidateText(this)</xsl:text>
</xsl:attribute>
</xsl:if>
</asp:TextBox>
</xsl:if>
It is not at all clear what exactly is wanted, but my guess is you want something like this:
<xsl:variable name="indent">
<xsl:choose>
<xsl:when test="@type='Text' and @required='yes'">
<xsl:text>return ValidateText(this)</xsl:text>
</xsl:when>
<xsl:otherwise>someDefault</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<asp:TextBox id="{@id}" onkeypress="{$indent}" runat="server" />
Other one guessing, this stylesheet:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:asp="remove">
<xsl:template match="root">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*/*">
<xsl:variable name="indent">
<xsl:if test="@type='Text'">
<asp:TextBox id="{@id}"
onkeypress="{substring('return ValidateText(this)',
1 div (@required='yes'))}"
runat="server" />
</xsl:if>
</xsl:variable>
<xsl:copy-of select="$indent"/>
</xsl:template>
</xsl:stylesheet>
With this input:
<root>
<form id="form1" type='Text' required="yes"/>
<form id="form2" type='Text' required="no"/>
<form id="form3" type='Input' required="yes"/>
</root>
Output:
<root>
<asp:TextBox id="form1" onkeypress="return ValidateText(this)" runat="server" xmlns:asp="remove" />
<asp:TextBox id="form2" onkeypress="" runat="server" xmlns:asp="remove" />
</root>
精彩评论