2013-05-14 89 views
0

不能重新分配一個值,XSLT變量從
<xsl:for-each有條件
我的XML無法從for-each循環有條件

<R> 
    <A id="a"> 
    <S id="a111">1</S> 
    <S id="a222">2</S> 
    <S id="a333">3 
     <S id="a3331">3.1</S> 
     <S id="a3332">3.2</S> 
     <S id="a3333">3.3</S> 
    </S> 
    <S id="a444">4</S> 
    <S id="a555">5</S> 
    <S id="a666">6</S> 
    </A> 
    <A id="x"> 
    <S id="x111">1</S> 
    <S id="x222">2</S> 
    <S id="x333">3 
     <S id="x3331">3.1</S> 
     <S id="x3332">3.2</S> 
     <S id="x3333">3.3</S> 
    </S> 
    <S id="x444">4</S> 
    <S id="x555">5</S> 
    <S id="x666">6</S> 
    </A> 
</R> 
重新分配一個值,XSLT變量,或者我可以退出,或者我可以退出

我的XSLT

 <select id ="S" name="SList"> 
    <option> 
     <xsl:attribute name="value"></xsl:attribute> Please Select S 
    </option> 
    <xsl:for-each select="A[id='x']//S"> 

     <xsl:if test="'x3333'= @id"> 
     <xsl:variable name="currentS" select="true()"/> 
     </xsl:if> 
     <xsl:if test="'x3333'!= @id"> 
     <xsl:variable name="currentS" select="false()"/> 
     </xsl:if> 

     <xsl:if test="@currentS = false()"> 
     <option> 
      <xsl:if test="@id = 'x3333'"> 
      <xsl:attribute name="selected">selected</xsl:attribute> 
      </xsl:if> 
      <xsl:attribute name="value"> 
      <xsl:value-of select="@id"/> 
      </xsl:attribute>    
      <xsl:value-of select="Text"/> 
     </option> 
     </xsl:if> 
    </xsl:for-each> 

我試圖創建一個下拉列表,我需要在列表中包含S項目。我通過當前位置(當前位置S,在此示例中爲x3333),當前編號爲A。我想在最近的A元素中只獲得S元素的列表。

在這個例子中,因爲我現在的Sx3333A ID是x我只需要x111x222x333x3331x3332是在列表中。這意味着我需要消除這些

<S id="x444">4</S>, 
    <S id="x555">5</S>, 
    <S id="x666">6</S>, nodes 
從這個代碼 <xsl:for-each select="A[id='x']//S">

我越來越S元素的列表

A其中id='x'

有人建議我一個解決方案嗎?做你想要用XSLT變量做什麼

回答

0

這是不是很清楚你試圖做什麼。
這裏是一些提示,你XSLT:

屬性的錯誤的訪問:<xsl:for-each select="A[id='x']//S">
要測試屬性ID使用@id:<xsl:for-each select="A[@id='x']//S">
你不能重新定義一個變量:

<xsl:if test="'x3333'= @id"> 
     <xsl:variable name="currentS" select="true()"/> 
</xsl:if> 
<xsl:if test="'x3333'!= @id"> 
    <xsl:variable name="currentS" select="false()"/> 
</xsl:if> 

變量currentS只在xsl:if的範圍內定義。 要設置根據屬性ID的變量值試試這個:

 <xsl:variable name="currentS"> 
      <xsl:choose> 
       <xsl:when test="@id = 'x3333'"> 
        <xsl:text>true</xsl:text> 
       </xsl:when> 
       <xsl:otherwise> 
        <xsl:text>false</xsl:text> 
       </xsl:otherwise> 
      </xsl:choose> 
     </xsl:variable> 

變量值的錯誤訪問:<xsl:if test="@currentS = false()">
XSLT變量訪問做WINT $前綴。

<xsl:if test="$currentS = 'false'"> 

根據我對你的XLST的解釋,這可能幫助:

<?xml version="1.0" encoding="iso-8859-1"?> 
<xsl:stylesheet version="1.0" 
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > 
    <xsl:template match="R"> 

     <select id ="S" name="SList"> 
      <option> 
       <xsl:attribute name="value"></xsl:attribute> Please Select S 
      </option> 
      <xsl:for-each select="A[@id='x']//S"> 
       <xsl:variable name="currentS"> 
       <xsl:choose> 
        <xsl:when test="preceding::S[@id = 'x3333']"> 
         <xsl:text>true</xsl:text> 
        </xsl:when> 
        <xsl:otherwise> 
         <xsl:text>false</xsl:text> 
        </xsl:otherwise> 
       </xsl:choose> 

      </xsl:variable> 
       <xsl:if test="$currentS = 'false'"> 
        <option> 
         <xsl:if test="@id = 'x3333'"> 
          <xsl:attribute name="selected">selected</xsl:attribute> 
         </xsl:if> 
         <xsl:attribute name="value"> 
          <xsl:value-of select="@id"/> 
         </xsl:attribute> 
         <xsl:value-of select="normalize-space(text())"/> 
        </option> 
       </xsl:if> 
      </xsl:for-each> 
     </select> 
    </xsl:template> 
</xsl:stylesheet> 

能產生以下的輸出:

<?xml version="1.0"?> 
<select id="S" name="SList"> 
    <option value=""> 
     Please Select S 
    </option> 
    <option value="x111">1</option> 
    <option value="x222">2</option> 
    <option value="x333">3</option> 
    <option value="x3331">3.1</option> 
    <option value="x3332">3.2</option> 
    <option selected="selected" value="x3333">3.3</option> 
</select> 
+0

感謝您回覆@ hr_117,當我嘗試使用此方法訪問變量它說」變量或參數未定義或超出範圍「 – 2013-05-14 13:11:30

+0

請查看更新。 – 2013-05-14 13:17:27

+0

謝謝@ hr_117 ..是的,這是非常好的,適合我的傾斜。謝謝 – 2013-05-14 13:47:19

0

一種方法是使用XPath如果聲明的變量的選擇:

<xsl:variable name="currentS" select=" if (@id = 'x3333') then true() else false()"/> 

如果是想做一個變量,其值時很有一手中的XPath應該依靠一個如果然後建設。但是,這隻適用於XPath 2.0處理器,看起來OP使用1.0處理器。這應該工作:

<xsl:variable name="currentS" select="@id = 'x3333'"/> 
+0

問題是,當我試圖讀取該值的 ?? – 2013-05-14 12:09:22

+0

另外需要注意的是變量的範圍在XSLT中,所以你的currentS變量的作用域只在你的xsl:if中。這是在變量的select中使用XPath的另一個原因:您可以輕鬆擴展變量的範圍。 – SpringSteven 2013-05-14 12:09:31

+0

這是因爲在XSLT中,「@」是屬性軸的縮寫。引用你使用「$」的變量。所以應該爲你工作 – SpringSteven 2013-05-14 12:10:22