473,378 Members | 1,134 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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***@emailprovider.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:hand; 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 8292
"Andrea Maschio" <am******@libero.it> wrote in message
news:cc**************************@posting.google.c om
<!--xsl:if test="@Esito != 'Ok' ">
<span style="cursor:hand; 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(@Esito) != 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(@Esito) = 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(@Esito) != 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(@Esito) != '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********************@twister1.libero.it
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:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/root">
<body>
<xsl:apply-templates select="Dipendente"/>
</body>
</xsl:template>
<xsl:template match="Dipendente">
<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:otherwise>
<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="Dipendente">.

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********@arcor.de> wrote in message news:<3f***********************@newsread2.arcor-online.net>...
"Andrea Maschio" <am*************@libero.it> wrote in message
news:2x********************@twister1.libero.it
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:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/root">
<body>
<xsl:apply-templates select="Dipendente"/>
</body>
</xsl:template>
<xsl:template match="Dipendente">
<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:otherwise>
<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******@libero.it> wrote in message
news:cc**************************@posting.google.c om

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="Dipendente">.
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="Dipendente">.
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="NodeName">
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="stileconti.xslt"?>
<Import>
<Row>
<Data_operazione> 07/10/2003 </Data_operazione>
<Data_valuta> 12/09/2003 </Data_valuta>
<Importo_debito></Importo_debito>
<Importo_credito>9,8</Importo_credito>
<Causale>CausaleText</Causale>
</Row>
<Row>
<Data_operazione> 08/10/2003 </Data_operazione>
<Data_valuta> 09/09/2003 </Data_valuta>
<Importo_debito>30</Importo_debito>
<Importo_credito> </Importo_credito>
<Causale>CausaleText</Causale>
</Row>
</Import>
--------end of XML File---------------------------------------------
------------xslt file----------
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 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="background-color:#004080">
<span style="color:white; font-family:Verdana; font-size:9;
font-weight:bold; text-transform:capitalize">Data operazione</span>
</td>
<td style="background-color:#004080">
<span style="color:white; font-family:Verdana; font-size:9;
font-weight:bold; text-transform:capitalize">Data valuta</span>
</td>
<td style="background-color:#004080">
<span style="color:white; font-family:Verdana; font-size:9;
font-weight:bold; text-transform:capitalize">Importo debito</span>
</td>
<td style="background-color:#004080">
<span style="color:white; font-family:Verdana; font-size:9;
font-weight:bold; text-transform:capitalize">Importo credito</span>
</td>
<td style="background-color:#004080">
<span style="color:white; font-family:Verdana; font-size:9;
font-weight:bold; text-transform:capitalize">Causale</span>
</td>
</tr>
</thead>
<tbody>
<xsl:for-each select="../Row">
<tr>
<td style="background-color:#C0C0C0">
<xsl:for-each select="Data_operazione">
<span style="font-family:Verdana; font-size:9">
<xsl:apply-templates/>
</span>
</xsl:for-each>
</td>
<td style="background-color:#D3D3D3">
<xsl:for-each select="Data_valuta">
<span style="font-family:Verdana; font-size:9">
<xsl:apply-templates/>
</span>
</xsl:for-each>
</td>
<td style="background-color:#D90000">
<xsl:for-each select="Importo_debito">
<!--------------------Here it is the "choose"--------------------->
<xsl:choose >
<xsl:when test="Importo_debito != '' ">mycode for coloured
cell</xsl:when>
</xsl:choose>
<!---------------------------------------------------------------->
<span style="color:white; font-family:Verdana;
font-size:9">
<xsl:apply-templates/>
</span>
<xsl:choose><xsl:when test="@Importo_debito != ''
"> </xsl:when></xsl:choose>
</xsl:for-each>
</td>
<td style="background-color:#008000">
<xsl:for-each select="Importo_credito">
<span style="color:#FFFFFF; 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:lowercase">
<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******@libero.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="stileconti.xslt"?>
% <Import>
% <Row>
% <Data_operazione> 07/10/2003 </Data_operazione>
% <Data_valuta> 12/09/2003 </Data_valuta>
% <Importo_debito></Importo_debito>
% <Importo_credito>9,8</Importo_credito>
% <Causale>CausaleText</Causale>
% </Row>
% <Row>
% <Data_operazione> 08/10/2003 </Data_operazione>
% <Data_valuta> 09/09/2003 </Data_valuta>
% <Importo_debito>30</Importo_debito>
% <Importo_credito> </Importo_credito>
% <Causale>CausaleText</Causale>
% </Row>
% </Import>

% <?xml version="1.0" encoding="UTF-8"?>
% <xsl:stylesheet 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_debito != '' ">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.com
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******@libero.it> wrote in message
news:cc**************************@posting.google.c om
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
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...
3
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>...
2
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
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...
5
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...
3
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...
4
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...
4
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...
3
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.