XML Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreXML Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today!
  #1  
Old February 11th, 2003, 09:17 PM
esosa esosa is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Location: Mexicali Mexico
Posts: 23 esosa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to esosa
Parameters inside a match sentence for filtering

Is it possible to pass parameters to a match Xpath (is this an Xpath sentence?) sentence?

I am using XML data Islands on IE 6.0, I want to apply the following XSLT to the XML.

by the way, the javascript functions are from an article written by Kurt Cagle, XML Pro
at this link
http://gethelp.devx.com/techtips/xm.../kc090101-1.asp

The following sentence works, but shows in the XSLT desired format only the one that fits in the criteria, and shows all the other ones like too ! only that in no format, just firstnames and lastname, no ID
<xsl:template match="record[firstname='Daren']">

I have stated 2 parameters wich are
<xsl:param name="filterColumn" select="'firstname'"/>
<xsl:param name="filterValue" select="'Daren'"/>

I would like to use something like this, in order to later be able to pass any filter criteria

<xsl:template match="record[$filterColumn=$filterValue]">

but it just doesn't works, the browser gives me the following error
<b>Error: Variables may not be used within this expression</b>

And another thing, I would like that on the body onload event, I could call the function wich passes the parameters but with a Wildcard, so It would display all the records, currently I do this on body onload
<body onload="filterRecords('firstname','Daren')">
Is it possible to do something like
<body onload="filterRecords('firstname','*')">


first things first, here bellow I put the entire code, any help will be appreciated

<html>
<head>
<title>Sorting and Filtering Records</title>
</head>

<!--
<body onload="showRecords('id','ascending')">
-->
<body onload="filterRecords('firstname','Daren')">

<script language="Javascript">//<![CDATA[
function call(xslIsland,xmlIsland){
var xslDoc=new ActiveXObject("MSXML2.FreeThreadedDOMDocument");
var rsltDoc=new ActiveXObject("MSXML2.FreeThreadedDOMDocument");
var xslTemplate=new ActiveXObject("MSXML2.XSLTemplate");
xslDoc.async = false;
rsltDoc.async = false;
xslDoc.load(xslIsland.XMLDocument);
xslTemplate.stylesheet=xslDoc;
var xslProc=xslTemplate.createProcessor();
xslProc.input=xmlIsland.XMLDocument;
xslProc.output=rsltDoc;
if (arguments.length >2 && arguments.length % 2 == 0){
for (var i=0;
i<Math.floor((arguments.length)/2)-1;
i++){
paramName=arguments[2*i+2];
paramValue=arguments[2*i+3];
xslProc.addParameter(paramName,paramValue);
}
}
xslProc.transform();
return rsltDoc;
}

function showRecords(sortKey,sortOrder){
var htmlDoc=
call(_ShowRecords,records,"sortKey", sortKey,"sortOrder",sortOrder);
window.container.innerHTML=htmlDoc.xml;
}

function filterRecords(filterColumn,filterValue){
var htmlDoc=
call(_FilterRecords,records,"filterColumn", filterColumn,"filterValue",filterValue);
window.container.innerHTML=htmlDoc.xml;
}

//]]></script>

Filters: &nbsp;&nbsp;<button onclick="filterRecords('firstname','Daren')">First Name = Daren</button><hr>
Sorting: &nbsp;&nbsp;<button onclick="showRecords('firstname',SortOrderList.value)">First Name</button>
<button onclick="showRecords('lastname',SortOrderList.value)">Last Name</button>
<button onclick= "showRecords('id',SortOrderList.value)">User ID</button>
<select name="SortOrderList" id="SortOrderList" value="ascending">
<option value="ascending" selected="selected">Ascending</option>
<option value="descending">Descending</option>
</select><hr>
<div id="container"></div>

<xml id="records">
<records>
<record id="101">
<firstname>Kurt</firstname>
<lastname>Cagle</lastname>
</record>
<record id="102">
<firstname>Aleria</firstname>
<lastname>Delamare</lastname>
</record>
<record id="103">
<firstname>Steven</firstname>
<lastname>Dallas</lastname>
</record>
<record id="104">
<firstname>Opus</firstname>
<lastname>Penguin</lastname>
</record>
<record id="105">
<firstname>Gina</firstname>
<lastname>Alacava</lastname>
</record>
<record id="106">
<firstname>Dean</firstname>
<lastname>Stanton</lastname>
</record>
<record id="107">
<firstname>Corey</firstname>
<lastname>Johnson</lastname>
</record>
<record id="108">
<firstname>Edgar</firstname>
<lastname>Marseiz</lastname>
</record>
<record id="109">
<firstname>Daren</firstname>
<lastname>Malecevitz</lastname>
</record>
<record id="110">
<firstname>Lori</firstname>
<lastname>Lemaris</lastname>
</record>

</records>
</xml>

<xml id="_ShowRecords">
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="sortKey" select="'firstname'"/>
<xsl:param name="sortOrder" select="'ascending'"/>
<xsl:template match="/">
<xsl:apply-templates select="records"/>
</xsl:template>

<xsl:template match="records">
<div>
<xsl:apply-templates select="record">
<xsl:sort select="*[name(.)=$sortKey]|@*[name(.)=$sortKey]" order="{$sortOrder}"/>
</xsl:apply-templates>
</div>
</xsl:template>

<xsl:template match="record">
<div>(<xsl:value-of select="@id"/>) <xsl:value-of select="firstname"/><xsl:text> </xsl:text><xsl:value-of select="lastname"/></div>
</xsl:template>

</xsl:stylesheet>
</xml>

<xml id="_FilterRecords">
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="filterColumn" select="'firstname'"/>
<xsl:param name="filterValue" select="'Daren'"/>
<xsl:template match="/">
<xsl:apply-templates select="records"/>
</xsl:template>

<xsl:template match="records">
<div>
<xsl:apply-templates select="record">
</xsl:apply-templates>
</div>
</xsl:template>

<!--

<xsl:template match="record[firstname='Daren']">
this one works, but it also displays all the other data without format,
only applies the format for the one that fits the match sentence

<xsl:template match="record['{$filterColumn}'='Daren']">
This one above this line, it just doesn't work at all
-->

<xsl:template match="record[$filterColumn=$filterValue]">
<div>(<xsl:value-of select="@id"/>) <xsl:value-of select="firstname"/><xsl:text> </xsl:text><xsl:value-of select="lastname"/>
</div>
</xsl:template>


</xsl:stylesheet>
</xml>

</body>
</html>

Last edited by esosa : February 11th, 2003 at 09:24 PM.

Reply With Quote
  #2  
Old February 12th, 2003, 02:05 PM
esosa esosa is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Location: Mexicali Mexico
Posts: 23 esosa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to esosa
I partially found a solution, the thing is that you cannot put a filter in the match sentence, but you can put it on the apply template part

like this

<xsl:template match="records">
<div>
<xsl:apply-templates select="record[firstname=$filterValue]">
</xsl:apply-templates>
</div>
</xsl:template>

Now the problem is that I don't know how to pass the $filterColumn parameter so it could look like this
<xsl:apply-templates select="record[$filterColumn=$filterValue]">

When I do that, the browser reports no errors, but simply does not show any records

here bellow I put the code with the filterColumn hardcoded as firstname.

Again, any help will be appreciated

<xml id="_FilterRecords">
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="filterColumn" select="'firstname'"/>
<xsl:param name="filterValue" select="'Daren'"/>
<xsl:template match="/">
<xsl:apply-templates select="records"/>
</xsl:template>

<xsl:template match="records">
<div>
<xsl:apply-templates select="record[firstname=$filterValue]">
</xsl:apply-templates>
</div>
</xsl:template>


<xsl:template match="record">
<div>(<xsl:value-of select="@id"/>) <xsl:value-of select="firstname"/><xsl:text> </xsl:text><xsl:value-of select="lastname"/>
</div>
</xsl:template>


</xsl:stylesheet>
</xml>

Reply With Quote
  #3  
Old February 14th, 2003, 08:27 PM
esosa esosa is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Location: Mexicali Mexico
Posts: 23 esosa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to esosa
Somebody Anonymous kindly posted a solution to my question, here I post it.


Hi Edgar

The select you've entered will always be false since you compare two variables, not the contents of the nodes you are looking for.

This is an altered version that does the jobb

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="wichColumn">firstname</xsl:param>
<xsl:param name="filterValue">Kurt</xsl:param>
<xsl:template match="/">
<!-- changed, xml is the element under root -->
<xsl:apply-templates select="xml/records"/>
</xsl:template>
<xsl:template match="records">
<div>
<xsl:apply-templates select="record[*[name()=$wichColumn and text()=$filterValue]]"/>
</div>
</xsl:template>
<xsl:template match="record">
<div>(<xsl:value-of select="@id"/>)
<xsl:value-of select="firstname"/>
<xsl:text/>
<xsl:value-of select="lastname"/>
</div>
</xsl:template>
</xsl:stylesheet>

Oh, I also removed the starting <xml id...
from the stylesheet.

Regards // Greger

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreXML Programming > Parameters inside a match sentence for filtering


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway