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 December 28th, 2009, 04:57 PM
Authorized User
 
Join Date: Nov 2009
Posts: 11
Thanks: 6
Thanked 0 Times in 0 Posts
Default Open element gives problem with choose

Hello,

If someone could be kind enough to help, I am having the following problem.

Given the following pseudo code, the goal is to switch the parent element based on some condition. The $variable also needs to be processed correctly. [$variable is a string passed by a java program and its value cannot be known ahead of time.]

**begin pseudo code **

<xsl:choose>
<xsl:when test="someTest">
<Element1 some attributes that call a variable {$variable} >
</xsl:when> [error occurs here]
<xsl:otherwise>
<Element2 some attributes that call a variable {$variable}>
</xsl:otherwise> [error occurs here]
</xsl:choose>

[Body of the code goes here. All of these are child elements of Element1 and Element2.]

<xsl:choose>
<xsl:when test="sameTestasAbove">
</ Element1>
</xsl:when>
<xsl:otherwise>
</Element2>
</xsl:otherwise>
</xsl:choose>

**end pseudo code **

The above code gives an error as it is expecting the closing tag for Element1 and Element2 before the closing tag of </when> and </otherwise>.

Using either <xsl:text> or <xsl:value of> with the disable-output-escaping set to "yes" fixes that problem [by using &lt; and &gt; rather than the brackets], but introduces the problem of literally processing the $variable instead of pulling in the desired value.

It would be possible to put the "body of the code" in both the when and otherwise statements, but this would lead to future updates occurring in two places. If possible, such a situation should be avoided.

Does anyone have another suggestion?

Thanks for the help.
 
Old December 28th, 2009, 06:53 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Unfortunately you are approaching the problem in the wrong manner - i.e. in a manner which is not the correct approach for XSLT.

If you can put the XSLT instructions which are common to both the inners of Element1 an Element2 in a separate template then you can do something like this:

<xsl:choose>
<xsl:when test="a">
<Element1>
<xsl:call-template name="innerWorkings"/>
</Element1>
</xsl:when>
<xsl:otherwise>
<Element2>
<xsl:call-template name="innerWorkings"/>
</Element2>
</xsl:otherwise>
</xsl:choose>

But as you wrote your example in pseudo code and we have no idea what your input or output should look like that is the best I can do at this stage.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
Kenneth.Dougherty (December 29th, 2009)
 
Old December 28th, 2009, 08:07 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You are thinking of a <x> as an instruction to create a start tag, and </x> as a separate instruction to create an end tag. That's an entirely incorrect mental model of the semantics of the language, and you will have conceptual difficulties writing XSLT code until you get it out of your head.

Sam has shown you one way to do this; another way (depending on the details) is

Code:
<xsl:element name="{if (a) then 'Element1' else 'Element2'}">
   ... create the attributes and content ....
</xsl:element>
__________________
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:
Kenneth.Dougherty (December 29th, 2009)
 
Old December 29th, 2009, 09:27 AM
Authorized User
 
Join Date: Nov 2009
Posts: 11
Thanks: 6
Thanked 0 Times in 0 Posts
Default thanks for the help

I put the rest of the code into a separate template, and the problem was solved. Thanks again.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem adding element to the previous element dani1 XSLT 5 September 10th, 2008 01:38 AM
Problem -- Empty element check ! back2grave XSLT 2 July 10th, 2006 04:58 PM
Schema Complex element with restriction problem [email protected] XML 1 June 27th, 2006 11:45 AM
Problem Renaming an Element Morrislgn XSLT 2 May 31st, 2005 06:18 AM
"choose, when, otherwise" - problem natjimy XSLT 8 December 17th, 2004 02:22 PM





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