如何在变量中为每个循环指定previous-sibling

时间:2015-06-24 09:56:42

标签: xslt xpath

当我尝试在XSLT 2.0中的xsl:for-each循环中使用XPath前兄弟时,我遇到了语法错误。我见过的所有例子都没有解决这个具体案例。我在变量$ v1中迭代了一系列elemente。以下是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。

3 个答案:

答案 0 :(得分:0)

  

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

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

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

此语法适用于我的案例

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

我正在转换<td>循环中的for-each元素序列。

相关问题