Removing child nodes via XSLT or Xpath - newbie question

Discussion in 'XML' started by jhoge123@yahoo.com, Oct 3, 2005.

  1. Guest

    I have an XML document that contains more information than I need for a
    particular application. The structure is as follows:

    <root>
    <category ID = "1">
    <product ID="1" />
    <product ID="2" />
    </category>
    <category ID = "2">
    <category id ="3">
    <product ID ="3"/>
    </category>
    <category id ="4">
    <product ID ="4"/>
    </category>
    </category>
    </root>

    What I want to do is populate a menu with all category nodes, but
    remove all of the product nodes. I tried using "/root/category" as an
    xpath, but that selects teh entire category node, along with the
    product child nodes that I don't want.

    Do I need to use XSLT first to remove the unwanted nodes?

    Any help would be greatly appreciated.

    Thanks,
    John
     
    , Oct 3, 2005
    #1
    1. Advertising

  2. wrote:


    > What I want to do is populate a menu with all category nodes, but
    > remove all of the product nodes. I tried using "/root/category" as an
    > xpath, but that selects teh entire category node, along with the
    > product child nodes that I don't want.
    >
    > Do I need to use XSLT first to remove the unwanted nodes?


    XPath indeed does not change the structure at all, it simply selects a
    node set in the original document and you can't exclude any child nodes
    of the selected nodes. Therefore if you want to alter the structure and
    for instance remove nodes you need to use XSLT (or DOM).

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Oct 3, 2005
    #2
    1. Advertising

  3. Guest

    Martin,

    Any idea how to make a simple XSLT file to rebuild an XML doc without a
    particualar node type? Most of the XSLT tutorials I find are elaborate
    descriptions of how to generate HTML from XML, and not simple
    instructions on how to edit an XML file.

    John
     
    , Oct 4, 2005
    #3
  4. wrote:

    > Any idea how to make a simple XSLT file to rebuild an XML doc without a
    > particualar node type?


    Starting with the identity transformation
    <http://www.w3.org/TR/xslt#copying> is often helpful:

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

    Then you simple add a template to not process a certain element at all e.g.
    <xsl:template match="product" />
    or to only process its child nodes e.g.
    <xsl:template match="product">
    <xsl:apply-templates />
    </xsl:template>
    or you add a template doing some other stuff you want to do with that
    element.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Oct 4, 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. Tjerk Wolterink
    Replies:
    9
    Views:
    1,315
    Joris Gillis
    Feb 7, 2005
  2. jon|k
    Replies:
    2
    Views:
    9,774
    jon|k
    Jan 20, 2006
  3. Replies:
    1
    Views:
    606
    Soren Kuula
    Aug 24, 2006
  4. Replies:
    1
    Views:
    1,224
    Joe Kesselman
    Nov 1, 2006
  5. Tim Streater

    Garbage collection after removing child nodes

    Tim Streater, Feb 29, 2008, in forum: Javascript
    Replies:
    2
    Views:
    122
    Thomas 'PointedEars' Lahn
    Mar 1, 2008
Loading...

Share This Page