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 MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XSL Search Form

Status
Not open for further replies.

Borvik

Programmer
Jan 2, 2002
1,392
US
OK, I remember doing this back in school - and I went to grab my old XML book, but found I had dumped it (go figure).

I'm looking at creating a searchable XML file that will be stored on a USB drive or CD - so no server-side language is available.

I have the XML working just fine, and the XSL displaying it the way I need it too.

I'm looking for a simple example that shows me how to do this type of search on an XML file. I've done quite a bit of searching for examples, but can't quite seem to find anything that does what I'm looking for. Any help would be appreciated.
 
OK, so I haven't completed the search form yet - but I have more information to add.

I've been playing with a JavaScript search example I found from Microsoft (bookFinder), that uses an html file with xml data islands.

I was able to get data I need to work within the rules of that search form (which is an exact search - still have to figure out a LIKE operator for searching), but displaying doesn't quite work properly.

It works fine when I just output the data, but not for conditional formatting of the data.

Code:
<DIV xmlns:xsl="[URL unfurl="true"]http://www.w3.org/TR/WD-xsl">[/URL]
<table style="table-layout:fixed" BORDER="2" CELLSPACING="2">
  <tr>
    <th>SKU</th><th>Description</th><th>Unit of Issue</th><th>List Price</th><th>Price</th><th>Origin</th></tr>
  <xsl:for-each select="item">
  <tr>
    <td><xsl:value-of select="sku"/></td>
    <td><xsl:value-of select="desc"/></td>
    <td><xsl:value-of select="uoi"/></td>
    <td><xsl:value-of select="list"/></td>
    <td><xsl:value-of select="price"/></td>
    <td><xsl:value-of select="origin"/></td>
  </tr>
  </xsl:for-each>
</table>
</DIV>

Here is the javascript that runs the pattern and applies the style:

Code:
function getSamples(){
  var strTitle = document.getElementById('txtTitle').value;
  //construct the pattern
  var patternString = "//item[";
  if (strTitle != "")
    patternString += "sku='" + strTitle + "'";
  patternString += "]"
  if (patternString == "//item[]")
    patternString = "//item";

  var selectedNodes = booksDoc.selectNodes(patternString);
  var bookHolder = booksDoc.createNode(1,"items","");
  for (var n=0; n< selectedNodes.length; n++)
    bookHolder.insertBefore(selectedNodes.item(n).cloneNode(true),null);

  insertTable.innerHTML = bookHolder.transformNode(bookSS.documentElement);
  }

In another version - where I'm just displaying the XML with a stylesheet applied, I have the SKU column bolded when an attribute named "exp" is equal to "True" - that works, but I'm using the declaration: <xsl:stylesheet version="1.0" xmlns:xsl=" - which clearly means XSLT.

I tried applying that xmlns:xsl declaration to the search style sheet so that I could use the <xsl:if> for my bold requirement, but that results in nothing showing up when the search is run (the table shows up, but no data).

Any ideas?
 
It works fine when I just output the data, but not for conditional formatting of the data.
Can you be more specific when you say "conditional formatting of the data." I'm not sure I understand exactly what you are trying to do.
 
Something like this:
Code:
<td><xsl:if test="@exp = 'True'"><xsl:attribute name="style">font-weight: bold</xsl:attribute></xsl:if><xsl:value-of select="sku" /></td>

I just stumbled acrossed another example, that I have displaying properly, and doing EXACT searches - know just to figure out the equivalent of the SQL "LIKE" operator.
 
Thanks for attempting to help ddiamond - it looks like I just needed a few more days to find what I was looking for.

I found a different MSDN article that did almost what I wanted:
I haven't finished yet, but if anyone is interested here's what I've got figured out:
Code:
<html>
<head>
	<title>Item Catalog</title>
    <style type="text/css">
    	.item_table, .item_table td, .item_table th{
			border-color: black;
			border-style: solid;
		}
		.item_table{
			border-width: 0 0 1px 1px;
			border-spacing: 0;
			border-collapse: collapse;
			width: 100%;
		}
		.item_table td, .item_table th{
			border-width: 1px 1px 0 0;
		}
		.category_header{
			background-color: #3366FF;
		}
		.column_headers{
			background-color: #FF0000;
		}
	</style>
</head>
<body>
<H1>Item Catalog</H1>
<DIV id="catalog_table"></DIV>

<xml id="item_catalog" src="gsa_exported.xml"></xml>
<xml id="catalog_filter">
	<xsl:stylesheet xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform"[/URL]
		xmlns:msxsl="urn:schemas-microsoft-com:xslt"
		version="1.0">
		
		<xsl:output method="xml" omit-xml-declaration="yes"/>
		<xsl:param name="sku" select="'*'" />
		<xsl:param name="desc" select="'*'" />
		<xsl:param name="uoi" select="'*'" />
		<xsl:param name="origin" select="'*'" />
		
		<xsl:variable name="colHeader">
      		<tr class="column_headers">
        		<th>Part Number</th>
	        	<th>Description</th>
    	    	<th>Unit of Issue</th>
        		<th>List Price</th>
        		<th>Price</th>
	        	<th>Country of Origin</th>
    	  	</tr>
		</xsl:variable>
		
		<xsl:template match="/">
			<xsl:apply-templates select="items"/>
		</xsl:template>
		
		<xsl:template match="items">
			<table class="item_table">
				<tr>
					<td style="border-width: 1px 0 0 0;"><input type="text" id="txtSku"><xsl:attribute name="value"><xsl:value-of select="$sku" /></xsl:attribute></input></td>
					<td style="border-width: 1px 0 0 0;"><input type="text" id="txtDesc"><xsl:attribute name="value"><xsl:value-of select="$desc" /></xsl:attribute></input></td>
					<td style="border-width: 1px 0 0 0;"><select id="cboUOI">
							<option value="*"><xsl:if test="$uoi='*'"><xsl:attribute name="selected">Selected</xsl:attribute></xsl:if>All</option>
					<xsl:for-each select="/items/item">
						<xsl:if test="not(uoi = preceding-sibling::item/uoi)">
							<option><xsl:attribute name="value"><xsl:value-of select="uoi" /></xsl:attribute><xsl:if test="$uoi=uoi"><xsl:attribute name="selected">Selected</xsl:attribute></xsl:if><xsl:value-of select="uoi" /></option>
						</xsl:if>
					</xsl:for-each>
					</select></td>
					<td style="border-width: 1px 0 0 0;"><input type="text" id="txtLPrice" /></td>
					<td style="border-width: 1px 0 0 0;"><input type="text" id="txtGPrice" /></td>
					<td style="border-width: 1px 1px 0 0;"><select id="cboOrigin">
							<option value="*"><xsl:if test="$origin='*'"><xsl:attribute name="selected">Selected</xsl:attribute></xsl:if>All</option>
					<xsl:for-each select="/items/item">
						<xsl:if test="not(origin = preceding-sibling::item/origin)">
							<option><xsl:attribute name="value"><xsl:value-of select="origin" /></xsl:attribute><xsl:if test="$origin=origin"><xsl:attribute name="selected">Selected></xsl:attribute></xsl:if><xsl:value-of select="origin" /></option>
						</xsl:if>
					</xsl:for-each>
					</select></td>
				</tr>
				<tr>
					<td colspan="6" style="border-width: 0 1px 0 0; text-align: right;"><input type="button" onclick="doSearch()" value="Search" /></td>
				</tr>
				<xsl:apply-templates select="item[(($sku='*') or (contains(./sku,$sku))) and (($desc='*') or (contains(./desc,$desc))) and (($uoi='*') or ($uoi = uoi)) and (($origin='*') or ($origin = origin))]" />
			</table>
		</xsl:template>
		
		<xsl:template match="item">
			<xsl:if test="not(category = preceding-sibling::item/category)">
				<tr>
					<th class="category_header" colspan="6"><xsl:value-of select="category" /></th>
				</tr>
				<xsl:copy-of select="$colHeader" />
			</xsl:if>
			<tr>
				<td><xsl:if test="@exp = 'True'"><xsl:attribute name="style">font-weight: bold</xsl:attribute></xsl:if><xsl:value-of select="sku" /></td>
				<td><xsl:value-of select="desc" /></td>
				<td><xsl:value-of select="uoi" /></td>
				<td><xsl:value-of select="list" /></td>
				<td><xsl:value-of select="price" /></td>
				<td><xsl:value-of select="origin" /></td>
			</tr>
		</xsl:template>
	</xsl:stylesheet>
</xml>

<SCRIPT language="JavaScript">
function loadSource(sourceObj){
    var xmlDoc=new ActiveXObject("Msxml2.FreeThreadedDOMDocument.5.0");
    xmlDoc.async=false;
    xmlDoc.load(sourceObj.XMLDocument);
    return xmlDoc;
    }

var table_proc=null;

function getProcessor(transformObj){
    if (table_proc==null){
        var xslDoc=new ActiveXObject("Msxml2.FreeThreadedDOMDocument.5.0");
        var xslTemplate=new ActiveXObject("Msxml2.XSLTemplate.5.0");
        xslDoc.async=false;
        xslDoc.load(transformObj.XMLDocument);
        xslTemplate.stylesheet=xslDoc;
        xslProcessor=xslTemplate.createProcessor();
        table_proc=xslProcessor;
        }
    else {
        xslProcessor=table_proc;
        }
    return xslProcessor;
    }
    
function transformData(srcDoc,processor){
    var resultDoc=new ActiveXObject("MSXML.DOMDocument");
    processor.input=srcDoc;
    processor.output=resultDoc;
    processor.transform();
    return resultDoc;
    }

function showGenre(genre){
    var srcDoc=loadSource(item_catalog);
    var processor=getProcessor(catalog_filter);
    //processor.addParameter("search",genre);
    var rsltDoc=transformData(srcDoc,processor);
    catalog_table.innerHTML=rsltDoc.xml
    }    

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}

function doSearch(){
	var srcDoc=loadSource(item_catalog);
	var processor=getProcessor(catalog_filter);
	var sku = trim(document.getElementById('txtSku').value);
	var desc = trim(document.getElementById('txtDesc').value);
	var uoi = document.getElementById('cboUOI').value;
	var origin = document.getElementById('cboOrigin').value;
	if ( sku == '' )
		sku = '*';
	if ( desc == '' )
		desc = '*';
	processor.addParameter("sku", sku);
	processor.addParameter("desc", desc);
	processor.addParameter("uoi", uoi);
	processor.addParameter("origin", origin);
	var rsltDoc = transformData(srcDoc,processor);
	catalog_table.innerHTML = rsltDoc.xml;
}

showGenre("all");    

</SCRIPT>

</BODY>
</HTML>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top