Search code examples
xsltxsl-fo

include chunk of xsl


I have a chunk of XSL code (along with Apache fop), that i want to reuse:

XSL version and schema used

<xsl:stylesheet version="3.0" xml:lang="en"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fo="http://www.w3.org/1999/XSL/Format"
    xmlns:exsl="http://exslt.org/common"
    xmlns:java="http://xml.apache.org/xslt/java"
    exclude-result-prefixes="java"      
    xmlns:map="xalan://java.util.HashMap" extension-element-prefixes="map">

Chunk to reuse:

    <xsl:choose>
        <xsl:when test="map:get($etiquetas,'datosMensajeLabel')" />

        <fo:table-cell padding="2pt" border-top-color="black"
            border-top-width="0.5pt" border-top-style="solid"
            background-color="grey">
            <fo:block text-align="start" color="white" font-size="12pt">
                <xsl:value-of
                    select="map:put($etiquetas,'datosMensajeLabel','')" />
                <xsl:value-of
                    select="map:get($etiquetas,'datosMensajeLabel')" />
            </fo:block>
        </fo:table-cell>
    </xsl:choose>
    <xsl:otherwhise>

        <fo:table-cell padding="2pt" background-color="grey">
            <fo:block text-align="start" color="white" font-size="12pt">
                
                <xsl:value-of
                    select="map:get($etiquetas,'datosMensajeLabel')" />
            </fo:block>
        </fo:table-cell>
    </xsl:otherwhise>

I tried so far:

  1. Declare and use named templates: it fails since table-cell breaks the Apache fop schema where template is declared
  2. Put that chunk of code in another file and use import or include: it's of no use as it's not used as a child of xsl:stylesheet

Solution

  • The reason you are getting the error fo:table-cell not allowed in that position is that the preceding xsl:when element is closed and therefore not wrapping the <fo:table-cell> element.

    xsl:choose only allows either xsl:when or xsl:otherwise as direct children and thus complains when it finds <fo:table-cell>.

    Wrapping xsl:when around <fo:table-cell> should eliminate the error message.

    By the way, you have misspelt xsl:otherwise