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 | 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 May 24th, 2013, 06:29 AM
Authorized User
Points: 106, Level: 2
Points: 106, Level: 2 Points: 106, Level: 2 Points: 106, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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!!!
Reply With Quote
  #2 (permalink)  
Old May 24th, 2013, 06:39 AM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
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
Reply With Quote
The Following User Says Thank You to Martin Honnen For This Useful Post:
EastvanAxon (May 24th, 2013)
  #3 (permalink)  
Old May 24th, 2013, 06:47 AM
Authorized User
Points: 106, Level: 2
Points: 106, Level: 2 Points: 106, Level: 2 Points: 106, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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)?
Reply With Quote
  #4 (permalink)  
Old May 24th, 2013, 07:08 AM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
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
Reply With Quote
The Following User Says Thank You to Martin Honnen For This Useful Post:
EastvanAxon (May 24th, 2013)
  #5 (permalink)  
Old May 24th, 2013, 08:17 AM
Authorized User
Points: 106, Level: 2
Points: 106, Level: 2 Points: 106, Level: 2 Points: 106, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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
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
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



All times are GMT -4. The time now is 08:35 AM.


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