Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 24th, 2008, 08:32 AM
Authorized User
 
Join Date: Jul 2008
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?

 
Old July 24th, 2008, 08:47 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
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 -/
 
Old July 24th, 2008, 09:11 AM
Authorized User
 
Join Date: Jul 2008
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.

 
Old July 24th, 2008, 09:27 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
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 -/
 
Old July 24th, 2008, 09:50 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
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
 
Old July 24th, 2008, 11:16 AM
Authorized User
 
Join Date: Jul 2008
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?






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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.