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, 2016, 03:54 AM
Registered User
 
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.
 
Old December 28th, 2016, 05:28 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
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
 
Old December 28th, 2016, 05:56 AM
Registered User
 
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.





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





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