Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help with "Variable"

 
Vivek Saxena
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was reading �Roger�s� tutorial on XSLT and I got confused in �variable� section.

A variable is �write once, read many�.
That is, you can assign a variable a value only once, but then you can retrieve the value of the variable many times.

I tried to assign �second� value to a variable and it seems that it is taking the second assigned value for the declared variable. I am using following files for my testing:
XML File:

XSL File:


OUTPUT File:


I really don�t understand. Why am I not getting an error? Or why is it using second assigned value to the variable?

Please do reply.
Thanks
 
Tong Chen
Ranch Hand
Posts: 1011
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am interested in this too.
In the chapter 11 of XSLT spec, nothing is mentioned about this.

It looks like the implementations for XSLT vary for different vendors.
I tried it in the MS IE 6, it gives me error:"Variable or parameter 'juicerName' cannot be defined twice within the same template. "
Then I tried in the Netscape 7.0, it takes the first value.
The I tried to use Xalan, it takes the second value.
I am expecting somebody can shed a light on it.
 
Tong Chen
Ranch Hand
Posts: 1011
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I reviewed the "11.5 Variables and Parameters within Templates" in XSLT spec, seems like the MS IE 6's implementation follows the spec:
The following is from the spec:
A binding shadows another binding if the binding occurs at a point where the other binding is visible, and the bindings have the same name. It is an error if a binding established by an xsl:variable or xsl aram element within a template shadows another binding established by an xsl:variable or xsl aram element also within the template. It is not an error if a binding established by an xsl:variable or xsl aram element in a template shadows another binding established by an xsl:variable or xsl aram top-level element. Thus, the following is an error:
<xsl:template name="foo">
<xsl aram name="x" select="1"/>
<xsl:variable name="x" select="2"/>
</xsl:template>
However, the following is allowed:
<xsl aram name="x" select="1"/>
<xsl:template name="foo">
<xsl:variable name="x" select="2"/>
</xsl:template>
NOTE: The nearest equivalent in Java to an xsl:variable element in a template is a final local variable declaration with an initializer. For example,
<xsl:variable name="x" select="'value'"/>
has similar semantics to
final Object x = "value";
 
Vivek Saxena
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Tong Chen,
I can see this implementation problem. I tried in IE 6 and it gave me same error.
Thanks
 
xp li
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Tong Chen,
I'm a beginner on XML. Could you please tell me how to get the output using MS IE 6.0, given the xml and xsl source files? Since nothing in both files refers the other one. Is additional file needed to connect them?
Thanks a lot in advance.
 
Tong Chen
Ranch Hand
Posts: 1011
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it's super easy:
1. create a folder on your c: drive (for example, c:\myxml)
2. under "myxml" folder, create an XML file "hello.xml":
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="hello.xsl"?>
<greeting>
Hello, World!
</greeting>
3. under "myxml" folder, create an XSL file "hello.xsl":
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl utput method="html"/>
<xsl:template match="/">
<xsl:apply-templates select="greeting"/>
</xsl:template>
<xsl:template match="greeting">
<html>
<body>
<h1>
<xsl:value-of select="."/>
</h1>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
4. open your IE, in the address bar, type in:
C:\myxml\hello.xml

And then you will get it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic