ClassCastException when using an XSL variable
XSL can be so frustrating.
So today, I wanted to be able to do something like this:
<xsl:variable name="foo">
<xsl:choose>
<xsl:when test="bar">
<xsl:copy-of select="bar"/>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="bat"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
(Set the variable “foo” to the contents of the node “bar” if available, otherwise “bat”.)
And then:
<xsl:value-of select="$foo/childNode1"/> <xsl:value-of select="$foo/childNode2"/>
(Get child nodes of either “bar” or “bat”, depending on whether “bar” is present in the XML or not.)
Seems simple and logical enough, don’t you think?
However, I kept getting a ClassCastException when I tried this.
So, Google to the rescue. I found this post which explained the problem. Basically, the contents of the variable $foo aren’t a normal node set. Why not? Beats me. So what are they? Something called a result tree fragment. And you can’t select nodes from a result tree fragment. Darn!
However, if you set the variable using a select attribute instead of the body, you’ll get a proper node set. That doesn’t help in this case, though, since I need a choose to determine what will go in the variable.
In this case, I was able to work around the limitation by using more verbose code - creating more variables and choose expressions. But that type of code could get really out of hand if you needed to select lots of child nodes, instead of just the two I needed in this instance.
Most of the time XSL is a pretty ok language (once you get past the initial learning curve), but it’s these occasional weird cases that make you just want to shake the people that designed the language.
May 24th, 2005 at 3:22 pm
Jennifer, do you think it’s beneficial to learn XSLT? I am very intrigued with the capabilities - especially more “distinctness,” when separating content from presentation from structure.
I read this article a while ago, and became intrigued with the technology.
May 24th, 2005 at 4:22 pm
Interesting article. I’ve never actually done client-side XSL transformations, but I guess it’s pretty much exactly the same as doing it server-side.
It is good for separation of data and presentation, but on the other hand, they’re never totally separate. Whenever I need to add something new to our UI, I almost always have to go in and change the XML I’m outputting as well. Though for more superficial changes (like rearranging the layout) it’s true, I don’t have to touch the XML at all.
Anyway, it often seems like a lot of overhead for what it is. I mean, we’re handling our data in Java, but then in the Java code we create XML documents, and then we run the XSL over those documents. It seems a lot more direct just to use JSPs to directly access the data in the Java objects, without having to convert it to something else and process the data yet again to display it.
It’s a good skill to have, and it’s interesting to learn, since it’s so different than JSP/ASP/PHP or Java/C++/Perl. But I’m not sure it really saves you that much.
May 24th, 2005 at 6:34 pm
Thanks for the reply…
It’s all very interesting. I’d like to learn more JSP/Java - and all that stuff.