473,386 Members | 1,958 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,386 software developers and data experts.

Best Way To Strip XML Document of Unwanted Nodes

I'm trying to strip an XML document of unwanted nodes such that in the
example below I would keep all the parent nodes of a child node I
wanted and for other parents which do not have the child requested they
would be stripped.

I understand I could use XSLT but I'm doing this in java and was
wondering if there was a preferred approach out there.
Before
<a>
<b>
<c>fdfdsf</c>
</b>
<b>
<x>dffsd</x>
</b>
<b>
<x>dffsd</x>
<c>fdfdsf</c>
</b>
</a>

After requesting to keep child c
<a>
<b>
<c>fdfdsf</c>
</b>
<b>
<c>fdfdsf</c>
</b>
</a>

Oct 4 '05 #1
3 1413
Eddy C wrote:
I'm trying to strip an XML document of unwanted nodes such that in the
example below I would keep all the parent nodes of a child node I
wanted and for other parents which do not have the child requested they
would be stripped.

I understand I could use XSLT but I'm doing this in java and was
wondering if there was a preferred approach out there.
Before
<a>
<b>
<c>fdfdsf</c>
</b>
<b>
<x>dffsd</x>
</b>
<b>
<x>dffsd</x>
<c>fdfdsf</c>
</b>
</a>

After requesting to keep child c
<a>
<b>
<c>fdfdsf</c>
</b>
<b>
<c>fdfdsf</c>
</b>
</a>


XSLT is easier to use for this, IMHO:

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:output method="xml"/>

<xsl:param name="keep"/>

<xsl:template match="*">
<!-- check current element, descendants, and siblings -->
<xsl:variable name="matches">
<xsl:for-each select=".|descendant::*|../*">
<xsl:if test="name()=$keep">
<xsl:text>Y</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:if test="$matches!=''">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:if>
</xsl:template>

</xsl:stylesheet>

There's probably a more elegant way, but my brain stopped working an hour
ago.

///Peter

Oct 5 '05 #2

"Eddy C" <ed****@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
I'm trying to strip an XML document of unwanted nodes such that in the
example below I would keep all the parent nodes of a child node I
wanted and for other parents which do not have the child requested they
would be stripped.

I understand I could use XSLT but I'm doing this in java and was
wondering if there was a preferred approach out there.
Before
<a>
<b>
<c>fdfdsf</c>
</b>
<b>
<x>dffsd</x>
</b>
<b>
<x>dffsd</x>
<c>fdfdsf</c>
</b>
</a>

After requesting to keep child c
<a>
<b>
<c>fdfdsf</c>
</b>
<b>
<c>fdfdsf</c>
</b>
</a>

Use:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>

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

<xsl:template match="*[not(descendant-or-self::c)]"/>
</xsl:stylesheet>
Cheers,
Dimitre Novatchev
Oct 5 '05 #3
Thanks

Oct 5 '05 #4

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

Similar topics

2
by: Malcolm Dew-Jones | last post by:
I am looking at xslt 1.0 and trying to understand if empty text nodes are supposed to be stripped or not as the default behaviour. 3.4 starts by listing rules for when white space is not stripped...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
1
by: Pino Carafa | last post by:
I've designed an aspx page with a Tab Strip on it. The problem is that unlike some of the simpler controls like text boxes and buttons and the likes, this Tab Strip is rendered something like this:...
2
by: Brian | last post by:
Hi all, I have an xml document that can contain 248 nodes with each node containing different fields. There is the possibility that there will be more nodes. Each node can have up to 30...
4
by: David Beck | last post by:
I donwnload some files for processing every day that have unwanted characters in them. In VB6 I use the InputB to read in the text and the StrConv. vLinesFromFile =...
2
by: Daniel M. Hendricks | last post by:
I'm looking for a function/regex in C# to strip unwanted HTML tags from comments posted to my web site. Previously, it was written in PHP and I used this function to strip unwanted tags: ...
2
by: CI | last post by:
I have to create a new XML document from the exising one, by adding some additional tags, etc.. My question is as follows: how to strip XML comments from the existing document? The identity...
9
by: amattie | last post by:
Does anyone have any idea on how I can strip the extra whitespace in the XML that shows up when I receive a response from an ASP.NET 2.0 webservice? This has been discussed before, but no one has...
6
by: lawpoop | last post by:
Hello all - I have a situation where I want to remove a string from a file and write it to disk before I do further work with it. It's a malformed xlm document with a string that causes our...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...

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.