dowdw44426 2013-05-25 09: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 14: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.

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部