2010-09-14 121 views
0

我有下面的xsl代碼,但它不工作,有誰可以請指導我。XSLT變量不能正常工作

<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> 

我需要分配收益ValidateText(this) onkeypress事件即使所需的內部XML是肯定的。

+0

「不工作」,沒有*如何*它不工作的聲明(例如XSLT處理器提供了一個錯誤),似乎表明你希望我們做很多猜測,而不是你清楚地說明問題的工作。 – LarsH 2010-09-14 18:44:55

+0

@LarsH和@Khan:不工作的部分顯然是在其範圍之外使用最內部的'indent'變量。在XSLT 2.0中,這會引發一個錯誤,因爲循環定義:在其自己的內容模板中使用最外面的'indent'值。靜靜地在XSLT中,將變量引用評估爲無。 – 2010-09-14 20:11:30

+0

@Alejandro:是的,我看到了,但我經常看到這個「不起作用」的東西,表現得像是對問題的完整陳述,我認爲值得向Khan指出。爲什麼你和我在做他的調查和打字工作,當他在他面前發生錯誤時?或者至少他可以說縮進變量以空值結束。他有一些他知道它不工作的信息;作爲尋求幫助的人,正派表示他應該分享這些信息。 @Khan,見http://tinyurl.com/so-hints – LarsH 2010-09-14 21:40:58

回答

0

很難得到正是你所追求的,但我想作以下猜測

<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> 
0

它不是完全清楚究竟是想要的,但我的猜測是,你希望是這樣的

<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" /> 
0

其他一個猜測,這個樣式表:

<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> 

有了這個輸入:

<root> 
    <form id="form1" type='Text' required="yes"/> 
    <form id="form2" type='Text' required="no"/> 
    <form id="form3" type='Input' required="yes"/> 
</root> 

輸出:

<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>