2013-07-01 45 views
-1
<xsl:for-each select="$script/startWith"> 
    <xsl:variable name = "i" > 
    <xsl:value-of select="$script/startWith[position()]"/> 
    </xsl:variable> 
    <xsl:for-each select="JeuxDeMots/Element"> 
    <xsl:variable name = "A" > 
     <xsl:value-of select="eName"/> 
    </xsl:variable> 
    <xsl:if test="starts-with($A,$i)= true()"> 
     <xsl:variable name="stringReplace"> 
     <xsl:value-of select="substring($i,0,3)"/> 
     </xsl:variable> 
     <xsl:value-of select="$stringReplace"/> 
    </xsl:if> 
    </xsl:for-each> 
</xsl:for-each> 

問題:變量$ i無法通過每個xsl。 請幫幫我。XSL:如何將變量傳遞給每個

+0

我一直這樣做。嘗試刪除變量聲明中等號周圍的空格: Jay

+0

謝謝,但無法正常工作。 – user2537590

+0

什麼是錯誤信息,你得到了什麼?在哪一行(有兩個引用* i *)? –

回答

0

嘗試用

<xsl:variable name="i" select="string(.)"/> 

替換的i的聲明中的上下文項(即,「」)是用於for-each指令的各評價不同,但表達$script/startWith[position()]的值不改變。 (在這裏,你正在製作一個startWith元素的序列,並測試每個元素的表達式position()的有效布爾值。表達式position()返回一個正整數,所以它的有效布爾值總是爲真所以謂詞[position()]是做no在所有的工作在這裏。

此外,你要替換的stringReplace聲明

<xsl:variable name="stringReplace" select="substring($i,1,3)"/> 

(字符串偏移量從1開始XPath中,不爲0)

我猜你w螞蟻處理$script的所有startWith兒童,並且對於其每一個發出值的前三個字符一次,對於每個startWith/JeuxDeMots /元素,其eName孩子以startWith的值開始。

其實,整件事可能是一個比較容易閱讀,如果它是簡短,更直接的:如果他們被多次使用

<xsl:for-each select="$script/startWith"> 
    <xsl:variable name = "i" select="string()"/> 
    <xsl:for-each select="JeuxDeMots/Element"> 
    <xsl:if test="starts-with(eName,$i)"> 
     <xsl:value-of select="substring($i,1,3)"/> 
    </xsl:if> 
    </xsl:for-each> 
</xsl:for-each> 

它可以完美的創建變量$ A和$ stringReplace在代碼中,你沒有向我們展示,但如果沒有,...

+0

謝謝,但不工作... – user2537590

+0

正確。如果您將上下文節點設置爲script.xml中的startWith元素,然後編寫像JeuxDeMots/Element或root/book這樣的相對XPath表達式,則這些相對XPath表達式不會匹配主輸入文檔中的任何內容。注意上下文節點;除非你這樣做,否則你永遠不會理解XPath。在理解XPath之前,您將永遠無法有效地使用XSLT。 –

0

我認爲問題在於你在第一個for-each中更改上下文。然後元素JeuxDeMots不是每個第二個「可見」的。例如,你可以嘗試將它存儲在變量中,然後在第二個變量中使用這個變量(還有另一種方法來解決這個問題)。

<xsl:template match="/"> 
    <xsl:variable name="doc" select="JeuxDeMots"/> 

    <xsl:choose> 
     <xsl:when test="$script/startWith"> 
      <xsl:for-each select="$script/startWith"> 
       <xsl:variable name="i"> 
        <xsl:value-of select="."/> 
       </xsl:variable> 
       <xsl:for-each select="$doc/Element"> 
        <xsl:variable name="A"> 
         <xsl:value-of select="eName"/> 
        </xsl:variable> 
        <xsl:if test="starts-with($A,$i) = true()"> 
         <xsl:variable name="stringReplace"> 
          <xsl:value-of select="substring($i,0,3)"/> 
         </xsl:variable> 
         <xsl:value-of select="$stringReplace"/> 
        </xsl:if> 
       </xsl:for-each> 
      </xsl:for-each> 
     </xsl:when> 
    </xsl:choose> 
</xsl:template> 

雖然我不確定你在處理什麼,但它似乎輸出所需的值AsAt。

您也可以考慮C.M.Sperberg-McQueen在XPath中關於字符串偏移的文章。