xsl-list
[Top] [All Lists]

Re: more elegant way to process element with default value?

2003-06-13 13:05:00
(1)
  <xsl:variable name="someName" select="concat(substring('true', 1, 4 - 4 *
(not($tmp))), $tmp)"/>

Or (I prefer this):


(2)
string($default[not($tmp)] | $tmp)

$default is a node-set defined in the stylesheet such that its string value
is "true"  , e.g.:

<myDefault:myDefault>true</myDefault:myDefault>

<xsl:variable name="default" select="document('')/*/myDefault:*[1]"/>


(3)
An elegant way is what David Carlisle demonstrated with creating multiple
attributes with the same name.


=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL


"Steve Rosenberry" 
<Steve(_dot_)Rosenberry(_at_)ElectronicSolutionsCo(_dot_)com> wrote in
message 
news:3EEA242C(_dot_)1FC6E272(_at_)ElectronicSolutionsCo(_dot_)com(_dot_)(_dot_)(_dot_)


Lars Huttar wrote:

  <xsl:variable name="number-rows">
    <xsl:variable name="tmp"
select="/*/llcd:global-settings/llcd:setting[(_at_)name =
 'number-rows']"/>
    <xsl:choose>
      <xsl:when test="$tmp"><xsl:value-of select="$tmp" /></xsl:when>
      <xsl:otherwise><xsl:value-of select="'true'" /></xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

But that seems pretty verbose.  And it will be worse as I have multiple
settings variables with various names.
Is there a more elegant way?


Wow, Lars, if you consider that verbose I must be doing something
terribly wrong...

Assuming, all your global settings follow the same basic XPath
structure, I'd do it as follows:

   <xsl:variable name="number-rows">
     <xsl:call-template name="get_global_setting">
       <xsl:with-param name="setting" select="'number-rows'"/>
     </xsl:call-template>
   </xsl:variable>

and then provide the appropriate get_global_setting named template.  If
your XPath structure doesn't allow for the simple syntax above, you can
always change the "setting" parameter to a "path" parameter.

In an actual XSLT file, I would write the entire variable declaration
above on a single line to get most of the verbosity out of my face.  As
an added benefit, if the retrieval method for your global settings
changes, you only have a single place to change your XSLT.

-- 
Steve Rosenberry
Sr. Partner

Electronic Solutions Company -- For the Home of Integration
http://ElectronicSolutionsCo.com

(610) 670-1710

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list






 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list