【问题标题】:How to specify preceding-sibling inside for-each loop over a variable如何在 for-each 循环中对变量指定前置兄弟
【发布时间】:2015-07-02 05:47:57
【问题描述】:

当我尝试在 XSLT 2.0 的 xsl:for-each 循环中使用 XPath 前同级时遇到语法错误。我见过的所有例子都没有解决这个具体案例。我正在迭代变量 $v1 中的一系列元素。下面是 XSLT 的结构:

<xsl:for-each select="$v1/*">
    <xsl:choose>
      <!-- minus in previous element means loss -->
      <xsl:when 
          test="matches(preceding-sibling::.[1]/.string(), '-')">     
        <xsl:copy>
          <xsl:sequence 
              select="concat('Loss: ', ./string()"/>        
        </xsl:copy>
      </xsl:when>
      <!-- just copy node and children -->
      <xsl:otherwise>
        <xsl:sequence select="."/>
      </xsl:otherwise>
    </xsl:choose>
</xsl:for-each>

这是运行代码的简化。工作版本还有其他可以正确编译的when 子句。我只显示给我编译时错误的when 子句。

错误是指Unexpected token "." after axis name。当我引用当前节点时,我使用matches(./string(), '(\(|\))'),我没有问题。为什么我不能对上一个节点使用相同的.

我正在使用 Saxon HE9.5.1.4。

【问题讨论】:

    标签: xslt xpath


    【解决方案1】:

    当我引用当前节点时,我使用matches(./string(), '(\(|\))'),我没有问题。为什么我不能对上一个节点使用相同的.

    恕我直言,应用与您上面提到的 preceding-sibling 相同的概念 ./string() 将导致以下表达式,而不是 preceding-sibling::.[1]/.string()

    ./preceding-sibling::*[1]/string()
    

    【讨论】:

      【解决方案2】:

      您可能只希望matches(preceding-sibling::*[1], '-') 匹配紧接在前的同级元素节点或matches(preceding-sibling::node()[1], '-') 匹配任何类型的紧接在前的节点,而不是matches(preceding-sibling::.[1]/.string(), '-')。无需拨打string()

      【讨论】:

        【解决方案3】:

        此语法适用于我的情况

        matches(preceding-sibling::td[1]/string(), '-')
        

        我正在我的for-each 循环中转换一系列&lt;td&gt; 元素。

        【讨论】:

          猜你喜欢
          • 2015-12-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-11-12
          • 1970-01-01
          • 2016-09-28
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多