我有一个模板:
<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/