473,785 Members | 3,137 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

cascading deletion/copy in xsl

I have an xml document in which elements are hierarchically related to
eachother conceptually. Unfortunately, the hierarchical relationship
is not modelled in the schema (i.e., the elements are "flattened" in
the document". I have a case in which I want to remove a high level
element using xslt and want all the related lower-level elements to be
removed as well. Is there an easy way to do this in a template?

For example, imagine a relationship of elements describing animals
starting with "animal" at the top of the hierarchy and ending in "pets"
at the bottom:

animal =class =beastie =breed =pet

Given the xml below, say I wanted to remove the class named "Aves" and
wanted to cause a cascading removal of all other elements related to
"Aves". Is there a simple way to do this using xsl that helps me avoid
writing specific templates for each child node of "animals"?

<?xml version="1.0" encoding="utf-8"?>
<animals>

<classes>
<class name="Mammalia"/>
<class name="Aves"/>
</classes>

<beasties>
<beast type="cat" class="Mammalia "/>
<beast type="dog" class="Mammalia "/>
<beast type="bird" class="Aves"/>
</beasties>

<breeds>
<breed name="collie" type="dog"/>
<breed name="beagle" type="dog"/>
<breed name="persian" type="cat"/>
<breed name="siamese" type="cat"/>
<breed name="parakeet" type="bird"/>
<breed name="crow" type="bird"/>
</breeds>

<pets>
<pet name="rover" breed="collie"/>
<pet name="fluffy" breed="persian"/>
<pet name="tweety" breed="parakeet "/>
</pets>

</animals>

The desired xml output would be this:

<?xml version="1.0" encoding="utf-8"?>
<animals>

<classes>
<class name="Mammalia"/>
</classes>

<beasties>
<beast type="cat" class="Mammalia "/>
<beast type="dog" class="Mammalia "/>
</beasties>

<breeds>
<breed name="collie" type="dog"/>
<breed name="beagle" type="dog"/>
<breed name="persian" type="cat"/>
<breed name="siamese" type="cat"/>
</breeds>

<pets>
<pet name="rover" breed="collie"/>
<pet name="fluffy" breed="persian"/>
</pets>

</animals>

Jan 4 '07 #1
2 2049

<ts*******@reva systems.comwrot e in message
news:11******** **************@ 11g2000cwr.goog legroups.com...
>I have an xml document in which elements are hierarchically related to
eachother conceptually. Unfortunately, the hierarchical relationship
is not modelled in the schema (i.e., the elements are "flattened" in
the document". I have a case in which I want to remove a high level
element using xslt and want all the related lower-level elements to be
removed as well. Is there an easy way to do this in a template?

For example, imagine a relationship of elements describing animals
starting with "animal" at the top of the hierarchy and ending in "pets"
at the bottom:

animal =class =beastie =breed =pet

Given the xml below, say I wanted to remove the class named "Aves" and
wanted to cause a cascading removal of all other elements related to
"Aves". Is there a simple way to do this using xsl that helps me avoid
writing specific templates for each child node of "animals"?

<?xml version="1.0" encoding="utf-8"?>
<animals>

<classes>
<class name="Mammalia"/>
<class name="Aves"/>
</classes>

<beasties>
<beast type="cat" class="Mammalia "/>
<beast type="dog" class="Mammalia "/>
<beast type="bird" class="Aves"/>
</beasties>

<breeds>
<breed name="collie" type="dog"/>
<breed name="beagle" type="dog"/>
<breed name="persian" type="cat"/>
<breed name="siamese" type="cat"/>
<breed name="parakeet" type="bird"/>
<breed name="crow" type="bird"/>
</breeds>

<pets>
<pet name="rover" breed="collie"/>
<pet name="fluffy" breed="persian"/>
<pet name="tweety" breed="parakeet "/>
</pets>

</animals>

The desired xml output would be this:

<?xml version="1.0" encoding="utf-8"?>
<animals>

<classes>
<class name="Mammalia"/>
</classes>

<beasties>
<beast type="cat" class="Mammalia "/>
<beast type="dog" class="Mammalia "/>
</beasties>

<breeds>
<breed name="collie" type="dog"/>
<breed name="beagle" type="dog"/>
<breed name="persian" type="cat"/>
<breed name="siamese" type="cat"/>
</breeds>

<pets>
<pet name="rover" breed="collie"/>
<pet name="fluffy" breed="persian"/>
</pets>

</animals>

XSLT 1.0 solution (49 lines):

<xsl:styleshe et version="1.0"
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="ye s" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:param name="pClassToD el" select="'Aves'"/>

<xsl:key name="kBeastByT ype" match="beast"
use="@type"/>

<xsl:key name="kBreedByN ame" match="breed"
use="@name"/>

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

<xsl:template match="class">
<xsl:if test="not(@name = $pClassToDel)">
<xsl:call-template name="identity"/>
</xsl:if>
</xsl:template>

<xsl:template match="beast">
<xsl:if test="not(@clas s = $pClassToDel)">
<xsl:call-template name="identity"/>
</xsl:if>
</xsl:template>

<xsl:template match="breed">
<xsl:if test=
"not(key('kBeas tByType', @type)/@class = $pClassToDel)">
<xsl:call-template name="identity"/>
</xsl:if>
</xsl:template>

<xsl:template match="pet">
<xsl:if test=
"not(key('kBeas tByType',
key('kBreedByNa me', @breed)/@type
)/@class
=
$pClassToDel)">
<xsl:call-template name="identity"/>
</xsl:if>
</xsl:template>

</xsl:stylesheet>

Output:

<animals>
<classes>
<class name="Mammalia" />
</classes>
<beasties>
<beast type="cat" class="Mammalia " />
<beast type="dog" class="Mammalia " />
</beasties>
<breeds>
<breed name="collie" type="dog" />
<breed name="beagle" type="dog" />
<breed name="persian" type="cat" />
<breed name="siamese" type="cat" />
</breeds>
<pets>
<pet name="rover" breed="collie" />
<pet name="fluffy" breed="persian" />
</pets>
</animals>

XSLT 2.0 solution (40 lines):

<xsl:styleshe et version="2.0"
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
exclude-result-prefixes="xs"
>
<xsl:output omit-xml-declaration="ye s" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:param name="pClassToD el" select="'Aves'"/>

<xsl:key name="kDeleted" match="beast"
use="string(@cl ass = $pClassToDel)"/>

<xsl:key name="kDeleted" match="class"
use="string(@na me = $pClassToDel)"/>

<xsl:key name="kDeleted" match="breed"
use="string(key ('kBeastByType' ,@type)/@class = $pClassToDel)"/>

<xsl:key name="kDeleted" match="pet"
use="string(key ('kBeastByType' ,
key('kBreedByNa me',@breed)/@type
)
/@class
=
$pClassToDel)"/>

<xsl:key name="kBeastByT ype" match="beast"
use="@type"/>

<xsl:key name="kBreedByN ame" match="breed"
use="@name"/>

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

<xsl:template match="*[. intersect key('kDeleted', 'true')]"/>
</xsl:stylesheet>

Output:
<animals>
<classes>
<class name="Mammalia"/>
</classes>
<beasties>
<beast type="cat" class="Mammalia "/>
<beast type="dog" class="Mammalia "/>
</beasties>
<breeds>
<breed name="collie" type="dog"/>
<breed name="beagle" type="dog"/>
<breed name="persian" type="cat"/>
<breed name="siamese" type="cat"/>
</breeds>
<pets>
<pet name="rover" breed="collie"/>
<pet name="fluffy" breed="persian"/>
</pets>
</animals>

Hope this helped.

Cheers,
Dimitre Novatchev
Jan 5 '07 #2
Wonderful! Thank you very much!

Jan 5 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2049
by: pp | last post by:
What would you say about a smart script that would transform really 'cascading' style sheet into appropriate CSS structures. When coding css by hand, most often you are forced to write the statements separately in lines, e.g. --- A {css-properties:A; } A B C {css-properties:C;} A B D {css-properties:D; } A B D:hover {css-properties:Dhover; }
9
3431
by: (Pete Cresswell) | last post by:
Seems like when there's a 1:1 relationship, the order of referential integrity enforcement depends on which way you drag the mouse pointer when drawing the relationship line. If you drag from tblObject to tblCollection, a tblObject record must exist before a tblCollection record can be created and if a tblObject record is deleted, a like-keyed rec in tblCollection will get the cascade delete. OTOH, if a record in tblCollection is...
0
1643
by: Frnak McKenney | last post by:
One part of a customer project I'm working on involves what seem like fairly straightforward updates to a set of related tables. While I've developed software for a number of years (it only seems like centuries on days like this <grin>), I'm new to C# and ADO.NET and I'm running into problems with record deletions. I (think I am) applying ForeignKeyConstraints correctly. I'm not applying DataRelations (yet), but those _appear_ to be...
10
1462
by: Radium | last post by:
Cascading Style Sheet is hazardous to your privacy. It allows others on the internet to see your monitor and files. It allows them to copy images on your monitor to their computers. It also allows them to copy files from your computer to their's. It is dangerous. Avoid at all costs. CSS that isn't stored in the victim's computer. Instead it is stored in the perpetrator's PC. What it does is it reads everything on the victim's screen...
9
2031
by: Radium | last post by:
Cascading Style Sheet is an extreme hazard to your privacy. It allows others on the internet to see your monitor and files. It allows them to copy images on your monitor to their computers. It also allows them to copy files from your computer to their's. It is dangerous. Avoid at all costs. CSS that isn't stored in the victim's computer. Instead it is stored in the perpetrator's PC. What it does is it reads everything on the victim's...
5
1578
by: Radium | last post by:
Cascading Style Sheet is such a hazard to your privacy. It allows others on the internet to see your monitor and files. It allows them to copy images on your monitor to their computers. It also allows them to copy files from your computer to their computers. It is dangerous. Avoid at all costs. CSS isn't stored in the victim's computer. Instead it is stored in the perpetrator's computer. What it does is it reads everything on the...
2
2286
by: SPOILED36 | last post by:
I am building a database to track attendance. I have one main form with multiple subforms. Within one of the subforms name sfrDailyAttendance, I also have cascading combo boxes (cboCategory and cboException) to filter difference absences (ie.Regular, Overtime, etc). cboCategory is filtered as follows: !!... Because I have the subform set up as a continuous form, I needed to add a text box (txtException) on top of one of the combo boxes...
7
1815
by: Green Xenon [Radium] | last post by:
Cascading Style Sheet is such a hazard to your privacy. It allows others on the internet to see your monitor and files. It allows them to copy images on your monitor to their computers. It also allows them to copy files from your computer to their computers. It is dangerous. Avoid at all costs. CSS isn't stored in the victim's computer. Instead it is stored in the perpetrator's computer. What it does is it reads everything on the...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10085
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5379
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4045
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.