xsl:if or xsl:choose to filter a database and call a templat

Here should go questions about transforming XML with XSLT and FOP.
ffJw334Me
Posts: 2
Joined: Thu Feb 12, 2009 5:16 am

xsl:if or xsl:choose to filter a database and call a templat

Post by ffJw334Me »

Eeeuuuureeekka! Hey George. I got it.
I knew that your input had placed me near the goal. I ran trials until I settled for this xslt:
code:
<?xml version="1.0" encoding="utf-8"?>
<xsl:transform version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:regex="http://exslt.org/regular-expressions"
xmlns="http://www.w3.org/1999/xhtml">
<xsl:param name="filter" select="'EWR'"/>
<xsl:template match="/">
<html>
<head>
<title>Special Airline Fares List</title>
</head>
<body>
<h1>Special Fares List</h1>
<xsl:for-each select="XML/item">

<xsl:if test="contains(description, $filter)">

<xsl:call-template name="q" />

</xsl:if>
</xsl:for-each>
</body>
</html>
</xsl:template>
<xsl:template name="q" match="item">

<p>
<xsl:apply-templates select="pubDate"/>
<xsl:apply-templates select="title"/>
<xsl:apply-templates select="link"/>
<xsl:apply-templates select="description"/>
</p>

</xsl:template>
<xsl:template match="pubDate">
<br /><span style="color:#991111">
<xsl:value-of select="." />
</span><br />
</xsl:template>
<xsl:template match="title">
<span style="color:#2A7B39">
<xsl:value-of select="." />
</span><br />
</xsl:template>
<xsl:template match="link">
<span style="color:#636763">
<a href="{.}">
<xsl:value-of select="." />
</a>
</span><br />
</xsl:template>
<xsl:template match="description">
<span style="color:#111111">
<xsl:value-of select="." />
</span><br /><br />
</xsl:template>
</xsl:transform>

which does precisely what people need it to do. Now I can move to the next hair pulling, forehead slapping project: passing the param in from a form.

Again, George, thank you.
Steve


Hello;
I am teaching myself to capture a form variable and pass it via param to xslt where form input will filter an xml database and transform the results for display on the xhtml document. It seems my task is complicated because I do not want to return just the node text but all of the child nodes text so the user can have the information and link to proceed further.

Here is a snippet from the xml:
code:
<item>
<pubDate>Tue, 20 Jan 2009 14:12:00 UT</pubDate>
<title>Air France Airlines DE Specials 90120</title>
<link>http://airtravelcenter.com/aff/air-fran ... .htm</link>
<description>PREIS IM EUR, Hin und Ruckflug inkl Steuern Gebuhren, Buchungszeitraum ab sofort bis 17 Feb, Preise ab nach Hamburg: Sao Paolo 595, Rio de Janeiro 640, Caracas 590, Mexiko City 589, Buenos Aires 877, Bogota 650, Santiago de Chile 927, Havanna 488, Saint Martin 721, Peking 491, Shanghai 491, Hongkong 605, Seoul 535, Singapur 527, Tokio 674, Osaka 634, Nagoya 693, Mumbai 502, Delhi 502, Chennai 522, Bangalore 519, Antananarivo 998, Dubai 452, Amman 475.</description>
</item>
<item>
<pubDate>Tue, 03 Feb 2009 12:18:00 UT</pubDate>
<title>Air France Airlines SE Specials 90203</title>
<link>http://airtravelcenter.com/aff/air-fran ... .htm</link>
<description>Prices in SEK, Return Flights, Tax Included, BOOK BY 16 Feb, Depart Stockholm TO: Paris 1 395, Nice 1 695, Toulouse 2 295, Rome 1 695, Madrid 1 695, Barcelona 1 695, Geneva 1 695, Malaga 1 695, New York 3 670, Chicago 4 155, Miami 4 630, Los Angeles 5 030, Mexico City 6 565, Bogota 8 300, Buenos Aires 8 065, Caracas 7 400, Sao Paulo 6 270, Rio de Janeiro 6 370, Beijing 4 480, Shanghai 4 830, Hong Kong 6 000, Tokyo 6 405, Hanoi 8 290, Cairo 4 380, Dubai 4 640, Beirut 5 290, Johannesburg 6 375, Dakar 8 420, Mauritius 7 815, Santo Domingo 7 860, Havana 7 875, Seychelles 8 285, Saint Marteen 8 440.</description>
</item>

and here is the xslt:
code:
<?xml version="1.0" encoding="utf-8"?>
<xsl:transform version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:regex="http://exslt.org/regular-expressions"
xmlns="http://www.w3.org/1999/xhtml">
<xsl:template match="XML">
<html>
<head>
<title>Special Airline Fares List</title>
</head>
<body>
<h1>Special Fares List</h1>
<xsl:apply-templates/>
</body>
</html>
<xsl:call-template name="query"/>
</xsl:template>
<xsl:template name="query" match="query">
<xsl:for-each select="XML/item/*">
For now, I am just trying to type in what would be passed in from the form, just to get my brain wrapped around this part. I will tackle the javascript portion once I have this filter working.
<xsl:choose>
<xsl:when test="???">
<xsl:call-template name="q"/>
</xsl:when>
<xsl:otherwise>
<xsl:text>EWR not found</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
<xsl:template name="q" match="item">

<p>
<xsl:apply-templates select="pubDate"/>
<xsl:apply-templates select="title"/>
<xsl:apply-templates select="link"/>
<xsl:apply-templates select="description"/>
</p>

</xsl:template>
<xsl:template match="pubDate">
<br /><span style="color:#991111">
<xsl:value-of select="." />
</span><br />
</xsl:template>
<xsl:template match="title">
<span style="color:#2A7B39">
<xsl:value-of select="." />
</span><br />
</xsl:template>
<xsl:template match="link">
<span style="color:#636763">
<a href="{.}">
<xsl:value-of select="." />
</a>
</span><br />
</xsl:template>
<xsl:template match="description">
<span style="color:#111111">
<xsl:value-of select="." />
</span><br /><br />
</xsl:template>
</xsl:transform>

Your assistance will be appreciated. I had this down pat for a long time and all browsers would work. IE7 is using the old xsl and displays all nodes in text but does not do the transformation I am working on here. I hope to get this project, once again, available to all (or most) browsers.
Last edited by ffJw334Me on Fri Feb 13, 2009 1:01 am, edited 1 time in total.
george
Site Admin
Posts: 2097
Joined: Thu Jan 09, 2003 2:58 pm

Re: xsl:if or xsl:choose to filter a database and call a templat

Post by george »

It is not very clear for me what you are trying to do... Have a look at the following stylesheet

Code: Select all


<?xml version="1.0" encoding="utf-8"?>
<xsl:transform version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:regex="http://exslt.org/regular-expressions"
xmlns="http://www.w3.org/1999/xhtml">

<xsl:param name="filter" select="'EUR'"/>

<xsl:template match="XML">
<html>
<head>
<title>Special Airline Fares List</title>
</head>
<body>
<h1>Special Fares List</h1>
<xsl:apply-templates/>
<h1>Filtered</h1>
<xsl:call-template name="query"/>
</body>
</html>
</xsl:template>

<xsl:template name="query">
<xsl:for-each select="/XML/item">
<xsl:choose>
<xsl:when test="contains(description, $filter)">
<xsl:call-template name="q"/>
</xsl:when>
<xsl:otherwise>
<xsl:text>EWR not found</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>

<xsl:template name="q">
<p>
<xsl:apply-templates select="pubDate"/>
<xsl:apply-templates select="title"/>
<xsl:apply-templates select="link"/>
<xsl:apply-templates select="description"/>
</p>
</xsl:template>
<xsl:template match="pubDate">
<br /><span style="color:#991111">
<xsl:value-of select="." />
</span><br />
</xsl:template>
<xsl:template match="title">
<span style="color:#2A7B39">
<xsl:value-of select="." />
</span><br />
</xsl:template>
<xsl:template match="link">
<span style="color:#636763">
<a href="{.}">
<xsl:value-of select="." />
</a>
</span><br />
</xsl:template>
<xsl:template match="description">
<span style="color:#111111">
<xsl:value-of select="." />
</span><br /><br />
</xsl:template>
</xsl:transform>
It will list in the second section (Filtered) only the items that contain EUR in their description, where EUR is the value of the filter parameter. If you pass another value to the parameter then you will get a different set of items listed in the second section.

The above assumes an XML like:

Code: Select all


<XML>
<item>
<pubDate>Tue, 20 Jan 2009 14:12:00 UT</pubDate>
<title>Air France Airlines DE Specials 90120</title>
<link>http://airtravelcenter.com/aff/air-france-specials.htm</link>
<description>PREIS IM EUR, Hin und Ruckflug inkl Steuern Gebuhren, Buchungszeitraum ab sofort
bis 17 Feb, Preise ab nach Hamburg: Sao Paolo 595, Rio de Janeiro 640, Caracas 590, Mexiko
City 589, Buenos Aires 877, Bogota 650, Santiago de Chile 927, Havanna 488, Saint Martin 721,
Peking 491, Shanghai 491, Hongkong 605, Seoul 535, Singapur 527, Tokio 674, Osaka 634, Nagoya
693, Mumbai 502, Delhi 502, Chennai 522, Bangalore 519, Antananarivo 998, Dubai 452, Amman
475.</description>
</item>
<item>
<pubDate>Tue, 03 Feb 2009 12:18:00 UT</pubDate>
<title>Air France Airlines SE Specials 90203</title>
<link>http://airtravelcenter.com/aff/air-france-specials.htm</link>
<description>Prices in SEK, Return Flights, Tax Included, BOOK BY 16 Feb, Depart Stockholm TO:
Paris 1 395, Nice 1 695, Toulouse 2 295, Rome 1 695, Madrid 1 695, Barcelona 1 695, Geneva 1
695, Malaga 1 695, New York 3 670, Chicago 4 155, Miami 4 630, Los Angeles 5 030, Mexico City
6 565, Bogota 8 300, Buenos Aires 8 065, Caracas 7 400, Sao Paulo 6 270, Rio de Janeiro 6 370,
Beijing 4 480, Shanghai 4 830, Hong Kong 6 000, Tokyo 6 405, Hanoi 8 290, Cairo 4 380, Dubai 4
640, Beirut 5 290, Johannesburg 6 375, Dakar 8 420, Mauritius 7 815, Santo Domingo 7 860,
Havana 7 875, Seychelles 8 285, Saint Marteen 8 440.</description>
</item>
</XML>
and the result looks like this

Code: Select all


<?xml version="1.0" encoding="utf-8"?><html xmlns="http://www.w3.org/1999/xhtml" xmlns:regex="http://exslt.org/regular-expressions"><head><title>Special Airline Fares List</title></head><body><h1>Special Fares List</h1>

<br/><span style="color:#991111">Tue, 20 Jan 2009 14:12:00 UT</span><br/>
<span style="color:#2A7B39">Air France Airlines DE Specials 90120</span><br/>
<span style="color:#636763"><a href="http://airtravelcenter.com/aff/air-france-specials.htm">http://airtravelcenter.com/aff/air-france-specials.htm</a></span><br/>
<span style="color:#111111">PREIS IM EUR, Hin und Ruckflug inkl Steuern Gebuhren, Buchungszeitraum ab sofort
bis 17 Feb, Preise ab nach Hamburg: Sao Paolo 595, Rio de Janeiro 640, Caracas 590, Mexiko
City 589, Buenos Aires 877, Bogota 650, Santiago de Chile 927, Havanna 488, Saint Martin 721,
Peking 491, Shanghai 491, Hongkong 605, Seoul 535, Singapur 527, Tokio 674, Osaka 634, Nagoya
693, Mumbai 502, Delhi 502, Chennai 522, Bangalore 519, Antananarivo 998, Dubai 452, Amman
475.</span><br/><br/>


<br/><span style="color:#991111">Tue, 03 Feb 2009 12:18:00 UT</span><br/>
<span style="color:#2A7B39">Air France Airlines SE Specials 90203</span><br/>
<span style="color:#636763"><a href="http://airtravelcenter.com/aff/air-france-specials.htm">http://airtravelcenter.com/aff/air-france-specials.htm</a></span><br/>
<span style="color:#111111">Prices in SEK, Return Flights, Tax Included, BOOK BY 16 Feb, Depart Stockholm TO:
Paris 1 395, Nice 1 695, Toulouse 2 295, Rome 1 695, Madrid 1 695, Barcelona 1 695, Geneva 1
695, Malaga 1 695, New York 3 670, Chicago 4 155, Miami 4 630, Los Angeles 5 030, Mexico City
6 565, Bogota 8 300, Buenos Aires 8 065, Caracas 7 400, Sao Paulo 6 270, Rio de Janeiro 6 370,
Beijing 4 480, Shanghai 4 830, Hong Kong 6 000, Tokyo 6 405, Hanoi 8 290, Cairo 4 380, Dubai 4
640, Beirut 5 290, Johannesburg 6 375, Dakar 8 420, Mauritius 7 815, Santo Domingo 7 860,
Havana 7 875, Seychelles 8 285, Saint Marteen 8 440.</span><br/><br/>

<h1>Filtered</h1><p><br/><span style="color:#991111">Tue, 20 Jan 2009 14:12:00 UT</span><br/><span style="color:#2A7B39">Air France Airlines DE Specials 90120</span><br/><span style="color:#636763"><a href="http://airtravelcenter.com/aff/air-france-specials.htm">http://airtravelcenter.com/aff/air-france-specials.htm</a></span><br/><span style="color:#111111">PREIS IM EUR, Hin und Ruckflug inkl Steuern Gebuhren, Buchungszeitraum ab sofort
bis 17 Feb, Preise ab nach Hamburg: Sao Paolo 595, Rio de Janeiro 640, Caracas 590, Mexiko
City 589, Buenos Aires 877, Bogota 650, Santiago de Chile 927, Havanna 488, Saint Martin 721,
Peking 491, Shanghai 491, Hongkong 605, Seoul 535, Singapur 527, Tokio 674, Osaka 634, Nagoya
693, Mumbai 502, Delhi 502, Chennai 522, Bangalore 519, Antananarivo 998, Dubai 452, Amman
475.</span><br/><br/></p>EWR not found</body></html>
Hope that helps,
George
George Cristian Bina
ffJw334Me
Posts: 2
Joined: Thu Feb 12, 2009 5:16 am

Re: xsl:if or xsl:choose to filter a database and call a templat

Post by ffJw334Me »

Thank you, George;

The xslt continued to print out all items/* so I moved the for each into the root template (was XML, now '/') like this:

<?xml version="1.0" encoding="utf-8"?>
<xsl:transform version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:regex="http://exslt.org/regular-expressions"
xmlns="http://www.w3.org/1999/xhtml">
<xsl:param name="filter" select="'EWR'"/>
<xsl:template match="/">
<html>
<head>
<title>Special Airline Fares List</title>
</head>
<body>
<h1>Special Fares List</h1>
<xsl:for-each select="XML/item/description">
<xsl:choose>
<xsl:when test="contains(., $filter)">
<xsl:call-template name="q"/>
</xsl:when>
<xsl:otherwise>
<xsl:text>EWR not found</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</body>
</html>
</xsl:template>
<xsl:template name="q" match="item">

<p>
<xsl:apply-templates select="pubDate"/>
<xsl:apply-templates select="title"/>
<xsl:apply-templates select="link"/>
<xsl:apply-templates select="description"/>
</p>

</xsl:template>
<xsl:template match="pubDate">
<br /><span style="color:#991111">
<xsl:value-of select="." />
</span><br />
</xsl:template>
<xsl:template match="title">
<span style="color:#2A7B39">
<xsl:value-of select="." />
</span><br />
</xsl:template>
<xsl:template match="link">
<span style="color:#636763">
<a href="{.}">
<xsl:value-of select="." />
</a>
</span><br />
</xsl:template>
<xsl:template match="description">
<span style="color:#111111">
<xsl:value-of select="." />
</span><br /><br />
</xsl:template>
</xsl:transform>

and the filter now does filter but returns:

Special Fares List
<!-- this and each below are filter selected nodes -->
EWR not foundEWR not foundEWR not foundEWR not foundEWR not foundEWR not foundEWR not foundEWR not foundEWR not foundEWR not foundEWR not foundEWR not foundEWR not foundEWR not foundEWR not foundEWR not foundEWR not foundEWR not foundEWR not found

EWR not foundEWR not foundEWR not foundEWR not foundEWR not foundEWR not foundEWR not foundEWR not foundEWR not foundEWR not foundEWR not foundEWR not foundEWR not found

EWR not foundEWR not foundEWR not foundEWR not foundEWR not foundEWR not foundEWR not foundEWR not foundEWR not foundEWR not foundEWR not foundEWR not foundEWR not foundEWR not foundEWR not foundEWR not foundEWR not foundEWR not foundEWR not foundEWR not foundEWR not foundEWR not foundEWR not foundEWR not foundEWR not foundEWR not foundEWR not found

EWR not foundEWR not foundEWR not found

instead of the text() of each returned node.

I see this as progress and I am grateful to you. In case you can make spare a few minutes more, the test is at this URL:
http://airtravelcenter.com/airfare-wirea.xml
and the actual database is at this URL:
http://airtravelcenter.com/airfare-wire.xml

best wishes,
Steve
Post Reply