Search code examples
xsltxhtmlxsl-fo

How to do a recursive template match in xslt


I have a text with html inside it, im trying to render the html present in it. But im facing problem when there are nested tags inside.

For ex: In my XML

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

And in my XSLT i have like

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

But it is not getting this rendered in my PDF.

Is there a way to match tags or any ways to do recursive template matching, or any other solutions?

Any idea/ suggestions?


Solution

  • Use multiple templates, they will apply recursively:

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