Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parameters inside a match sentence for filtering

Status
Not open for further replies.

edgarsosa

Programmer
Mar 22, 2002
5
0
0
MX
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

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=&quot;record[firstname='Daren']&quot;>

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

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

<xsl:template match=&quot;record[$filterColumn=$filterValue]&quot;>

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=&quot;filterRecords('firstname','Daren')&quot;>
Is it possible to do something like
<body onload=&quot;filterRecords('firstname','*')&quot;>


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=&quot;showRecords('id','ascending')&quot;>
-->
<body onload=&quot;filterRecords('firstname','Daren')&quot;>

<script language=&quot;Javascript&quot;>//<![CDATA[
function call(xslIsland,xmlIsland){
var xslDoc=new ActiveXObject(&quot;MSXML2.FreeThreadedDOMDocument&quot;);
var rsltDoc=new ActiveXObject(&quot;MSXML2.FreeThreadedDOMDocument&quot;);
var xslTemplate=new ActiveXObject(&quot;MSXML2.XSLTemplate&quot;);
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,&quot;sortKey&quot;, sortKey,&quot;sortOrder&quot;,sortOrder);
window.container.innerHTML=htmlDoc.xml;
}

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

//]]></script>

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

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

</records>
</xml>

<xml id=&quot;_ShowRecords&quot;>
<xsl:stylesheet
version=&quot;1.0&quot;
xmlns:xsl=&quot;<xsl:param name=&quot;sortKey&quot; select=&quot;'firstname'&quot;/>
<xsl:param name=&quot;sortOrder&quot; select=&quot;'ascending'&quot;/>
<xsl:template match=&quot;/&quot;>
<xsl:apply-templates select=&quot;records&quot;/>
</xsl:template>

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

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

</xsl:stylesheet>
</xml>

<xml id=&quot;_FilterRecords&quot;>
<xsl:stylesheet
version=&quot;1.0&quot;
xmlns:xsl=&quot;<xsl:param name=&quot;filterColumn&quot; select=&quot;'firstname'&quot;/>
<xsl:param name=&quot;filterValue&quot; select=&quot;'Daren'&quot;/>
<xsl:template match=&quot;/&quot;>
<xsl:apply-templates select=&quot;records&quot;/>
</xsl:template>

<xsl:template match=&quot;records&quot;>
<div>
<xsl:apply-templates select=&quot;record&quot;>
</xsl:apply-templates>
</div>
</xsl:template>

<!--

<xsl:template match=&quot;record[firstname='Daren']&quot;>
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=&quot;record['{$filterColumn}'='Daren']&quot;>
This one above this line, it just doesn't work at all
-->

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


</xsl:stylesheet>
</xml>

</body>
</html>


__________________
Edgar Sosa
Mexicali BC Mexico
 
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=&quot;records&quot;>
<div>
<xsl:apply-templates select=&quot;record[firstname=$filterValue]&quot;>
</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=&quot;record[$filterColumn=$filterValue]&quot;>

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=&quot;_FilterRecords&quot;>
<xsl:stylesheet
version=&quot;1.0&quot;
xmlns:xsl=&quot; <xsl:param name=&quot;filterColumn&quot; select=&quot;'firstname'&quot;/>
<xsl:param name=&quot;filterValue&quot; select=&quot;'Daren'&quot;/>
<xsl:template match=&quot;/&quot;>
<xsl:apply-templates select=&quot;records&quot;/>
</xsl:template>

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


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


</xsl:stylesheet>
</xml>
 
try xsl:if/xsl:choose (?) on the filterColumn to &quot;call&quot; the appropriate template - assuming there are a limited number of columns you want to search on...
 
Someone at another forum already solved my question, I post it here so it could help others

I had this
Code:
select=&quot;row[$filterKey=$filterValue]&quot;

but the correct way is this one
Code:
select=&quot;row[*[name()=$filterKey and text()=$filterValue]]

Thanks for the time to answer this question, I totally forgot to post the answer here, sorry.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top