How do I use a variable or parameter in an <xsl:if> expression to compare it with the value of an xm


M

mark4asp

How do I use a variable or parameter in an <xsl:if> expression to
compare it with the value of an xml attribute?

For instance the entryDate attribute value shown below. I want to use
an if expression to compare this with an initial value (say
2020-01-01). When the entryDate is less than the initial value I want
to write a <tr> of data showing only the date (e.g. Tuesday, 13 Nov
2007), and then set the variable to the current entryDate value. In
every case I want to show the data below in a table with alternating
color rows (which I have written the xsl for). I need to figure out is
how to use variables in my xslt code. Are there any articles describing
this? I need to output a pager at the end of the table of data [which
will need a minPages variable set to 1]. So I also need to know how to
compare xsl variables with attributes in both the <xsl:if> amd
<xsl:for-each> declarations.


In psuedocode, the logic is:

if ($date > @entryDate) then
set $date = @entryDate
write (<tr><td colspan="2"> @entryDate formatted as (e.g. Tuesday, 13
Nov 2007)</td></tr>)
end if

where $date is a variable initially equal to '2020-01-01'
@entryDate is an attribute (see xml file below).

Finally, Does it make sense to use variable types in xsl, such as
integers, datetime, etc., or is everything a string?



html file using both files below
================================

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Table XML Exercise</title>
<link rel="stylesheet" type="text/css" href="images/news.css" />
<script type="text/javascript" src="zxml.js"></script>
<script type="text/javascript">
var str1;
var str2 = "<tr><td colspan='8'>No data to display.</td></tr>";

function init() {
loadXml("funds.xml", "funds.xsl", "divNews");
}

function loadXml(xmlFilename, xslFile, elementID) {
var oReq = zXmlHttp.createRequest();
oReq.onreadystatechange = function () {
if (oReq.readyState == 4) {
// only if "OK"
if (oReq.status == 200) {
transformXml(oReq.responseText, xslFile, elementID);
}
}
};
oReq.open("GET", xmlFilename, true);
oReq.send(null);
}

function transformXml(sResponseText, xslFilename, elementId) {
var oXmlDom = zXmlDom.createDocument();
oXmlDom.async = false;
oXmlDom.loadXML(sResponseText);

var oXslDom = zXmlDom.createDocument();
oXslDom.async = false;
oXslDom.load(xslFilename);

str1 = zXslt.transformToText(oXmlDom, oXslDom);
if (str1 == "")
str1 = str2;
document.getElementById(elementId).innerHTML = str1;
}

</script>
</head>
<body onload="init();">
<div>
<table cellspacing="0" class="fundtable">
<tr>
<th class="ReportListColumn_1_Normal">Advisor</th>
<th class="ReportListColumn_2_Normal">Activity</th>
<th class="ReportListColumn_3_Normal">Client</th>
<th class="ReportListColumn_4_Normal">Timetable</th>
<th class="ReportListColumn_5_Normal">PDF</th>
</tr>
</table>
</div>
<div id="divNews"></div>
</body>
</html>




========
news.xsl
========

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:eek:utput method="xml" omit-xml-declaration="yes" indent="yes" />
<xsl:template match="/">
<table cellspacing="2" class="DayGroup">
<xsl:for-each select="news/stories/story" >
<tr>
<td class="ReportListColumn_1_Blue">
<xsl:value-of select="@advisor" /><br />
<xsl:value-of select="@advisorOffice" />
</td>
<td class="ReportListColumn_2_Blue"><xsl:value-of select="@activity"
/></td>
<td class="ReportListColumn_3_Blue"><xsl:value-of select="@client"
/></td>
<td class="ReportListColumn_4_Blue"><xsl:value-of
select="@timetable" /></td>
<td class="ReportListColumn_5_Blue"><a
href="javascript:downloadPDF(1);"><img class="PdfIcon"
src="images/pdfdnld.gif" alt="Download PDF" /></a>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>



========
news.xml
========


<?xml version="1.0" encoding="utf-8"?>
<news>
<page currrent="3" forwardButton="true" maxPages="490" />
<stories>
<story id="5842" entryDate="2007-11-12" advisor="Watson Wyatt"
advisorOffice="Leeds" activity="UK Corporate Pension Scheme Seeks Hedge
Funds in Strategy Overhaul" client="Clariant UK Pension Scheme"
timetable="N/A" />
<story id="5841" entryDate="2007-11-12" advisor="Cardano"
advisorOffice="London" activity="UK Corporate Pension Fund Replaces
Consultant Amid Pan-Europe Merger Rumours" client="AstraZeneca UK
Pension Fund" timetable="N/A" />
<story id="5840" entryDate="2007-11-12" advisor="Not disclosed"
advisorOffice="" activity="Belgian Corporate Pension Fund Signals
Pan-Europe Merger, UK Sister Hires Consultant" client="Astra Zeneca
Belgium" timetable="2008" />
<story id="5839" entryDate="2007-11-11" advisor="Hymans Robertson"
advisorOffice="London" activity="UK Local Authority Pension Fund
Outlines Currency Investment Program" client="Suffolk County Council
Pension Fund" timetable="N/A" />
<story id="5838" entryDate="2007-11-11" advisor="Complementa
Investment Controlling" advisorOffice="St Gallen" activity="Swiss
Corporate Pension Fund Eyes Private Equity, Puts Lacklustre Managers
Under Review" client="Oerlikon Contraves, Pensionskasse der"
timetable="Q4 2007" />
<story id="5837" entryDate="2007-11-11" advisor="Mercer Investment
Consulting" advisorOffice="Dublin" activity="Irish Corporate Pension
Fund Overhauls Asset Allocation" client="Guinness Ireland Group Pension
Scheme" timetable="N/A" />
<story id="5836" entryDate="2007-11-11" advisor="Gissings Consultancy
Services Limited" advisorOffice="London" activity="UK Corporate Pension
Scheme Set to Replace Sole Manager" client="Commonwealth War Graves
Commission Superannuation Fund" timetable="Q! 2008" />
<story id="5835" entryDate="2007-11-11" advisor="Bruni Marino &amp;
C" advisorOffice="Milan" activity="Italian Negoziale Pension Fund Mulls
New High-Risk Mandate" client="Concreto, Fondo" timetable="2008" />
<story id="5834" entryDate="2007-11-11" advisor="No consultant
engaged" advisorOffice="" activity="Austrian Bank Seeks Investment
Advice" client="Karntner Landes- und Hypothekenbank-Holding"
timetable="07/12/2007 10:00CET" />
<story id="5833" entryDate="2007-11-11" advisor="No consultant
engaged" advisorOffice="" activity="Irish Public Body Tenders for
Accountancy Services" client="Pensions Board, The"
timetable="03/01/2008 12:00GMT" />
</stories>
</news>
 
Ad

Advertisements

J

Joseph Kesselman

mark4asp said:
if ($date > @entryDate) then
set $date = @entryDate

XSLT variables are single-assignment. You need to use two separate
variable names, or turn the problem inside out and do the conditional
inside the computation of the variable's value. Which makes most sense
depends on the details of your stylesheet.

Conditionals: depending on what you're doing, the xsl:if or xsl:choose
directives may be the right answer
(http://www.w3.org/TR/xslt#section-Conditional-Processing), or you may
want a predicate test inside an XPath expression
(http://www.w3.org/TR/xpath#predicates). Again, which makes most sense
depends on the details of your stylesheet.
Finally, Does it make sense to use variable types in xsl, such as
integers, datetime, etc., or is everything a string?

In XSLT 1.0, everything is a string until converted in the expression.
In XSLT 2.0 and XQuery, types become more meaningful.
 
Ad

Advertisements

M

mark4asp

XSLT variables are single-assignment. You need to use two separate
variable names, or turn the problem inside out and do the conditional
inside the computation of the variable's value. Which makes most sense
depends on the details of your stylesheet.

Thanks Joseph, I discovered that yesterday anyway reading Michael Kay on
my way home - I wish text books would make that clear early on as it's
so fundamental.

So, I think I could best procede by using different data. Instead of
this:

<?xml version="1.0" encoding="utf-8"?>
<news>
<page currrent="3" forwardButton="true" maxPages="490" />
<stories>
<story id="5842" entryDate="2007-11-12" blah blah />
<story id="5841" entryDate="2007-11-12" blah blah />
<story id="5840" entryDate="2007-11-12" blah blah />
<story id="5839" entryDate="2007-11-11" blah blah />
<story id="5838" entryDate="2007-11-11" blah blah />
<story id="5837" entryDate="2007-11-11" blah blah />
<story id="5836" entryDate="2007-11-11" blah blah />
<story id="5835" entryDate="2007-11-11" blah blah />
<story id="5834" entryDate="2007-11-11" blah blah />
<story id="5833" entryDate="2007-11-11" blah blah />
</stories>
</news>

I could use this instead:

<?xml version="1.0" encoding="utf-8"?>
<news>
<page currrent="3" forwardButton="true" maxPages="490" />
<stories entryDate="2007-11-12">
<story id="5842" blah blah />
<story id="5841" blah blah />
<story id="5840" blah blah />
</stories>
<stories entryDate="2007-11-11">
<story id="5839" blah blah />
<story id="5838" blah blah />
<story id="5837" blah blah />
<story id="5836" blah blah />
<story id="5835" blah blah />
<story id="5834" blah blah />
<story id="5833" blah blah />
</stories>
</news>

Then I can solve it with a nested loop.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top