如何在xslt中进行递归模板匹配

时间:2018-12-28 14:00:28

标签: xslt xhtml xsl-fo

我有一个带有html的文本,我试图呈现其中的html。但是当内部有嵌套标签时,我会遇到问题。

例如,在我的XML中

<section>
<p><u><em><b>Hello</b></em></u></p>
</section>

在我的XSLT中,我喜欢

<xsl:choose>
<xsl:when test="section/b">
<fo:inline font-weight="bold"><xsl:apply-templates select="*|text()"/>
</fo:inline>
</xsl:when> 
<xsl:when test="section/u">
<fo:inline text-decoration="underline"><xsl:apply- 
templats select="*|text()"/>
</fo:inline>
</xsl:when> 
<xsl:when test="section/em">
<fo:inline font-style="italic"><xsl:apply- 
templats select="*|text()"/>
</fo:inline>
</xsl:when>

<xsl:otherwise>
<xsl:value-of select="section"/>
</xsl:otherwise>
</xsl:choose>

但是它并没有在我的PDF中呈现。

是否有匹配标签的方法或进行递归模板匹配的方法或其他解决方案?

有什么想法/建议吗?

1 个答案:

答案 0 :(得分:2)

使用多个模板,它们将递归应用:

  <xsl:template match="b">
    <fo:inline font-weight="bold">
      <xsl:apply-templates select="*|text()"/>
    </fo:inline>
  </xsl:template>

  <xsl:template match="u">
    <fo:inline text-decoration="underline">
      <xsl:apply-templates select="*|text()"/>
    </fo:inline>
  </xsl:template>

  <xsl:template match="em">
    <fo:inline font-style="italic">
      <xsl:apply-templates select="*|text()"/>
    </fo:inline>
  </xsl:template>
相关问题