473,472 Members | 1,728 Online
Bytes | Software Development & Data Engineering Community
Create 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 2031

<ts*******@revasystems.comwrote in message
news:11**********************@11g2000cwr.googlegro ups.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:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>

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

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

<xsl:key name="kBreedByName" 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(@class = $pClassToDel)">
<xsl:call-template name="identity"/>
</xsl:if>
</xsl:template>

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

<xsl:template match="pet">
<xsl:if test=
"not(key('kBeastByType',
key('kBreedByName', @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:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
exclude-result-prefixes="xs"
>
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>

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

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

<xsl:key name="kDeleted" match="class"
use="string(@name = $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('kBreedByName',@breed)/@type
)
/@class
=
$pClassToDel)"/>

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

<xsl:key name="kBreedByName" 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
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...
9
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...
0
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...
10
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...
9
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...
5
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...
2
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...
7
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.