[xsl] Simplify HAIRY xslt

Subject: [xsl] Simplify HAIRY xslt
From: Marcus Andersson <marcus@xxxxxxxxxx>
Date: Fri, 14 May 2004 02:35:19 +0200
I have this stylesheet that generates another stylesheet based on some XML that contains some XPaths that should get evaluated on the next pass and also generate some other stuff like absolute XPaths. The code I have works but is extremely hairy and also duplicates some code.

I provide the template that does the duplication in hope that someone can help me. The structures inside the res:when and the res:otherwise are identical apart from the content of the uid attribute. I'll be glad to provide you with more info if you need.

  <xsl:template name="rule">
    <xsl:param name="uri"/>
    <xsl:variable name="prefix" select="../@prefix"/>

<res:choose>
<res:when test="*[name() = '{$prefix}:rule']">
<xsl:element name="{$prefix}:rule" namespace="{$uri}">
<xsl:if test="string(@display)">
<xsl:attribute name="displayValue">{<xsl:value-of select="@display"/>}</xsl:attribute>
</xsl:if>
<xsl:attribute name="uid">{*[name() = '<xsl:value-of select="$prefix"/>:rule']/@uid}</xsl:attribute>
<xsl:attribute name="xpath">{$newXpath}</xsl:attribute>
<xsl:attribute name="type">{$type}</xsl:attribute>
<xsl:copy-of select="@*|node()"/>
</xsl:element>
</res:when>
<res:otherwise>
<xsl:element name="{$prefix}:rule" namespace="{$uri}">
<xsl:if test="string(@display)">
<xsl:attribute name="displayValue">{<xsl:value-of select="@display"/>}</xsl:attribute>
</xsl:if>
<xsl:attribute name="uid">{generate-id()}_<xsl:value-of select="$prefix"/></xsl:attribute>
<xsl:attribute name="xpath">{$newXpath}</xsl:attribute>
<xsl:attribute name="type">{$type}</xsl:attribute>
<xsl:copy-of select="@*|node()"/>
</xsl:element>
</res:otherwise>
</res:choose>


</xsl:template>

Sorry about the long lines.

Thanks

/Marcus

Current Thread