473,738 Members | 10,068 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

xsl:if condition

Hi, i have a terrible noobie frustration formatting an XML file like
this:

<Dipendente Id="1" Anno="2003" Nome="pippo" Cognome="pippi"
Nato_il="10/03" Email="pi***@em ailprovider.it" Esito="ok"/>

On the XSLt file relayed to this XML, i wrote in green the attribute
value if Attribute "Esito" is "Ok".

I mean, something, like this:

<!--xsl:if test="@Esito != 'Ok' ">
<span style="cursor:h and; font-family:Verdana; font-size:9">
<xsl:value-of select="."/>
</span>
</xsl:if!-->

It seems that the test expression is always evaluated false even if i
set the attribute value to ok, OK, or even anything else. Is there
something wrong with this?

Please Help, I can't get it out!
Jul 20 '05 #1
9 8318
"Andrea Maschio" <am******@liber o.it> wrote in message
news:cc******** *************** ***@posting.goo gle.com
<!--xsl:if test="@Esito != 'Ok' ">
<span style="cursor:h and; font-family:Verdana; font-size:9">
<xsl:value-of select="."/>
</span>
</xsl:if!-->


Despite the fact that this is a comment and therefore cannot do
_anything_... ;-)

XML is case-sensitive. 'Ok' is different from 'OK' or 'oK'. I think this
causes your trouble.

Four ways to solve it:
1. Make sure it is alwalys 'OK' and nothing else, and
then test for "@Esito != 'OK'"
2. Test for all combinations of capital and non-capital
letters. This is idiotic, but in case your word is only
two chars long, it is still a possibility.
(OK, then it is four possibilities. *g*)
3. Work with a number, if you can.
test="number(@E sito) != 0"
4. Utilize the XPath translate() function to translate
everything into capitals or non-capitals.

Martin
Jul 20 '05 #2
> Despite the fact that this is a comment and therefore cannot do
_anything_... ;-)
Yes, sorry, it was a Typo : )

Ok, that's exactly:

<xsl:if test="number(@E sito) = 0 ">

I tried this solution, setting the value of @Esito to 0, to 1 or anything
else.

Xslt debugger show me the right value, but i can't get to get the test
expression evaluated as True.

Four ways to solve it:
1. Make sure it is alwalys 'OK' and nothing else, and
then test for "@Esito != 'OK'"
I tried that : (
2. Test for all combinations of capital and non-capital
letters. This is idiotic, but in case your word is only
two chars long, it is still a possibility.
Tried also that
(OK, then it is four possibilities. *g*)
3. Work with a number, if you can.
test="number(@E sito) != 0"
I tried 4. Utilize the XPath translate() function to translate
everything into capitals or non-capitals.


I tried also this:

<xsl:when test="string(@E sito) != 'Ok' "> // this is always evaluated true,
despite the value of @Esito

Gosh, i'm sorry, but i'm a newbie, i really don't understand.

THanks for your help

Andrea Maschio
Jul 20 '05 #3
"Andrea Maschio" <am************ *@libero.it> wrote in message
news:2x******** ************@tw ister1.libero.i t
Gosh, i'm sorry, but i'm a newbie, i really don't understand.


I dont, either. I don't know what *exactly* you do, but it works for me:

---test.xml------------------------------------------
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="test.xsl" ?>
<root>
<Dipendente Id="1" Esito="ok"/>
<Dipendente Id="2" Esito="not ok"/>
<Dipendente Id="3" Esito="ok"/>
</root>
-----------------------------------------------------

combined with...

---test.xsl------------------------------------------
<xsl:styleshe et version="1.0"
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
<xsl:template match="/root">
<body>
<xsl:apply-templates select="Dipende nte"/>
</body>
</xsl:template>
<xsl:template match="Dipenden te">
<div>
<xsl:value-of select="@Id"/>
<xsl:text> is </xsl:text>
<xsl:choose>
<xsl:when test="@Esito = 'ok'">
<xsl:value-of select="@Esito"/>
</xsl:when>
<xsl:otherwis e>
<b><xsl:value-of select="@Esito"/></b>
</xsl:otherwise>
</xsl:choose>
</div>
</xsl:template>
</xsl:stylesheet>
-----------------------------------------------------

produces...

---Result--------------------------------------------
<?xml version="1.0" encoding="UTF-16"?>
<body>
<div>1 is ok</div>
<div>2 is
<b>not ok</b>
</div>
<div>3 is ok</div>
</body>
-----------------------------------------------------

This is more or less what you want. If you take the same test (xsl:if or
xsl:when, that makes no difference) and do not get the same result, you
must be in error elsewhere.

Martin
Jul 20 '05 #4
The problem is exactly that i didn't know how to use templates. I
erroneously though it was enough to apply the condition to every
<xsl:for-each name="Dipendent e">.

Now i'm safe from going crazy, phew!

Can you indicate me where i could find a nice tutorial on this
argoment? THe problem was that making xslt with easy editors, i have
not a full control on its processes.

THank you very much for your precious helping!

Andrea

"Martin Boehm" <ng********@arc or.de> wrote in message news:<3f******* *************** *@newsread2.arc or-online.net>...
"Andrea Maschio" <am************ *@libero.it> wrote in message
news:2x******** ************@tw ister1.libero.i t
Gosh, i'm sorry, but i'm a newbie, i really don't understand.


I dont, either. I don't know what *exactly* you do, but it works for me:

---test.xml------------------------------------------
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="test.xsl" ?>
<root>
<Dipendente Id="1" Esito="ok"/>
<Dipendente Id="2" Esito="not ok"/>
<Dipendente Id="3" Esito="ok"/>
</root>
-----------------------------------------------------

combined with...

---test.xsl------------------------------------------
<xsl:styleshe et version="1.0"
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
<xsl:template match="/root">
<body>
<xsl:apply-templates select="Dipende nte"/>
</body>
</xsl:template>
<xsl:template match="Dipenden te">
<div>
<xsl:value-of select="@Id"/>
<xsl:text> is </xsl:text>
<xsl:choose>
<xsl:when test="@Esito = 'ok'">
<xsl:value-of select="@Esito"/>
</xsl:when>
<xsl:otherwis e>
<b><xsl:value-of select="@Esito"/></b>
</xsl:otherwise>
</xsl:choose>
</div>
</xsl:template>
</xsl:stylesheet>
-----------------------------------------------------

produces...

---Result--------------------------------------------
<?xml version="1.0" encoding="UTF-16"?>
<body>
<div>1 is ok</div>
<div>2 is
<b>not ok</b>
</div>
<div>3 is ok</div>
</body>
-----------------------------------------------------

This is more or less what you want. If you take the same test (xsl:if or
xsl:when, that makes no difference) and do not get the same result, you
must be in error elsewhere.

Martin

Jul 20 '05 #5
"Andrea Maschio" <am******@liber o.it> wrote in message
news:cc******** *************** ***@posting.goo gle.com

Hi Andrea,
The problem is exactly that i didn't know how to use templates.
I thought so.
I erroneously though it was enough to apply the condition to
every <xsl:for-each name="Dipendent e">.
It is <xsl:for-each select... not name ;-)

But I am afraid I still can't picture your problem. Can you post a
example snippet of your XML and XSL?
Can you indicate me where i could find a nice tutorial on this
argoment?
If you only could be a bit more specific. Which argument?
You might want to read into XPath at first. I have no URL at hand, but
Google will.
THe problem was that making xslt with easy editors, i have
not a full control on its processes.


Dump your "easy" editor and use a more appropriate tool (Xselerator from
www.topxml.com has proven to be _very_ useful, if you work in a Windows
environment), or a plain text editor with XML syntax highlighting. At
least if you want to really learn it.

Martin
Jul 20 '05 #6
> > The problem is exactly that i didn't know how to use templates.

I thought so.
I erroneously though it was enough to apply the condition to
every <xsl:for-each name="Dipendent e">.
It is <xsl:for-each select... not name ;-)


It was a Type error, but you are perfectly right: how can you
understand if i don't spell correctly words? : P
But I am afraid I still can't picture your problem. Can you post a
example snippet of your XML and XSL?
My Xml finally worked, elimininating this tag <xsl:for-each
select="NodeNam e">
and creating a template for this node. After i recall the template
from inside <body></body>. That was right?

Ok, so i thought "Now I Understood!". But I was still not right. I've
got another XML file, it's attached at the end of this post with his
xslt. I was trying to colour a cell only if the relative node is not
empty. In this case still doesn't work.
Dump your "easy" editor and use a more appropriate tool (Xselerator from
www.topxml.com has proven to be _very_ useful, if you work in a Windows
environment), or a plain text editor with XML syntax highlighting. At
least if you want to really learn it.


I surely want to learn this meta language, but like you know,
sometimes it happens you have to work on a tecnology, and then you
have the calm and the time to learn it. I used XMLSPy, with a
stylesheet designer. It seems to me to be very easy to use, but i
don'to know how to set choice upon two conditions, for example.

Here's the file, thanks a lot Martin for your help ^^

------------XML File----------------------------------------------
<!DOCTYPE Import SYSTEM "conti.dtd" >
<?xml-stylesheet type="text/xsl" href="stilecont i.xslt"?>
<Import>
<Row>
<Data_operazion e> 07/10/2003 </Data_operazione >
<Data_valuta> 12/09/2003 </Data_valuta>
<Importo_debito ></Importo_debito>
<Importo_credit o>9,8</Importo_credito >
<Causale>Causal eText</Causale>
</Row>
<Row>
<Data_operazion e> 08/10/2003 </Data_operazione >
<Data_valuta> 09/09/2003 </Data_valuta>
<Importo_debito >30</Importo_debito>
<Importo_credit o> </Importo_credito >
<Causale>Causal eText</Causale>
</Row>
</Import>
--------end of XML File---------------------------------------------
------------xslt file----------
<?xml version="1.0" encoding="UTF-8"?>
<xsl:styleshe et version="1.0"
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head/>
<body>
<xsl:for-each select="Import" >
<xsl:apply-templates select="Row"/>
</xsl:for-each>
</body>
</html>
</xsl:template>

<xsl:template match="Row">
<xsl:if test="position( )=1">
<table border="1">
<thead>
<tr>
<td style="backgrou nd-color:#004080">
<span style="color:wh ite; font-family:Verdana; font-size:9;
font-weight:bold; text-transform:capit alize">Data operazione</span>
</td>
<td style="backgrou nd-color:#004080">
<span style="color:wh ite; font-family:Verdana; font-size:9;
font-weight:bold; text-transform:capit alize">Data valuta</span>
</td>
<td style="backgrou nd-color:#004080">
<span style="color:wh ite; font-family:Verdana; font-size:9;
font-weight:bold; text-transform:capit alize">Importo debito</span>
</td>
<td style="backgrou nd-color:#004080">
<span style="color:wh ite; font-family:Verdana; font-size:9;
font-weight:bold; text-transform:capit alize">Importo credito</span>
</td>
<td style="backgrou nd-color:#004080">
<span style="color:wh ite; font-family:Verdana; font-size:9;
font-weight:bold; text-transform:capit alize">Causale</span>
</td>
</tr>
</thead>
<tbody>
<xsl:for-each select="../Row">
<tr>
<td style="backgrou nd-color:#C0C0C0">
<xsl:for-each select="Data_op erazione">
<span style="font-family:Verdana; font-size:9">
<xsl:apply-templates/>
</span>
</xsl:for-each>
</td>
<td style="backgrou nd-color:#D3D3D3">
<xsl:for-each select="Data_va luta">
<span style="font-family:Verdana; font-size:9">
<xsl:apply-templates/>
</span>
</xsl:for-each>
</td>
<td style="backgrou nd-color:#D90000">
<xsl:for-each select="Importo _debito">
<!--------------------Here it is the "choose"--------------------->
<xsl:choose >
<xsl:when test="Importo_d ebito != '' ">mycode for coloured
cell</xsl:when>
</xsl:choose>
<!---------------------------------------------------------------->
<span style="color:wh ite; font-family:Verdana;
font-size:9">
<xsl:apply-templates/>
</span>
<xsl:choose><xs l:when test="@Importo_ debito != ''
"> </xsl:when></xsl:choose>
</xsl:for-each>
</td>
<td style="backgrou nd-color:#008000">
<xsl:for-each select="Importo _credito">
<span style="color:#F FFFFF; font-family:Verdana;
font-size:9">
<xsl:apply-templates/>
</span>
</xsl:for-each>
</td>
<td>
<xsl:for-each select="Causale ">
<span style="font-family:Verdana; font-size:9;
text-transform:lower case">
<xsl:apply-templates/>
</span>
</xsl:for-each>
</td>
</tr>
</xsl:for-each>
</tbody>
</table>
</xsl:if>
</xsl:template>

</xsl:stylesheet>
Jul 20 '05 #7
In article <cc************ **************@ posting.google. com>,
Andrea Maschio <am******@liber o.it> wrote:

[...]

% Ok, so i thought "Now I Understood!". But I was still not right. I've
% got another XML file, it's attached at the end of this post with his
% xslt. I was trying to colour a cell only if the relative node is not
% empty. In this case still doesn't work.

[...]

% <!DOCTYPE Import SYSTEM "conti.dtd" >
% <?xml-stylesheet type="text/xsl" href="stilecont i.xslt"?>
% <Import>
% <Row>
% <Data_operazion e> 07/10/2003 </Data_operazione >
% <Data_valuta> 12/09/2003 </Data_valuta>
% <Importo_debito ></Importo_debito>
% <Importo_credit o>9,8</Importo_credito >
% <Causale>Causal eText</Causale>
% </Row>
% <Row>
% <Data_operazion e> 08/10/2003 </Data_operazione >
% <Data_valuta> 09/09/2003 </Data_valuta>
% <Importo_debito >30</Importo_debito>
% <Importo_credit o> </Importo_credito >
% <Causale>Causal eText</Causale>
% </Row>
% </Import>

% <?xml version="1.0" encoding="UTF-8"?>
% <xsl:styleshe et version="1.0"
% xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
% <xsl:template match="/">
% <html>
% <head/>
% <body>
% <xsl:for-each select="Import" >

It's nothing to do with your problem, but since there can be only one
Import, you don't need this xsl:for-each.

% <xsl:apply-templates select="Row"/>

[...]

% <xsl:for-each select="Importo _debito">
% <!--------------------Here it is the "choose"--------------------->
% <xsl:choose >
% <xsl:when test="Importo_d ebito != '' ">mycode for coloured
% cell</xsl:when>
% </xsl:choose>
% <!---------------------------------------------------------------->

The problem here is that you're testing inside an xsl:for-each loop.
The context is set to each node returned by the select expression
in turn. What you want is

test = ". != ''"
--

Patrick TJ McPhee
East York Canada
pt**@interlog.c om
Jul 20 '05 #8
> The problem here is that you're testing inside an xsl:for-each loop.
The context is set to each node returned by the select expression
in turn. What you want is

test = ". != ''"

Gosh! Now i feel very stupid! I though that in XML this

<node> </node>

was exactly the same as
<node></node>

It's qbvious that the
<xsl:if test ". != '' ">
couldn't work, because it should have been

<xsl:if test ". != ' ' ">

Gosh again..

It's sunday morning 8.pm

thank you very much for helping

Andrea
Jul 20 '05 #9
"Andrea Maschio" <am******@liber o.it> wrote in message
news:cc******** *************** ***@posting.goo gle.com
It's qbvious that the
<xsl:if test ". != '' ">
couldn't work, because it should have been

<xsl:if test ". != ' ' ">


No, it should be
<xsl:if test "normalize-space(.) != ''">

Martin
Jul 20 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1206
by: Steven | last post by:
I'm using XSL to transform an XML document to HTML, however I'm encountering the following problem.I want to test a couple of values using an xsl:if statement and then print a couple of HTML tags only when the condition is met: <xsl:if test="position() = $countPar"> </td> <td width="50%" valign="top">
3
3895
by: Lizard | last post by:
OK, total newbie here, so this may be a mind-numbingly dumb question AND I may be phrasing it badly. I have an xsl:template which looks like this: <xsl:template match="LoanRecord"> <hr> <xsl:number count="LoanRecord" format="1"/><br/> Loan ID:<xsl:value-of select="loan_no"> </xsl:value-of><br/> Calculated CLTV:<xsl:value-of select="format-number (curr_balance div
2
10816
by: Jørn Tommy Kinderås | last post by:
I need to get nodes in a xml file that match one out of two parameters...but how can I create a or statemement with <xsl:if>? E.G ---xml-- ... <movie> <title>T2</title> </movie> <movie>
3
3600
by: Eric Theil | last post by:
I'm at my wit's end with this one. Within an xsl:if test, I'm not able to get 2 variables to properly evaluate if one of them is wrapped within a string function. <!-- This works --> <xsl:if test="$var:v141=&quot;true&quot;"> <!-- This doesn't work --> <xsl:if test="string($var:v141)=&quot;true&quot;">
5
2336
by: Luke Vogel | last post by:
Hi all, Probably a really basic question, but I cant find an answer ... I have an xml file of books something like: <product> <isbn>0-735-61374-5</isbn> <title>Microsoft Visual Basic Step By Step</title> <author>Michael Halvorsen</author> <subject>Programming</subject>
3
14279
by: tldisbro | last post by:
Hello All, I am trying to use the returned value of the <fo:page-number> element/function in my <xsl:if> test condition. But am unsuccessful in doing so. Is it possible to use it in this fashion with a conversion or correct syntax? I would like to test the current page number and see if it is even or odd - and if it is odd I would like to perform additional steps. I would like to do something like this (assume all namespaces are set):...
4
2968
by: Doulos05 | last post by:
Ok, this seems like it should be easy, but it has escaped me. Here is my xml file: <ref_sheet> <item> <date>2007/04/06</date> <product>124567</product> <description>TAB DIVIDERS</description> <note>Description of problem here</note>
4
4414
by: grbeal | last post by:
How do I test for a child element with xsl if condition? We have a vendor application that outputs an XML file containing records of School Closings due to inclement weather. That XML file gets FTP'd to my web host when the Access database is changed. I'm using Dreamweaver to create an XSLT fragment to read the XML file and include the HTML output into my ASP page. It works fine to display the XML data, the School Closings, in my web page. ...
3
2352
by: z1 | last post by:
hi- i am fooling around with soap and weather templates. for some reason either this if or select is failing. i am very new to xml and found this code at another site. i can show you the xml and then the xslt sample code that is not matching. please look and if it jumps right off the page give me a tip on why it didnt select the data. i think for most xslt people this will be easy. i want the if to work if it is null because
0
8969
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8788
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
6751
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3279
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.