2013-07-01 28 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中关于字符串偏移的文章。