Subject: Re: [xsl] variable definition reuse From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx> 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
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
RE: [xsl] variable definition reuse, Michael Kay | Thread | Entity references to guard against , Wendell Piez |
[xsl] FOP - what, where, when, how?, Alastair Growcott | Date | Re: [xsl] FOP - what, where, when, , David B. Bitton |
Month |