472,779 Members | 1,918 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,779 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 8261
"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: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.