我有一个模板:
<xsl:template match="paragraph">
...
</xsl:template>
Run Code Online (Sandbox Code Playgroud)
我称之为:
<xsl:apply-templates select="paragraph"/>
Run Code Online (Sandbox Code Playgroud)
对于我需要做的第一个元素:
<xsl:template match="paragraph[1]">
...
<xsl:apply-templates select="."/><!-- I understand that this does not work -->
...
</xsl:template>
Run Code Online (Sandbox Code Playgroud)
如何从模板中调用<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>
Run Code Online (Sandbox Code Playgroud)
一种方法是使用命名模板,并让第一段和其他段落都调用这个命名模板.
<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>
Run Code Online (Sandbox Code Playgroud)
另一种方法是分别为第一段和其他段落调用apply-templates
<!-- First Paragraph -->
<xsl:apply-templates select="Paragraph[1]"/>
<!-- Other Paragraphs -->
<xsl:apply-templates select="Paragraph[position() != 1]"/>
Run Code Online (Sandbox Code Playgroud)