Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 24th, 2008, 08:32 AM
Authorized User
 
Join Date: Jul 2008
Location: , , .
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem when a code changed with call template

I already have a piece of code which is working fine

<xsl:for-each select="c:CourtAction">
  <xsl:for-each select="c:CourtCharge">
    <xsl:value-of select="i:ChargeDescriptionText"/>
     <xsl:if test="position()!=last()">,</xsl:if>
  </xsl:for-each>
</xsl:for-each>


For better readability i decided to gor for XSL:call-template. I wrote code ike this

<xsl:call-template name="datadisplay">
 <xsl:with-param name="field1" select="c:CourtAction"></xsl:with-param>
 <xsl:with-param name="field2" select="c:CourtCharge"></xsl:with-param>
 <xsl:with-param name="field3" select="i:ChargeDescriptionText"></xsl:with-param>
 <xsl:with-param name="field4" select="''"></xsl:with-param>
</xsl:call-template>


 and

<xsl:template name="datadisplay">
    <xsl:param name="node"></xsl:param>
    <xsl:param name="subnode"></xsl:param>
    <xsl:param name="element1"></xsl:param>
    <xsl:param name="element2"></xsl:param>
    <xsl:for-each select="$node">
      <xsl:for-each select="$subnode">
      <xsl:value-of select="$element1"/>
      <xsl:if test="position()!=last()">,</xsl:if>
    </xsl:for-each>
    </xsl:for-each>
</xsl:template>

but its throwing the error "Expression must evaluate to a node set. What could have gone wrong?

Reply With Quote
  #2 (permalink)  
Old July 24th, 2008, 08:47 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

What you want to accomplish can probably be done by substituting the above <xsl:for-each> loop as follows:

<xsl:apply-templates select="c:CourtAction"/>

and then defining the following template:

<xsl:template match="c:CourtCharge">
  <xsl:value-of select="i:ChargeDescriptionText"/><xsl:if test="position()!=last()">,</xsl:if>
</xsl:template>


/- Sam Judson : Wrox Technical Editor -/
Reply With Quote
  #3 (permalink)  
Old July 24th, 2008, 09:11 AM
Authorized User
 
Join Date: Jul 2008
Location: , , .
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thats realy good. But canu please point out what went wrong when i used call template.

Reply With Quote
  #4 (permalink)  
Old July 24th, 2008, 09:27 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Well in your working code above, each time you called xsl:for-each it changes the context object, so the inner xsl:for-each is referring to a children of the outer elements only.

In your xsl:call-template example however the context does not change within the parameters, so the second and third parameters would not select anything. Oh, and the fact that the "name" attribute between your parameters didn't match (i.e. you should have a matching xsl:with-param for each xsl:param).

/- Sam Judson : Wrox Technical Editor -/
Reply With Quote
  #5 (permalink)  
Old July 24th, 2008, 09:50 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Variables in XSLT don't hold fragments of XPath expressions, they hold values. So when you do this:

<xsl:with-param name="field1" select="c:CourtAction"></xsl:with-param>
 <xsl:with-param name="field2" select="c:CourtCharge"></xsl:with-param>
 <xsl:with-param name="field3" select="i:ChargeDescriptionText"></xsl:with-param>

the values of all three variables are sets of nodes; all the expressions are evaluated with the same context node. If the CourtCharge elements are children of the CourtAction elements, then select="c:CourtCharge" starting from this context node will select an empty set of nodes.

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
Reply With Quote
  #6 (permalink)  
Old July 24th, 2008, 11:16 AM
Authorized User
 
Join Date: Jul 2008
Location: , , .
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks fro the replies. I have one more problem when i am using applytemplate. The condition position()!=last() always evaluates false. Any reason for this and any work around?

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
anyway to call a template with a lower precedence? ojasrege XSLT 3 November 30th, 2007 12:13 PM
Using Parameter to call another template mikevn123 XSLT 1 October 27th, 2006 03:08 PM
how to use call-template result in a test? briforge XSLT 6 February 13th, 2006 06:54 PM
xsl:call-template name="$variable" problem chemi XSLT 1 October 6th, 2005 07:28 AM
import / call / template match Kabe XSLT 3 February 26th, 2004 11:31 AM



All times are GMT -4. The time now is 08:20 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.