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 February 2nd, 2007, 04:42 AM
Registered User
 
Join Date: Apr 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Comparing two variables by using = operator in xsl

Hi,

I have the following xml.

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="destination.xsl"?>
<destination>
    <searchCities>
        <searchCitiy>
            <cityName>London</cityName>
            <cityHotel>AAA</cityHotel>
        </searchCitiy>
        <searchCitiy>
            <cityName>Paris</cityName>
            <cityHotel>BBB</cityHotel>
        </searchCitiy>
        <searchCitiy>
            <cityName>Rome</cityName>
            <cityHotel>BBB</cityHotel>
        </searchCitiy>
    </searchCities>
    <parameters>
        <query>London</query>
    </parameters>
</destination>

The criteria is when value of <query> will be matched with any of <cityName>, that <cityName> will be displayed. When it is not matched different template (<xsl:otherwise> section) would be called.

Now both the values (when it is matched and not matched) are coming at the same time. It should come either one of the values.


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/destination">
        <xsl:apply-templates select="searchCities"/>
    </xsl:template>
    <xsl:template match="searchCities">
        <xsl:variable name="var1">
            <xsl:value-of select="../parameters/query"/>
        </xsl:variable>
        <xsl:for-each select="searchCitiy">
            <xsl:variable name="var2">
                <xsl:value-of select="cityName"/>
            </xsl:variable>
            <xsl:choose>
                <xsl:when test="$var1 = $var2">
                    <xsl:value-of select="cityName"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:text> "Call Different Template" </xsl:text>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>


Is there any concept of premature exit from the for loop in xsl so that after gettng any one of the values (either from match/non-match) for the first time control will come out from the loop.


Thanks in advance,
Saurabh

 
Old February 2nd, 2007, 05:22 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You can't exit an xsl:for-each prematurely for the simple reason that it's not a loop. In an ideal world all the nodes selected in by the for-each are processed simultaneously, even in the real world there is not guarantee of the actual order. You are fighting XSLT instead of utilising it, try something like:
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/destination">
        <xsl:apply-templates select="searchCities/searchCity"/>
    </xsl:template>

   <xsl:template match="searchCity">
    <xsl:choose>
      <xsl:when test=" cityName = /destination/parameters/query">Do something</xsl:when>
      <xsl:otherwise>Something else</xsl:otherwise>      
    </xsl:choose>
   <xsl:template>

--

Joe (Microsoft MVP - XML)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparing with blank value using xsl:if vidhiS XSLT 3 March 11th, 2008 07:16 AM
copying the value of xsl in variable and comparing amhicraig XSLT 1 December 4th, 2007 07:58 PM
Invalid operator for data type. Operator equals di Pusstiu SQL Server 2000 2 August 10th, 2007 04:51 AM
Comparing floating point variables patpicos BOOK: Beginning Java 2, JDK 5 Edition 0 February 1st, 2005 12:37 AM
Stupid Problem... on comparing variables! Varg_88 Classic ASP Basics 1 December 11th, 2004 06:00 PM





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