第一次出现标签时出现XSLT1错误

时间:2013-09-02 17:24:00

标签: xslt-1.0

这个问题(和我的问题)类似于XSLT1 Get the first occurence of a specific tag ......但我有一个“未定义的变量”错误。

我有一个带refpos属性的XML,可以通过第一个XSLT制作,

  <xsl:template match="p[@class='ref']">
          <xsl:copy>
             <xsl:attribute name="refpos">
               <xsl:value-of select="position()"/>
             </xsl:attribute>
             <xsl:apply-templates />
           </xsl:copy>
  </xsl:template>

      <xsl:template match="@*|node()" name="idt">
         <xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
      </xsl:template>

然后,我有一个主要的(第二个)XSLT,

      <xsl:variable name="firstRef">
         <xsl:value-of select="(//p[@class='ref'])[1]/@refpos"/>
      </xsl:variable>

  <xsl:template match="p[@refpos=$firstRef]">
    <title><xsl:apply-templates /></title>
  </xsl:template>

      <xsl:template match="@*|node()" name="idt">
         <xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
      </xsl:template>

但是这里的XSLT不起作用!!


PS:我也相信XSLT1允许我们一步到位。

1 个答案:

答案 0 :(得分:1)

XSLT 1.0不允许模板匹配表达式中的变量引用(XSLT 2.0)。您必须从模板中的谓词中移动检查:

<xsl:template match="p">
  <xsl:choose>
    <xsl:when test="@refpos=$firstRef">
      <title><xsl:apply-templates /></title>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="idt" />
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>
相关问题