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 August 18th, 2008, 07:17 PM
Authorized User
 
Join Date: Aug 2008
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
Default Removal of same tiered nodes, based on text

Given:

<Event>
  <EventType>Name1</EventType>
  <Stuff>stuff</Stuff>
  <ImageName>imageName1</ImageName>
</Event>

<Event>
  <EventType>Name2</EventType>
  <Destination>place</Destination>
  <ImageName>imageName1</ImageName>
  <ImageName>imageName2</ImageName>
  <imageName>imageName3</ImageName>
</Event>

End Result:

<Event>
  <EventType>Name1</EventType>
  <Stuff>stuff</Stuff>
  <ImageName>imageName1</ImageName>
</Event>

<Event>
  <EventType>Name2</EventType>
  <Destination>place</Destination>
</Event>

Essentially, I want to remove all of the <ImageName> nodes in the second <Event> set. I am thinking that this can be done based on the text that is in the second sets <EventType> field (Name2 in this example). However, I am not sure how to check another node's text and remove (or not copy) the nodes that are in the same tier.
Attempt:

  <xsl:template match="/Event/ImageName">
    <xsl:if test="parent::node()/EventType[./text() = Name2]">
    </xsl:if>
  </xsl:template>

I've also put the <xsl:apply-templates /> in between the if statements. I have also done a number of other attempts, almost all I cannot remember. In the end, none of my attempts remove the <ImageName> tags from the second <Event> set. OR, one of my attempts seemed to remove everything from teh XML during transform.

Any ideas? Thank you.


 
Old August 19th, 2008, 03:57 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You simply need the identity template to copy everything:

<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

And then a simple template to match the elements you don't want to copy:

<xsl:template match="/Event[EventType='Name2']/ImageName"></xsl:template>

I think you had the right idea - but don't forget if you are matching a string you need to put it in quotes.

/- Sam Judson : Wrox Technical Editor -/
 
Old August 19th, 2008, 12:43 PM
Authorized User
 
Join Date: Aug 2008
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
Default

That worked! Thank you.

Well, it's obvious that I have a lot to learn about XSLT. But I only started 2 weeks ago. Appreciate it!







Similar Threads
Thread Thread Starter Forum Replies Last Post
Removal of series of similarly named nodes overcast XSLT 4 August 18th, 2008 07:03 PM
text and child nodes eepyoga XSLT 1 April 8th, 2008 12:45 PM
Count nodes based of if condition suri_1811 XSLT 1 December 7th, 2006 08:00 PM
XPath - Selecting nodes based on attribute values billy_bob_the_3rd XML 4 December 1st, 2004 06:12 PM
Two tiered collections (user-defined) sholom VB How-To 0 November 26th, 2003 02:51 PM





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