Best Way To Strip XML Document of Unwanted Nodes

Discussion in 'XML' started by Eddy C, Oct 5, 2005.

  1. Eddy C

    Eddy C Guest

    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>
    Eddy C, Oct 5, 2005
    #1
    1. Advertising

  2. Eddy C

    Peter Flynn Guest

    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: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
    Peter Flynn, Oct 5, 2005
    #2
    1. Advertising

  3. "Eddy C" <> wrote in message
    news:...
    > 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
    Dimitre Novatchev, Oct 5, 2005
    #3
  4. Eddy C

    Eddy C Guest

    Thanks
    Eddy C, Oct 5, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. gavnosis
    Replies:
    0
    Views:
    500
    gavnosis
    Aug 2, 2003
  2. Tony Prichard
    Replies:
    0
    Views:
    715
    Tony Prichard
    Dec 12, 2003
  3. th3dude
    Replies:
    0
    Views:
    424
    th3dude
    Jan 3, 2007
  4. Aquila
    Replies:
    35
    Views:
    438
    Mathieu Bouchard
    Mar 31, 2005
  5. yelipolok
    Replies:
    4
    Views:
    241
    John W. Krahn
    Jan 27, 2010
Loading...

Share This Page