This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: variable definition reuse


Dear Jenni/David / Michael
The XML file i had given as an example is a stripped down version.. The real 
example is lot more complicated where each variable would be of the kind
<xsl:variable name = "language" select "-expression that picks language"/>
<xsl:variable name="MyVar" 
select="varlist/listids[@id='1']/var/european[@language='$language']"/>
and i would use this MyVar in displayin in many places.
Reasons for my defining the variable:
1. Reduce Error in typing the long expression
2. Reduce need to type the condition again
3. Increase the legibility ( because this takes a considerable space in 
style sheet)
4.Reduce the length of style sheet.

please comment..
laura


>From: Jeni Tennison <jeni@jenitennison.com>
>Reply-To: xsl-list@lists.mulberrytech.com
>To: "Laura Jenkins" <xsl_list@hotmail.com>
>CC: xsl-list@lists.mulberrytech.com
>Subject: Re: [xsl] variable definition reuse
>Date: Fri, 22 Mar 2002 14:15:18 +0000
>
>Hi Laura,
>
> > 1. it is possible to do define element specific variables in a
> > different file??
>
>Not through XSLT means. By definition, element-specific variables are
>local to the place where you're processing the element. The only
>variables that you can share across stylesheets are global variables.
>If you were only interested in (for example) the exp-date element
>child of the first Product in your file, then it would be fine.
>
>You could, however, do this through entities. Create a file that holds
>the variable declarations:
>
>--- variables.xml ---
><?xml version="1.0"?>
><xsl:variable name = "color" select = "color">
><xsl:variable name = "weight" select = "weight">
><xsl:variable name = "cost" select = "cost">
><xsl:variable name = "acceptance" select = "acceptance">
><xsl:variable name = "mnf-date" select = "mnf-date">
><xsl:variable name = "exp-date" select = "exp-date">
><xsl:variable name = "lable-design" select = "lable-design">
><xsl:variable name = "mkt-agncy" select = "mkt-agncy">
><xsl:variable name = "serial-id" select = "serial-id">
><xsl:variable name = "order-qty" select = "order-qty">
><xsl:variable name = "re-order-level" select = "re-order-level">
>---
>
>Then declare a general entity that references it in your stylesheet:
>
>--- style.xsl ---
><?xml version="1.0"?>
><!DOCTYPE xsl:stylesheet [
><!ENTITY variables SYSTEM 'variables.xml'>
>]>
><xsl:stylesheet ...>
>...
></xsl:stylesheet>
>---
>
>and within the relevant template, use an entity reference to pull in
>the content of the file:
>
><xsl:template match="Product">
>   &variables;
>   ...
></xsl:template>
>
> > 2. it is the right approach to do it that way??
>
>As David said, it's not altogether apparent why you want to use a
>variable rather than just referring to the element each time. You
>want to do:
>
>   <xsl:value-of select="$exp-date" />
>
>rather than:
>
>   <xsl:value-of select="exp-date" />
>
>I can see three reasons in general for using a variable rather than
>repeating the same expression over and over again:
>
>  a. if the expression is long, it cuts down on the length of your
>     stylesheet, reduces the possibility of errors, and makes it more
>     maintainable
>
>  b. if the expression takes a long time to evaluate, it increases the
>     speed of the transformation
>
>  c. if you make a mistake in typing the name of a variable, you're
>     told about it whereas if you make a mistake when typing the name
>     of an element or attribute, XSLT recovers silently
>
>In your case, neither a nor b really apply, since the expression is
>neither long nor particularly processor-intensive, although I suppose
>that you might be thinking in terms of maintainability. As far as c
>goes, it's a valid point.
>
>I've been in discussion recently with someone who wanted to go as far
>as declaring all the names of elements and attributes in the source
>XML document as entities so that there was an error if he made a typo.
>I'd welcome other people's thoughts as to whether that's a good idea
>or not...
>
>Cheers,
>
>Jeni
>
>---
>Jeni Tennison
>http://www.jenitennison.com/
>
>
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
>


_________________________________________________________________
Send and receive Hotmail on your mobile device: http://mobile.msn.com


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]