[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Simplify HAIRY xslt

Subject: Re: Simplify HAIRY xslt
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Fri, 14 May 2004 13:15:35 -0400
xslt display attribute
Hi Marcus,

When this kind of thing appears, the simplest solution is to isolate just the bit that changes from the rest of the code:

At 08:35 PM 5/13/2004, you wrote:
  <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>

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

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


    <res:choose>
      <res:when test="*[name() = '{$prefix}:rule']">
        <xsl:text>{*[name() = '</xsl:text>
        <xsl:value-of select="$prefix"/>
        <xsl:text>:rule']/@uid}</xsl:text>
      </res:when>
      <res:otherwise>
        <xsl:text>{generate-id()}_</xsl:text>
        <xsl:value-of select="$prefix"/>
      </res:otherwise>
    </res:choose>

        </xsl:attribute>
        <xsl:attribute name="xpath">{$newXpath}</xsl:attribute>
        <xsl:attribute name="type">{$type}</xsl:attribute>
        <xsl:copy-of select="@*|node()"/>
      </xsl:element>
  </xsl:template>

You can isolate this logic further:

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

    <res:choose>
      <res:when test="*[name() = '{$prefix}:rule']">
        <xsl:text>{*[name() = '</xsl:text>
        <xsl:value-of select="$prefix"/>
        <xsl:text>:rule']/@uid}</xsl:text>
      </res:when>
      <res:otherwise>
        <xsl:text>{generate-id()}_</xsl:text>
        <xsl:value-of select="$prefix"/>
      </res:otherwise>
    </res:choose>

</xsl:variable>

<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">
<xsl:value-of select="$uid-assign"/>
</xsl:attribute>
<xsl:attribute name="xpath">{$newXpath}</xsl:attribute>
<xsl:attribute name="type">{$type}</xsl:attribute>
<xsl:copy-of select="@*|node()"/>
</xsl:element>
</xsl:template>


which can be handy for parameterization, shortening the syntax, etc.
(You will want to straighten up the indents in that code.)

I hope that helps.
Wendell


Sorry about the long lines.

Thanks

/Marcus

___&&__&_&___&_&__&&&__&_&__&__&&____&&_&___&__&_&&_____&__&__&&_____&_&&_
"Thus I make my own use of the telegraph, without consulting
the directors, like the sparrows, which I perceive use it
extensively for a perch." -- Thoreau


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.