Can I set <xsl:output/> attributes conditionally in an XSLT stylesheet?

107 Views Asked by At

I'm trying to automate some xslt transformations, and I need a way to set xsl:output attributes based on the content in the XML files being transformed.

Specifically, I want to look into the XML files, grab the lang attribute from the root element, and set attributes based on the lang value.

I've tried the following:

<xsl:param name="language">
    <xsl:value-of select="//*/@lang"/>
</xsl:param>

<xsl:output method="xml" xmlns:saxon="http://icl.com/saxon" encoding="utf-8">
    <xsl:choose>
        <xsl:when test="$language != 'ja'">
            <xsl:attribute name="saxon:character-representation"><xsl:value-of select="'native'"/></xsl:attribute>
        </xsl:when>
        <xsl:otherwise>
            <xsl:attribute name="saxon:character-representation"><xsl:value-of select="'hex'"/></xsl:attribute>
        </xsl:otherwise>
    </xsl:choose>
</xsl:output>

...but my parser informs me that the xsl:output element must be empty.

Is there a way to do something like this in the context of a style sheet, or am I going to have to manipulate these attributes at a higher level?

1

There are 1 best solutions below

0
Michael Kay On

You've tagged this docbook, which I suspect is why you're still using the ancient Saxon 6.5.5 processor and its http://icl.com/saxon namespace. However, the docbook stylesheets can be made to work with a modern version of Saxon, which allows you to choose serialization attributes dynamically in an xsl:result-document instruction.

An alternative is to override the xsl:output properties from the Java API or command line. However, that's awkward in your case where you want to make the properties dependent on something in the source document.