0

This question (and my problem) is similar to XSLT1 Get the first occurence of a specific tag... But I have an "Undefined variable" error.

I have a XML with refpos attribute, that can be make by this first 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>

Then, I a main (second) XSLT I have,

      <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>

But here this XSLT not works!!


PS: I also believe that XSLT1 allow us to do everything in one step.

4

1 回答 1

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>
于 2013-09-02T17:48:32.360 回答