Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 17th, 2011, 09:56 AM
Authorized User
 
Join Date: Nov 2007
Posts: 31
Thanks: 6
Thanked 0 Times in 0 Posts
Default Conditional variable set to RTF

Hello all,

I am having an issue trying to set a conditional variable (which I know is tricky anyway in XSLT) to a path.

I am declaring the variable name first, and then within the xsl:variable tags I have the xsl:choose that checks a condition and outputs one of two possible paths - the aim being that I can then use the variable as a 'root' for all other paths in the stylesheet.

Below is the code for the variable:

Code:
<xsl:variable name="Root">
  <xsl:choose>
    <xsl:when test="somepath/sometag = 'abcd'">
      rootone/roottwo[@desc='firsttype']
    </xsl:when>
    <xsl:otherwise>
      rootone/roottwo[@desc='othertype']
    </xsl:otherwise>
  </xsl:choose>
</xsl:variable>
I have tried doing this with <xsl:text> tags around the paths, <xsl:value-of> and <xsl:copy-of> tags, but no joy.

With these I get the error:
org.apache.xpath.objects.XRTreeFrag cannot be cast to org.apache.xpath.objects.XNodeSet
if I try to use the variable within an XPath expression, for example:
$Root/name/surname/.

If the variable isn't conditional, e.g.:
Code:
<xsl:variable name="Root" select="rootone/roottwo[@desc='firsttype']" />
then it works fine.

I understand that a conditional variable will set the variable value to a RTF (result tree fragment) rather than a node-set, which is what I need to be able to use it within another path.
I had thought converting it to text would work as it would add it to the additional path and read it as a path.

Please could someone advise how else I can use this conditional variable?
I am using xslt 1.0, but couldn't see any node-set functions even in xslt 2.0.

Thanks in advance,
__________________
Neil Belch
Technical Officer
CDL

The views opinions and judgements expressed in this message are solely those of the author. The message contents have not been reviewed or approved by CDL.
 
Old October 17th, 2011, 10:15 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

XPath expressions in XSLT always go in attributes, never in text nodes. And an RTF will never select excising nodes, it will always create new nodes.

You're presumably on XSLT 1.0 (this would be very easy in XSLT 2.0, and since you're writing in Java there's really no excuse not to move forward.)

You need something like this:

Code:
<xsl:variable name="condition" select="somepath/sometag = 'abcd'"/>
<xsl:variable name="Root" select="
    rootone/roottwo[@desc='firsttype'][$condition] |
    rootone/roottwo[@desc='othertype'][not($condition)]"/>
In 2.0 it would be simply:

Code:
<xsl:variable name="Root" select="
    rootone/roottwo[@desc=if ($condition) then 'firsttype' else 'other type']
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
The Following User Says Thank You to mhkay For This Useful Post:
Belch (October 17th, 2011)
 
Old October 17th, 2011, 10:58 AM
Authorized User
 
Join Date: Nov 2007
Posts: 31
Thanks: 6
Thanked 0 Times in 0 Posts
Default

Hi Michael,

That solution works perfectly, and makes sense now I can see it written out!

Thanks very much for the quick reply.
__________________
Neil Belch
Technical Officer
CDL

The views opinions and judgements expressed in this message are solely those of the author. The message contents have not been reviewed or approved by CDL.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Object variable not set or With Block not set brucechess BOOK: Beginning VB.NET Databases 1 March 3rd, 2009 07:30 AM
Object Variable or With Block Variable Not Set Iashia06 Access 1 May 22nd, 2006 10:24 AM
Created RTF when try to open RTF, got an error not24 C# 0 March 31st, 2006 01:31 PM
Object variable or With block variable not set tparrish VS.NET 2002/2003 3 May 22nd, 2005 07:40 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.