Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 28th, 2016, 03:54 AM
Registered User
Points: 8, Level: 1
Points: 8, Level: 1 Points: 8, Level: 1 Points: 8, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2016
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default XSLT - IF TEST is not working

MY XML:
-------
<VEHICLE>
<CAR> POLO </CAR>
<CAR> VENTO </CAR>
<VEHICLE>

MY XSLT:
--------
<xsl:choose>
<xsl:when test="/VEHCILE/CAR = 'POLO'">
<VEHICLE>
<xsl:value-of select="/VEHCILE/CAR"/>
<VEHICLE>
</xsl:when>
</xsl:choose>

OUTPUT I GOT:
-------------
<VEHICLE>
<CAR> POLO VENTO </CAR>
<VEHICLE>

EXPECTED OUTPUT:
----------------
<VEHICLE>
<CAR> POLO </CAR>
<VEHICLE>

Please help me to get EXPECTED OUTPUT using above XSLT.
Reply With Quote
  #2 (permalink)  
Old December 28th, 2016, 05:28 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

First you need to understand why you got the output you got.

The test /VEHICLE/CAR = 'POLO' tests compares the node-set /VEHICLE/CAR (which contains two element nodes) to the string 'POLO', and this comparison is true if any of the elements has a string value of 'POLO' (which it does). So the condition is true, and therefore the body of the xsl:when is executed. The body of the xsl:when creates a VEHICLE element and populates it with the result of the xsl:value-of instruction.

The xsl:value-of instruction selects two element nodes. What happens now varies between XSLT 1.0 and 2.0.

In 1.0, it will output the value of the first selected element node, which happens to be 'POLO'. (But it's only by chance that this is the same one as matched the xsl:when test).

In 2.0 it will output the values of all the selected element nodes, space-separated - this is what you are seeing.

Correcting your code requires a better understanding of your requirements. The quick fix to get your expected output would be to write

Code:
<xsl:when test="/VEHICLE/CAR = 'POLO'">
<VEHICLE>POLO<VEHICLE>	
</xsl:when>
But I suspect that you've simplified the real requirement. A much more typical approach would be to do something like

Code:
<xsl:apply-templates select="/VEHICLE/CAR"/>
and then

Code:
<xsl:template match="CAR[. = 'POLO']">
<VEHICLE><xsl:value-of select="."/></VEHICLE>
</xsl:template>
But at this stage we are guessing what you really want to achieve.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
Reply With Quote
  #3 (permalink)  
Old December 28th, 2016, 05:56 AM
Registered User
Points: 8, Level: 1
Points: 8, Level: 1 Points: 8, Level: 1 Points: 8, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2016
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you mhkay,

I will try your code and update you.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Test Condition not working in xslt. darkknight XSLT 7 August 14th, 2012 06:31 AM
XSLT - Function to test value haixia XSLT 5 August 14th, 2006 09:31 AM
C# If test not working RichardOrmiston C# 7 December 20th, 2005 08:18 AM
C# If test not working RichardOrmiston ASP.NET 1.0 and 1.1 Professional 0 December 18th, 2005 02:23 PM
IIS test not working kevi3032 ASP.NET 1.0 and 1.1 Basics 21 December 20th, 2004 02:43 PM



All times are GMT -4. The time now is 04:16 PM.


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