Subject: RE: [xsl] xsl:variable question From: "Long Zhao" <LZhao@xxxxxxxxxxxxxxxx> Date: Wed, 19 Dec 2001 18:21:31 -0600 |
i tested your xsl with my xml(shown bellow) your xsl with one change: <xsl:template match="//Item"> without the change there is no output, so i agree on that root element can not access the variable <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <xsl:variable name="mode1" select="'1'" /> <xsl:variable name="mode2">1</xsl:variable> <xsl:apply-templates /> </xsl:template> <xsl:template match="//Item"> <html> <body> <xsl:if test="$mode1='1'">Mode 1 = '1'</xsl:if> <xsl:if test="$mode2='1'">Mode 2 = '1'</xsl:if> <xsl:if test="$mode2=$mode1">Mode 2 = Mode 1</xsl:if> </body> </html> </xsl:template> </xsl:stylesheet> my xml <?xml version="1.0" encoding="UTF-8"?> <study> <Item/> </study> here is the result Mode 1 ='1'Mode 2 ='1'Mode 2 =Mode 1 but in my case it is a different story, i did not use apply-template, instead, i use call-template. i also tested with my xsl <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <xsl:variable name="mode1" select="'1'"/> <xsl:variable name="mode2" select="2"/> <xsl:variable name="mode3">3</xsl:variable> <xsl:call-template name="dosomething"/> </xsl:template> <xsl:template name="dosomething"> <xsl:if test="$mode1='1'"> <xsl:text>mode='1'</xsl:text> </xsl:if> <xsl:if test="$mode2='1'"> <xsl:text>mode=2</xsl:text> </xsl:if> <xsl:if test="$mode3='3'"> <xsl:text>mode=3</xsl:text> </xsl:if> </xsl:template> </xsl:stylesheet> here is the output mode='1'mode=3 so the template "dosomething" can access the variable set in template which match "/" cheers Long -----Original Message----- From: Chris Bayes [mailto:chris@xxxxxxxxxxx] Sent: Wednesday, December 19, 2001 6:36 PM To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx Subject: RE: [xsl] xsl:variable question NO!! You are both wrong. This stylesheet will not work <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <xsl:variable name="mode1" select="'1'" /> <xsl:variable name="mode2">1</xsl:variable> <xsl:apply-templates /> </xsl:template> <xsl:template match="root"> <html> <body> <xsl:if test="$mode1='1'">Mode 1 = '1'</xsl:if> <xsl:if test="$mode2='1'">Mode 2 = '1'</xsl:if> <xsl:if test="$mode2=$mode1">Mode 2 = Mode 1</xsl:if> </body> </html> </xsl:template> </xsl:stylesheet> It gives the error Description: A reference to variable or parameter 'mode1' cannot be resolved. The variable or parameter may not be defined, or it may not be in scope. Or Failed to compile style sheet At xsl:if on line 29 of _file:/C:/DOCUME~1/chris/LOCALS~1/Temp/[Input XSL]: Variable mode1 has not been declared Depending on the processor Ciao Chris XML/XSL Portal http://www.bayes.co.uk/xml > -----Original Message----- > From: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx > [mailto:owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx] On Behalf Of Long Zhao > Sent: 19 December 2001 22:54 > To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx > Subject: RE: [xsl] xsl:variable question > > > You are RIGHT!!! > but for the scope issue, i am right. > > Long > > -----Original Message----- > From: Thomas B. Passin [mailto:tpassin@xxxxxxxxxxxx] > Sent: Wednesday, December 19, 2001 5:23 PM > To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx > Subject: Re: [xsl] xsl:variable question > > > Aside from the question about the scope of the variable, > which Wendell and Joshua raised, there is a fundamental > issue. In the first example, you assign a value "1" to the > variable. In the second, you try to assign a nodeset > consisting of elements named "1", which of course cannot > exist since it would be illegal for an element name. To make > the second formulation equivalent, you should write > > <xsl:variable name="mode" select='"1"'/> > > (that's '.."1"..' where I am inserting two dots to space out > the inner and outer quotes for visibility). This assigns the > string value "1" to the variable, just as in the first example. > > Cheers, > > Tom P > > > [Long Zhao] > > > here are two xsl files : > > <xsl:template match="/"> > <xsl:variable name="mode">1</xsl:variable> > ...... > </xsl:template> > > <xsl:template name="dosomething"> > <xsl:if test="$mode='1'"> > ..... > </xsl:if> > ..... > </template> > ------------------------------------ > <xsl:template match="/"> > <xsl:variable name="mode" select='1'/> > ...... > </xsl:template> > > <xsl:template name="dosomething"> > <xsl:if test="$mode='1'"> > ..... > </xsl:if> > ..... > </template> > > why the first one does not work, but the second one works. > > am i doing anything wrong in the first xsl? > > > > > XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list > > > XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list > > XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
RE: [xsl] xsl:variable question, Chris Bayes | Thread | Re: [xsl] xsl:variable question, David Carlisle |
RE: [xsl] How to express a select a, Chris Bayes | Date | RE: [xsl] Use of XSLT to Transform , Uslu, Cihan Y (MED) |
Month |