Best Way To Strip XML Document of Unwanted Nodes

E

Eddy C

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>
 
P

Peter Flynn

Eddy said:
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:eek:utput 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
 
D

Dimitre Novatchev

Eddy C said:
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:eek:utput 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top