using XSLT to insert variables into another XSLT
I recently encountered a need at work to insert an XSL variable into an XSL stylesheet. This is fairly easy to do using XSLT. The transformation shown below takes one XSLT and produces an XSLT result tree with a new XSL variable inserted just below the <xsl:stylesheet> element. In this case, the XSL variable is named locale and has the value en.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="xsl:stylesheet">
<xsl:element name="xsl:stylesheet">
<xsl:for-each select="@*">
<xsl:attribute name="{name()}"><xsl:value-of select="."/></xsl:attribute>
</xsl:for-each>
<xsl:element name="xsl:variable">
<xsl:attribute name="name">locale</xsl:attribute>
<xsl:text>en</xsl:text>
</xsl:element>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="*|@*|text()">
<xsl:copy>
<xsl:apply-templates select="*|@*|text()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
By the way, don't try using <pre> tags to preserve whitespace when trying to post code in Blogger. Blogger's templates don't accommodate <pre> very well and it does bad things to your blog layout.
0 Comments:
Post a Comment
<< Home