xsl-list
[Top] [All Lists]

Re: [xsl] problem with xsl:if test statement

2007-12-18 01:02:57
She just wants to spit out a link if one or many nodes exists.

Pretty easy.

<xsl:if test="This/node[element=match]/exists">
    <link />
</xsl:if>

On Dec 18, 2007 1:14 AM, John Horner 
<Horner(_dot_)John(_at_)abc(_dot_)net(_dot_)au> wrote:
I'm rather confused by this question/example code.

Given the code you've sent us, it *doesn't* produce two links. The
for-each works once for each Fruits node, and if it matches the country
and the fruit, produces one link, even if there are two
<Type>Apples</Type> nodes.

Perhaps the answer to the more general question is to use count()? As
in,

        <xsl:for-each select="Products">
                <xsl:if test="count(Fruits[Type='Apples']) &gt; 0 ">
                there is at least one 'Apples' node in the
                Product node for <xsl:value-of select="Fruits/Country"/>
                </xsl:if>
        </xsl:for-each>

By the way, there are quite a few mistakes in your code and it doesn't
run without fixing them. I suspect you didn't send us the code you're
actually working on?

-----Original Message-----
From: Amy Huang [mailto:h(_dot_)amy88(_at_)yahoo(_dot_)com]
Sent: Tuesday, 18 December 2007 3:51 PM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] problem with xsl:if test statement

Hello,

I'm new to xml and xsl. I need to generate one link if my query finds
one or more records in my XML document. But I if I use "for each
statement", the link will repeat again and again depending on how many
records satisfy my query. E.g., if 2 records satisfy my query, the link
will repeat twice like this:

For apples, click here
For apples, click here

How can I generate only one link if my query finds more than one
records?

XSL:
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";;>

<xsl:param name="Country" select="Mexico" ></xsl:param>
<xsl:template match="/">
<html>
<body>
<h2>Types of Fruits</h2>
<xsl:for-each select="Products/Fruits">
<xsl:if test="Country = $Country and Type = 'Apples'">
<a>
<xsl:attribute name="href">
fruits.aspx?moi=<xsl:value-of select="$Country"/>
</xsl:attribute>
For apples, click here
</a>
<br />
</xsl:if>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

XML:
<Products>
<Fruits>
<Country>Mexico</Country>
<Tpye>Apples</Type>
<Tpye>Bananas</Type>
</Fruits>
<Fruits>
<Country>Brazil</Country>
<Tpye>Oranges</Type>
<Tpye>Apples</Type>
</Fruits>
<Fruits>
<Country>Peru</Country>
<Tpye>Bananas</Type>
<Tpye>Oranges</Type>
</Fruits>
</Products>

Any help would be greatly appreciated!

Amy Huang



________________________________________________________________________
____________
Never miss a thing.  Make Yahoo your home page.
http://www.yahoo.com/r/hs


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--




==============================================================================
The information contained in this email and any attachment is confidential and
may contain legally privileged or copyright material.   It is intended only 
for
the use of the addressee(s).  If you are not the intended recipient of this
email, you are not permitted to disseminate, distribute or copy this email or
any attachments.  If you have received this message in error, please notify 
the
sender immediately and delete this email from your system.  The ABC does not
represent or warrant that this transmission is secure or virus free.   Before
opening any attachment you should check for viruses.  The ABC's liability is
limited to resupplying any email and attachments
==============================================================================

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>