dowdw44426 2013-05-25 17:18
浏览 42
已采纳

为什么不能在XSL模板选择器谓词中使用变量?

Given this XML;

<root>
  <foo x='1'/>
  <foo x='3'/>
  <foo x='7'/>
</root>

and this stylesheet;

<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="root">
    <result>
      <xsl:apply-templates select="foo"/>
    </result>
  </xsl:template>

  <xsl:template match="foo[@x > 2]">
    <xsl:copy-of select="."/>
  </xsl:template>

</xsl:transform>

I get the desired result;

<result>
  <bar x="3"/>
  <bar x="7"/>
</result>

But if the template match for foo is changed to use a variable $i instead of a constant;

<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:variable name="i" select="2"/>  

  <xsl:template match="root">
    <result>
      <xsl:apply-templates select="foo"/>
    </result>
  </xsl:template>

  <xsl:template match="foo[@x > $i]">
    <xsl:copy-of select="."/>
  </xsl:template>

</xsl:transform>

then I get this error;

XSLTProcessor::importStylesheet(): compilation error: Failed to compile predicate

Am I doing something wrong or can't variables be used in that way?

I have tried declaring the variable in other ways eg;

  <xsl:variable name="i" select="2"/>
  <xsl:variable name="i">2<xsl:variable>

but it always fails to compile the stylesheet.

I'm using the PHP XSL 1.0 processor libxslt;

PHP Version      5.3.2
libxslt Version  1.1.23
  • 写回答

1条回答 默认 最新

  • dougu3988 2013-05-28 22:46
    关注

    No, variables cannot be referred to in template match patterns (or in xsl:key instructions).

    Why not? Because the declaration of a variable is allowed to contain a call to xsl:apply-templates -- so allowing variable references in template match patterns would make circular variable declarations possible.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致