xslt - 第一个元素的特定模板

标签 xslt optimization

我有一个模板:

<xsl:template match="paragraph">
    ...
</xsl:template>

我称之为:

<xsl:apply-templates select="paragraph"/>

对于我需要做的第一个元素:

<xsl:template match="paragraph[1]">
    ...
    <xsl:apply-templates select="."/><!-- I understand that this does not work -->
    ...
</xsl:template>

如何调用<xsl:apply-templates select="paragraph"/> (对于第一个元素 paragraph )来自模板 <xsl:template match="paragraph[1]">

到目前为止,我有类似循环的东西。

<小时/>

我解决了这个问题(但我不喜欢它):

<xsl:for-each select="paragraph">
    <xsl:choose>
        <xsl:when test="position() = 1">
            ...
            <xsl:apply-templates select="."/>
            ...
        </xsl:when>
        <xsl:otherwise>
            <xsl:apply-templates select="."/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:for-each>

最佳答案

实现此目的的一种方法是使用命名模板,并让第一个段落和其他段落都调用此命名模板。

<xsl:template match="Paragraph[1]">
   <!-- First Paragraph -->
   <xsl:call-template name="Paragraph"/>
</xsl:template>

<xsl:template match="Paragraph">
   <xsl:call-template name="Paragraph"/>
</xsl:template>

<xsl:template name="Paragraph">
   <xsl:value-of select="."/>
</xsl:template>

另一种方法是为第一段和其他段落分别调用 apply-templates

  <!-- First Paragraph -->
  <xsl:apply-templates select="Paragraph[1]"/>

  <!-- Other Paragraphs -->
  <xsl:apply-templates select="Paragraph[position() != 1]"/>

关于xslt - 第一个元素的特定模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2863563/

相关文章:

optimization - 渲染优化

python - Scipy.optimize 最小化花费的时间太长

xml - 如何在 xsl 中检查不区分大小写的字符串相等性

xml - XSLT 删除源的样式表

java - 用动态数据替换占位符

optimization - llvm 使用库函数进行优化

xml - 如何在 XSLT 中创建键值对并使用键获取值

xml - 为什么 XSLT 从未见过互联网繁荣期间出现的许多其他语言的流行?

algorithm - 拼图搜索过程的树表示

javascript - JavaScript 的尾递归优化?