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 May 24th, 2013, 06:29 AM
Authorized User
 
Join Date: Mar 2012
Posts: 25
Thanks: 9
Thanked 0 Times in 0 Posts
Default Presence of a node in a node set

Dear all,

Once again I need your help
I'm trying to replace a node N with a node N1, only if N is deep-equal() to a node stored into a variable V. The tricky part is that if such a node N does not exist, I have to add the node V to the original set of nodes, i.e.

Code:
<list>
  <node1>
  <node2>
  <node3>
</list>
has to become

Code:
<list>
  <v>
  <node2>
  <node3>
</list>
if "deep-equal($node1, $v) = true()"

or

Code:
<list>
  <node1>
  <v>
  <node3>
</list>
if "deep-equal($node2, $v) = true()"

or

Code:
<list>
  <node1>
  <node2>
  <v>
</list>
if "deep-equal($node3, $v) = true()"

or, finally,

Code:
<list>
  <node1>
  <node2>
  <node3>
  <v>
</list>
if no node in ($node1, $node2, $node3) is deep-equal to $v
[suppose that $node{1,2,3} are variables storing the nodes with the same name() of the variable, and that <node{1,2,3}> are nodes with children]


I tried different approaches...using index-of was the one more promising because I could use a variable to store the sequence of nodes under <list> and then check index-of($list-of-nodes, $v)...sadly this function does not use deep-equal to check the equality of $v and the items in $list-of-nodes, thus it doesn't work for my case, where I might have two nodes with same name() but different attributes or other differences in their children that I must consider

Do you have any suggestion?

Thanks in advance for your help!!!
 
Old May 24th, 2013, 06:39 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Write a template
Code:
<xsl:template match="list[not(*[deep-equal(., $v)])]">
  <xsl:copy>
     <xsl:copy-of select="node(), $v"/>
  </xsl:copy>
</xsl:template>
together of course with the identity transformation template.

I don't think you need a template to replace node1 or node2 or node3 if they are deep-equal to the node in the variable as that would not change the result.

If you think you need to then use
Code:
<xsl:template match="list/node1[deep-equal(., $v)] | list/node2[deep-equal(., $v)] | list/node3[deep-equal(., $v)]">
  <xsl:copy-of select="$v"/>
</xsl:template>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
The Following User Says Thank You to Martin Honnen For This Useful Post:
EastvanAxon (May 24th, 2013)
 
Old May 24th, 2013, 06:47 AM
Authorized User
 
Join Date: Mar 2012
Posts: 25
Thanks: 9
Thanked 0 Times in 0 Posts
Default

Thanks for your reply Martin,

I don't understand how the template is able to add the node in $v just one time, when no list/* are deep-equal to $v.

because your first template seems to add $v for every node that is not deep-equal to $v, or am I wrong?

Can't I use the check you put in the match attribute of that first template to check if I have to add $v, and thus use a <choose> to split the two cases (where there is a node deep-equal and where there isn't one)?
 
Old May 24th, 2013, 07:08 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

The first template I posted matches on
list[not(*[deep-equal(., $v)])]
so it looks at the "list" elements not containing a single child element that is deep-equal to the variable "v".
It does a shallow copy of the "list" element and inside a deep copy of all child nodes and of a single instance of the variable "v" so I don't see why it would add "v" several times.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
The Following User Says Thank You to Martin Honnen For This Useful Post:
EastvanAxon (May 24th, 2013)
 
Old May 24th, 2013, 08:17 AM
Authorized User
 
Join Date: Mar 2012
Posts: 25
Thanks: 9
Thanked 0 Times in 0 Posts
Default

Ah ok, thx.

BTW I got confused, the check is with another node stored into the variable $n and not $v, because $v stores the node that has to be used for replacement

I solved the problem without using the template. I used a choose and checked when "count(list/*[deep-equal(., $n1)])=0". If that is true I use for-each "list/*" and do copy-of "." and finally "$v". Otherwise I used another for-each with another choose (to check whenever deep-equal(., $n1) is true and replace "." with $v)

I know that this is not probably the best solution, but I tested it and it's working

Thanks a lot for your help, that matching condition was of inspiration





Similar Threads
Thread Thread Starter Forum Replies Last Post
Full Comparison of current node and a node stored in a variable EastvanAxon XSLT 2 December 19th, 2012 12:01 PM
Adding a node to an existing xml node list. codehelp C# 2008 aka C# 3.0 2 October 12th, 2009 07:41 AM
Displaying data as a Parent Node with Left Node and Right Node Manoj Bisht Visual Basic 2008 Professionals 0 April 2nd, 2009 02:34 AM
XPath: set operation with a disjoint node set rich_unger XSLT 7 May 6th, 2008 09:24 AM
Copying Source Node attributes to output node pvsat XSLT 2 November 3rd, 2005 09:46 AM





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