RE: [xsl] contains()???

Subject: RE: [xsl] contains()???
From: "Diamond, Jason" <Jason.Diamond@xxxxxxx>
Date: Tue, 15 May 2001 20:45:36 -0500
You're not too clear on what you're trying to achieve. Do you only want to
output the employees that have id attributes listed in the id parameter? If
so, there's a number of problems that I can see in your transform and
they're all contribute to the results your getting. I'll list them off as in
the order I noticed them.

Firstly, the first parameter to contains() is not what I think you think it
is. The innermost expression, emp[@id], is selecting all emp elements that
have an id attribute--not all the id attributes on the emp elements. You're
converting those elements into a string and testing to see if that string
contains the id. The string value of an emp element only contains an email
address and some whitespace. I think that you want to use emp/@id instead
which will select all of the id attributes on all of the emp elements.

Also, if $id can contain comma separated IDs then you probably want to
switch the order in which you pass in your parameters. The contains()
function only returns true when the string value of the first parameter
contains the string value of the second parameter. Since your example source
document has id attributes that contain only single IDs, it will never be
true that they will contain a string like "1002,1003,1004" but it could be
true the other way around.

More importantly, your xsl:if statements aren't even doing anything. They're
both empty. The instructions that you want to execute if a certain condition
is true need to be children of xsl:if. You have an xsl:apply-templates
between--not inside--two empty if statements so the apply-templates will
always happen.

And the final reason for why you're outputting everybody is that your
xsl:apply-templates isn't using a predicate. Without a predicate (the part
in square brackets), it will select all emp child elements of the current
node. It should probably be something like this: 

<xsl:apply-templates select="emp[contains($id, @id)]"/>

This will select all emp children who's id attribute is "contained" by the
$id parameter.

I've made this change and cut out some of the dead code (as far as I can
reason) and the stylesheet now looks like this:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
  <xsl:output method="html" indent="no"/>
  <xsl:param name="id" select="1003" />

  <xsl:template match="addresses">
    <xsl:apply-templates select="emp[contains($id, @id)]"/>
  </xsl:template>

  <xsl:template match="emp">
    <h1>Name: <xsl:value-of select="concat(details/@fname, ' ',
details/@lname)" /></h1>
    <p>Email: <a href="mailto:{email}";><xsl:value-of select="email"
/></a><br />
    Country: <xsl:value-of select="details/@country" /><br />
 Phone: <xsl:value-of select="details/@phone" />
    </p>
  </xsl:template>
</xsl:stylesheet>

Running this on your example without specifying a value for id results in
only Kimberly Chung being output. Is that what you expected?

Jason.

-----Original Message-----
From: Gitanjali [mailto:narsu@xxxxxxxxxxxxx]
Sent: Tuesday, May 15, 2001 5:59 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: [xsl] contains()???


All,

Could you please solve my problem.  Acutally I passed the parameter from the
html as id = "1002,1003,1004"

Now I want to display only those records where parameter id matches the
input .  The following xsl is not working...It's displaying all the results.

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
  <xsl:output method="html" indent="no" />
  <xsl:param name="id" select="1003" />

  <xsl:template match="addresses">
 <xsl:value-of select="$id"/>
 <xsl:if test="contains(string(emp[@id]),string($id))"> </xsl:if>
     <xsl:apply-templates select="emp"/>
 <xsl:if test="contains(string(emp[@id]),string($id))"> </xsl:if>
  </xsl:template>

  <xsl:template match="emp">
    <h1>Name: <xsl:value-of select="concat(details/@fname, ' ',
details/@lname)" /></h1>
    <p>Email: <a href="mailto:{email}";><xsl:value-of select="email"
/></a><br />
    Country: <xsl:value-of select="details/@country" /><br />
 Phone: <xsl:value-of select="details/@phone" />
    </p>
  </xsl:template>
</xsl:stylesheet>


My xml file is :

<addresses>
 <emp registered="20010125" id="1001">
  <email>scott@xxxxxxxxxx</email>
  <details fname="Scott" lname="Wilson" country="UK" phone="1800callme"/>
 </emp>
 <emp registered="20010125" id="1002">
  <email>john@xxxxxxxx</email>
  <details fname="John" lname="Kerr" country="USA" phone="1800callJohn"/>
 </emp>
 <emp registered="20010125" id="1003">
  <email>kim@xxxxxxxxx</email>
  <details fname="Kimberly" lname="Chung" country="USA"
phone="1800callKim"/>
 </emp>
 <emp registered="20010125" id="1004">
  <email>Tim@xxxxxxxxxx</email>
  <details fname="Timmothy" lname="Kelley" country="USA"
phone="1800callTim"/>
 </emp>
 <emp registered="20010125" id="1005">
  <email>Marie@xxxxxxxxxxxx</email>
  <details fname="Marie" lname="Whitaker" country="AU"
phone="1800callMarie"/>
 </emp>
</addresses>



Thanks
Narsu


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

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


Current Thread