不能为XSLT变量重新赋值,或者可以有条件地退出for-each循环

本文关键字:或者 有条件 循环 for-each 退出 变量 XSLT 新赋值 赋值 不能 | 更新日期: 2023-09-27 18:13:42

不能为XSLT变量重新赋值,或者可以退出
<xsl:for-each条件
我的XML

<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

     <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的当前id。我想只得到最近的A元素中S元素的列表。

在这个例子中,因为我当前的Sx3333, A的id是x,所以我只需要x111, x222, x333, x3331, x3332在列表中。这意味着我需要去掉这些

  <S id="x444">4</S>,
  <S id="x555">5</S>,
  <S id="x666">6</S>, nodes

从这个代码<xsl:for-each select="A[id='x']//S">

我在A中获得S元素列表,其中id='x'

谁能给我一个解决方案?

不能为XSLT变量重新赋值,或者可以有条件地退出for-each循环

不太清楚你想做什么。
这里有一些提示给你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>

变量current仅在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>

变量value访问错误: <xsl:if test="@currentS = false()">
xslt变量访问使用$前缀。

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

根据我对你的清单的解释,这可能有帮助:

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

在XSLT中处理变量的一种方法是在变量的select中使用XPath if语句:

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

当试图处理一个值应该依赖于if-then构造的变量时,XPath if非常方便。但这只能在XPath 2.0处理器中使用,而OP似乎使用的是1.0处理器。这应该可以工作:

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