This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: xsl string comparison fails why?


The problem you have here is unwanted whitespace around the state names:

<state>Alabama
  <statelink

Here there is a carriage return and some spaces after Alabama.  When you
pass in a parameter you only pass in 'Alabama', without any trailing
whitespace, so the two are not equal.

The simplest solution that fits your stylesheet is to translate() out
any whitespace from the text() content of the <state> element before it
gets compared to the parameter:

<xsl:for-each select="admissions/state/text()">
  <xsl:if test="translate(.,'&#x9;&#xA;&#xD;&#x20;','')=$state">
    <xsl:value-of select="."/>
  </xsl:if>
</xsl:for-each>

The way your data is structured is definitely not a good idea, hence
your current problem.  I would either place the state name as an
attribute on the element:

<state name="Alabama">...

Or as a child:

<state>
  <name>Alabama</name>

The former is best, as it fits what attributes are all about.

Mixed element content (imho) should only be used where presentation
markup doesnt interfere,

ok: the state of <state>Alabama</state> is...

bad: <state>Alabama
       <link>.....

cheers
andrew




> -----Original Message-----
> From: Geoff [mailto:ghank@lawlead.com]
> Sent: 25 September 2002 14:38
> To: xsl-list@lists.mulberrytech.com
> Subject: [xsl] xsl string comparison fails why?
> 
> 
> I am passing a parameter to an xslt template and trying to use that
> parameter in an if comparison but it evaluates to false when 
> it appears
> it should be true.
> 
> The parameter is taken from an html form select box as so:
> <select name='state'>
> 	<option value='' selected='yes'></option>
> 	<option value="Alabama">Alabama</option>
> 	<option value="Alaska">Alaska</option>
> 	<option value="Arizona">Arizona</option>
> 	<option value="Arkansas">Arkansas</option>
> </select>
> 
> Then the parameter is passed via php to the xslt template 
> like this (in
> case this is relevant but it probably isn't):
> <?php
> 	$xmlfile= $_POST['catID'] . '.xml';
> 	$xslfile= $_POST['catID'] . '.xsl';
> 	$args = array();
> 	$params["state"] = $_POST['state']; 
> 	// create XSLT processor 
> 	$xh = xslt_create();
> 
> 	// call xslt processor
> 	// Process the document
> 	$result = xslt_process($xh, $xmlfile, $xslfile, NULL, 
> $args, $params); 
> 	if ($result) {
> 		print $result;
> 	}
> 	
> 	xslt_free($xh);
> 
> 
> ?>
> 
> Then Given this xml:
> <?xml version="1.0" encoding="UTF-8"?>
> <admissions>
>  <state>Alabama
>   <statelink
>   href="http://www.alabar.org/page.cfm?view=3&amp;subgroup=main";>
>   Bar Admissions Information</statelink>
>   </state>
> 
>   <state>Alaska 
>   <statelink href="http://www.alaskabar.org/index.cfm?ID=5362";>
>   Admissions</statelink>
>   </state>
> 
>   <state>Arizona 
>   <statelink href="http://www.supreme.state.az.us/admis/";>
>   Admissions Unit of the Certification &amp; Licensing
>   Division</statelink>
>   </state>
> 	
>   </state>
> </admissions>
> 
> Then I am trying to find the value of the parameter (a state) 
> and print
> only that state to the browser> I am trying to do this like this:
> <?xml version="1.0" encoding="iso-8859-1" ?>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
> <xsl:output method="html" indent="yes"/>
> <xsl:param name="state" />
> <xsl:template match="/">
> <html>
> <head>
> <body bgcolor="#FFFFFF">
> 	<xsl:for-each select="admissions/state/text()">
> 		<xsl:if test=".=$state">
> 			<xsl:value-of select="."/>
> 		</xsl:if>
> 	</xsl:for-each>
> </body>
> </html>
> </xsl:template>
> </xsl:stylesheet>
> 
> The if never evalutes to true even if it is testing Alabama=Alabama.
> I can print the value of the parameter using <xsl:value-of
> select="$state"/> just fine
> 
> Any hints/suggestions?
> 
> 
> 
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
> 
> 
> 
> ---
> Incoming mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.391 / Virus Database: 222 - Release Date: 19/09/2002
>  
> 

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.391 / Virus Database: 222 - Release Date: 19/09/2002
 

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]