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 February 12th, 2007, 07:14 PM
Authorized User
 
Join Date: Feb 2007
Posts: 51
Thanks: 3
Thanked 0 Times in 0 Posts
Default

and yes, the node that is the next child of the same parent node is indeed what I am looking for. Even with your corrected syntax, it continues to only reference the original node from the first call to the template...

 
Old February 12th, 2007, 07:18 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Well, there's nothing intrinsically wrong with that code snippet; to see what you are doing wrong I need to stand back and see what problem you are trying to solve and what the overall structure of your stylesheet looks like.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old February 12th, 2007, 11:13 PM
Authorized User
 
Join Date: Feb 2007
Posts: 51
Thanks: 3
Thanked 0 Times in 0 Posts
Default

understood.. Here is some better context.

Here is the original calling portion:

<xsl:call-template name="actorTotal">
<xsl:with-param name="node" select="ownedMember"/>
<xsl:with-param name="actorCount" select="0"/>
<xsl:with-param name="memberCount" select="count(ownedMember)"/>
</xsl:call-template>

and here is the named template in its entirety:

<xsl:template name="actorTotal">
<xsl:param name="node"/>
<xsl:param name="actorCount"/>
<xsl:param name="memberCount"/>

<xsl:if test="$memberCount !=0">
<xsl:choose>
<xsl:when test="@xsi:type='uml:Actor'">
<xsl:call-template name="actorTotal">
<xsl:with-param name="node" select="$node/following-sibling::*[1]"/>
<xsl:with-param name="actorCount" select="$actorCount + 1"/>
<xsl:with-param name="memberCount" select="$memberCount - 1"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="actorTotal">
<xsl:with-param name="node" select="$node/following-sibling::*[1]"/>
<xsl:with-param name="actorCount" select="$actorCount"/>
<xsl:with-param name="memberCount" select="$memberCount - 1"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:if>

<xsl:if test="$actorCount > 0">
Total Actors: <xsl:value-of select="$actorCount"/>
</xsl:if>
</xsl:template>


I am hopeful this additional context might be useful in your assisting in finding my tenacious little flaw. Thanks so much again!

 
Old February 13th, 2007, 04:55 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Well, I'm still working a bit in the dark because I don't know what your input looks like, I don't know what output you are trying to produce, and I don't know what the context is at the entry point. But let's try anyway...

<xsl:call-template name="actorTotal">
<xsl:with-param name="node" select="ownedMember"/>
<xsl:with-param name="actorCount" select="0"/>
<xsl:with-param name="memberCount" select="count(ownedMember)"/>
</xsl:call-template>

From this we know that "node" is a set of zero or more nodes, which are siblings to each other: your choice of variable name suggests that there is exactly one node, but on the other hand the presence of memberCount suggests there might be several.

Now we see:

<xsl:if test="$memberCount !=0">
<xsl:choose>
<xsl:when test="@xsi:type='uml:Actor'">
<xsl:call-template name="actorTotal">
<xsl:with-param name="node" select="$node/following-sibling::*[1]"/>

Firstly, referring to @xsi:type accesses an attribute of the context node, which is passed to named templates as an implicit parameter. Did you intend this? I've no idea what the context node is, so I can't tell, but I suspect you meant $node/@xsi:type.

Secondly, if $node can contain several nodes, then "$node/following-sibling::*[1]" is going to select several nodes, which doesn't seem like a good idea.

Anyway, your code looks like an attempt at a classic head-tail recursion (of which there are plenty of examples in my book, incidentally). When the sequence you are processing using head-tail recursion is a sequence of siblings, you have two choices: you can pass a single node $node, and then use $node to refer to the head and use $node/following-sibling::* to access the tail of the sequence, or you can pass the whole sequence as $nodes, in which case you must use $nodes[1] to access the head and $nodes[position()!=1] to refer to the tail. You seem to be mixing the two approaches: you are passing the whole sequence, but then you are referring to $node as if you had only passed a single node.

Finally, from what I can see, your code is simply trying to count the number of nodes with @xsi:type='uml:actor'. If that's all you need to do, then you're making it far too difficult: it's just select="count(ownedMember[@xsi:type='uml:actor'])".

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old February 13th, 2007, 01:13 PM
Authorized User
 
Join Date: Feb 2007
Posts: 51
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Michael,

Yes indeed I was making it more difficult than I had too. It all works fine now that I have eviscerated my overly-complex code, and replaced with simpler XPath expressions, whose power I was not taking advantage of. I also have your book already and have referenced it (obviously not well enough so far..) on this current project I am undertaking. Thanks as usual, and have a great afternoon (evening?) where you are. Tim






Similar Threads
Thread Thread Starter Forum Replies Last Post
problem accessing nodes using document() and id() siris phi XSLT 3 April 12th, 2007 11:05 PM
Nodes ....'For Each' problem. Neal XSLT 3 February 13th, 2006 08:57 AM
Problem copying XML nodes francislang XSLT 9 October 21st, 2005 10:37 AM
Copying XML nodes from one document to another hughcr C# 2 May 12th, 2005 01:20 AM
reorder nodes problem pompluck XSLT 4 November 5th, 2003 09:02 AM





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