473,406 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Use XSL to remove "outdated" nodes?

Hi everyone,

Could someone reccomend a way to remove duplicate/outmoded nodes in an XML
document?

I didn't describe that well, so let me give an example.

For instance, if I have an XML doc like so:

<inventory>
<part>
<name="CPU"/>
<version="4"/>
</part>
<part>
<name="Hard Disk"/>
<version="3"/>
</part>
<part>
<name="CPU"/>
<version="5"/>
</part>
<part>
<name="Keyboard"/>
<version="3"/>
</part>
<part>
<name="Keyboard"/>
<version="3"/>
</part>
</inventory>

I'd like to get a final document with a single CPU element whose
version="5", a Hard Disk element of version="3", and *one* Keyboard element
of version "3". If you could give me a lead, I'd be grateful. Thanks.

-Jim

Jul 20 '05 #1
6 2398
"Dimitre Novatchev" <dn********@yahoo.com> wrote in message
news:bd*************@ID-152440.news.dfncis.de...
This is not well-formed XML.


Sorry, let's try that again-- just something I typed quickly into the news
posting:

<inventory>
<part>
<name>CPU</name>
<version>4</version>
</part>
<part>
<name>Hard Disk</name>
<version>"3"</version>
</part>
<part>
<name>CPU</name>
<version>5</version>
</part>
<part>
<name>Keyboard</name>
<version>"3"</version>
</part>
<part>
<name>Keyboard</name>
<version>3</version>
</part>
</inventory>

Jul 20 '05 #2
In my solution that uses attributes, just replace in the xsl:sort
instructions:

"@name" with "name"
and
"@version" with "version"

=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL
"Jim Bancroft" <bo*********@nospam.yahoo.com> wrote in message
news:Gs*****************@news.uswest.net...
"Dimitre Novatchev" <dn********@yahoo.com> wrote in message
news:bd*************@ID-152440.news.dfncis.de...
This is not well-formed XML.


Sorry, let's try that again-- just something I typed quickly into the news
posting:

<inventory>
<part>
<name>CPU</name>
<version>4</version>
</part>
<part>
<name>Hard Disk</name>
<version>"3"</version>
</part>
<part>
<name>CPU</name>
<version>5</version>
</part>
<part>
<name>Keyboard</name>
<version>"3"</version>
</part>
<part>
<name>Keyboard</name>
<version>3</version>
</part>
</inventory>

Jul 20 '05 #3
Hi Jim,

Assuming that your XML were well-formed and looked something like...

<inventory>
<part>
<name>CPU</name>
<version>4</version>
</part>
<part>
<name>Hard Disk</name>
<version>3</version>
</part>
<part>
<name>CPU</name>
<version>5</version>
</part>
<part>
<name>Keyboard</name>
<version>3</version>
</part>
<part>
<name>Keyboard</name>
<version>3</version>
</part>
</inventory>

Then something like (using Muenchian technique to find distinct)...

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>
<!-- key for finding distinct part names -->
<xsl:key name="kDistinctPartName" match="part" use="name"/>
<xsl:template match="inventory">
<inventory>
<xsl:apply-templates select="part[generate-id() =
generate-id(key('kDistinctPartName',name))]"/>
</inventory>
</xsl:template>

<!-- template for handling distinct part names -->
<xsl:template match="part">
<!-- copy only the highest version of this distinct part name -->
<xsl:copy-of select="key('kDistinctPartName',name)[not(version &lt;
key('kDistinctPartName',name)/version)][1]"/>
</xsl:template>
</xsl:stylesheet>
Hope this helps
Marrow
http://www.marrowsoft.com - home of Xselerator (XSLT IDE and debugger)
http://www.topxml.com/Xselerator

"Jim Bancroft" <bo*********@nospam.yahoo.com> wrote in message
news:g4*****************@news.uswest.net...
Hi everyone,

Could someone reccomend a way to remove duplicate/outmoded nodes in an XML
document?

I didn't describe that well, so let me give an example.

For instance, if I have an XML doc like so:

<inventory>
<part>
<name="CPU"/>
<version="4"/>
</part>
<part>
<name="Hard Disk"/>
<version="3"/>
</part>
<part>
<name="CPU"/>
<version="5"/>
</part>
<part>
<name="Keyboard"/>
<version="3"/>
</part>
<part>
<name="Keyboard"/>
<version="3"/>
</part>
</inventory>

I'd like to get a final document with a single CPU element whose
version="5", a Hard Disk element of version="3", and *one* Keyboard element of version "3". If you could give me a lead, I'd be grateful. Thanks.

-Jim

Jul 20 '05 #4
Dimitre, Marrow:

Thanks for the assistance-- appreciate it.

-Jim
Jul 20 '05 #5
Hi Jim,

Assuming that your XML were well-formed and looked something like...

<inventory>
<part>
<name>CPU</name>
<version>4</version>
</part>
<part>
<name>Hard Disk</name>
<version>3</version>
</part>
<part>
<name>CPU</name>
<version>5</version>
</part>
<part>
<name>Keyboard</name>
<version>3</version>
</part>
<part>
<name>Keyboard</name>
<version>3</version>
</part>
</inventory>

Then something like (using Muenchian technique to find distinct)...

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>
<!-- key for finding distinct part names -->
<xsl:key name="kDistinctPartName" match="part" use="name"/>
<xsl:template match="inventory">
<inventory>
<xsl:apply-templates select="part[generate-id() =
generate-id(key('kDistinctPartName',name))]"/>
</inventory>
</xsl:template>

<!-- template for handling distinct part names -->
<xsl:template match="part">
<!-- copy only the highest version of this distinct part name -->
<xsl:copy-of select="key('kDistinctPartName',name)[not(version &lt;
key('kDistinctPartName',name)/version)][1]"/>
</xsl:template>
</xsl:stylesheet>
Hope this helps
Marrow
http://www.marrowsoft.com - home of Xselerator (XSLT IDE and debugger)
http://www.topxml.com/Xselerator

"Jim Bancroft" <bo*********@nospam.yahoo.com> wrote in message
news:g4*****************@news.uswest.net...
Hi everyone,

Could someone reccomend a way to remove duplicate/outmoded nodes in an XML
document?

I didn't describe that well, so let me give an example.

For instance, if I have an XML doc like so:

<inventory>
<part>
<name="CPU"/>
<version="4"/>
</part>
<part>
<name="Hard Disk"/>
<version="3"/>
</part>
<part>
<name="CPU"/>
<version="5"/>
</part>
<part>
<name="Keyboard"/>
<version="3"/>
</part>
<part>
<name="Keyboard"/>
<version="3"/>
</part>
</inventory>

I'd like to get a final document with a single CPU element whose
version="5", a Hard Disk element of version="3", and *one* Keyboard element of version "3". If you could give me a lead, I'd be grateful. Thanks.

-Jim

Jul 20 '05 #6
Hi Jim,

Assuming that your XML were well-formed and looked something like...

<inventory>
<part>
<name>CPU</name>
<version>4</version>
</part>
<part>
<name>Hard Disk</name>
<version>3</version>
</part>
<part>
<name>CPU</name>
<version>5</version>
</part>
<part>
<name>Keyboard</name>
<version>3</version>
</part>
<part>
<name>Keyboard</name>
<version>3</version>
</part>
</inventory>

Then something like (using Muenchian technique to find distinct)...

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>
<!-- key for finding distinct part names -->
<xsl:key name="kDistinctPartName" match="part" use="name"/>
<xsl:template match="inventory">
<inventory>
<xsl:apply-templates select="part[generate-id() =
generate-id(key('kDistinctPartName',name))]"/>
</inventory>
</xsl:template>

<!-- template for handling distinct part names -->
<xsl:template match="part">
<!-- copy only the highest version of this distinct part name -->
<xsl:copy-of select="key('kDistinctPartName',name)[not(version &lt;
key('kDistinctPartName',name)/version)][1]"/>
</xsl:template>
</xsl:stylesheet>
Hope this helps
Marrow
http://www.marrowsoft.com - home of Xselerator (XSLT IDE and debugger)
http://www.topxml.com/Xselerator

"Jim Bancroft" <bo*********@nospam.yahoo.com> wrote in message
news:g4*****************@news.uswest.net...
Hi everyone,

Could someone reccomend a way to remove duplicate/outmoded nodes in an XML
document?

I didn't describe that well, so let me give an example.

For instance, if I have an XML doc like so:

<inventory>
<part>
<name="CPU"/>
<version="4"/>
</part>
<part>
<name="Hard Disk"/>
<version="3"/>
</part>
<part>
<name="CPU"/>
<version="5"/>
</part>
<part>
<name="Keyboard"/>
<version="3"/>
</part>
<part>
<name="Keyboard"/>
<version="3"/>
</part>
</inventory>

I'd like to get a final document with a single CPU element whose
version="5", a Hard Disk element of version="3", and *one* Keyboard element of version "3". If you could give me a lead, I'd be grateful. Thanks.

-Jim

Jul 20 '05 #7

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

Similar topics

10
by: Derek Basch | last post by:
Hello, I have a string like: string = "WHITE/CLARET/PINK/XL" which I need to alter to this format: string = "WHITE-CLARET-PINK/XL"
5
by: kelvin | last post by:
How do I use get data in a url sent from aform on an other page. All i want to do is take this data and display it on the page. thanking you in anticipation
23
by: Hostile17 | last post by:
I keep coming across people, online and in real life, who believe that to code single tags like <br> and <img> with trailing slashes, <br /> and <img /> is considered "best practice" and when...
10
by: Dave O'Hearn | last post by:
I want to open a file for both reading and writing, but when I do "ios::in|ios::out" with an fstream, it creates the file if it doesn't already exist. I don't like that last part. C's fopen has the...
41
by: AngleWyrm | last post by:
I have created a new container class, called the hat. It provides random selection of user objects, both with and without replacement, with non-uniform probabilities. Uniform probabilities are a...
1
by: Seraph | last post by:
I've never posted here before, but I have lurked for quite some time, and have been frustrated to my whits end on what to do about this. Since there is limited support in previous posts (and many...
7
by: spinoza1111 | last post by:
"segfault city" was an interchange wherein Richard Heathfield posted a complete lie: <heathfield> Richard Heathfield wrote: > This would be a straightforward test for numerics: > > int...
3
by: divya | last post by:
Hi, I have a table tblbwday with 2 fields Name and Birthday.I have written this script for displaying evryday names of the people on that day. <% set objConn...
6
by: Rolf Welskes | last post by:
Hello, if I have for example: <table style="width: 100%; height: 100%;" border="1"> <tr> <td style="width: 100px">k </td> <td style="width: 100px">k </td> </tr>
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
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
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...

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.