 |
| 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
|
|
|
|

February 12th, 2007, 04:43 PM
|
|
Authorized User
|
|
Join Date: Feb 2007
Posts: 51
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
Problem Copying Nodes
Hello,
I am attempting to make a local copy of a node (including its siblings and descendants) so as to preserve the original for other processing needs. If I do the following:
<xsl:call-template name="actorTotal">
<xsl:with-param name="node" select="ownedMember"/>
<xsl:with-param name="actorCount" select="0"/>
</xsl:call-template>
where "ownedMember is the source node-set, then in the named template:
<xsl:template name="actorTotal">
<xsl:param name="node"/>
<xsl:param name="actorCount"/>
</xsl:template>
the node named "node" turns out to be a single node, without the corresponding sibs and descendants. I cannot figure out how to copy the entire not-set. I have the general idea that "xsl:copy-of" would be useful to me, but I cannot figure out the correct syntax for copying the entire node-set. Thanks in advance for any assistance.
|
|

February 12th, 2007, 04:49 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
I can't really see why you need to make a copy of the nodes, since trees in XSLT are immutable (even if you use xsl:copy-of, there's a risk that the XSLT processor won't make a physical copy, because it's allowed to assume that the original nodes aren't going to change). However, what you are doing is simply passing a reference to the node at the top of the subtree.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

February 12th, 2007, 05:15 PM
|
|
Authorized User
|
|
Join Date: Feb 2007
Posts: 51
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
The problem is that I need to recurse the node-set in one named template, and then recurse the same node-set a second for another named template. What do you suggest I do instead of copying? I am assuming you think I should recurse using the input node set. If this is the case, what is the best way to get back to my original context before recursing the node-set again in the second named template?
thank you
|
|

February 12th, 2007, 05:53 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
I'm having difficulty understanding your difficulty. When you return from a named template, the context is exactly what it was before you called that template.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

February 12th, 2007, 05:58 PM
|
|
Authorized User
|
|
Join Date: Feb 2007
Posts: 51
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
Sorry for being dense.. How then do I pass the entire sub-tree to the named template, rather than the reference to the single node?
|
|

February 12th, 2007, 06:06 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
You don't. You pass a reference to a single node, and the called template then uses XPath expressions to navigate from that node to any other nodes it is interested in.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

February 12th, 2007, 06:08 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
(Incidentally, I don't think you're being dense at all. You're having exactly the same kind of conceptual hurdles that I always encounter when learning a new technology, because you're asking penetrating questions about it rather than just blindly copying specimen code like so many people do.)
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

February 12th, 2007, 06:20 PM
|
|
Authorized User
|
|
Join Date: Feb 2007
Posts: 51
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
thanks for your patient words.. ok, so I comprehend how the node reference I am passing has all I need to recurse the sub-tree, provided I use the appropriate XPath syntax. So, along those lines, I essentially have a recursive template as follows:
<xsl:template name="actorTotal">
<xsl:param name="node"/>
<xsl:param name="actorCount"/>
<xsl:choose>
<xsl:when test="@xsi:type='uml:Actor'">
<xsl:call-template name="actorTotal">
<xsl:with-param name="node" select="$node[position() + 1]"/>
<xsl:with-param name="actorCount" select="$actorCount + 1"/>
... (more stuff here)
I would have expected (given my limited XSLT experience) that $node[position() + 1] would have passed a reference to the next node in the list, but it doesn't work. Would you be able to tell me what I am doing wrong? I have been struggling with this most of the day now
thank you
|
|

February 12th, 2007, 06:54 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
>$node[position() + 1]
Within square brackets the context changes, so position() inside the brackets doesn't give the same result as outside. In fact $node[x] where x is a number means $node[position()=x], so you are asking for $node[position() = position()+1], which selects nothing.
>the next node in the list
what do you mean by "the list"? If you want the node that is the next child of the same parent node, use $node/following-sibling::*[1].
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

February 12th, 2007, 07:10 PM
|
|
Authorized User
|
|
Join Date: Feb 2007
Posts: 51
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
thank you so much for your generous time and patience. However, it still is not working. Here is the exact syntax as I understood you to have communicated to me:
<xsl:call-template name="actorTotal">
<xsl:with-param name="node" select="$node/following-sibling::*[1]"/>
...more stuff..
any other ideas what I might be doing wrong?? thanks so much!
|
|
 |