grouping elements

Discussion in 'XML' started by ofuuzo1@yahoo.no, Apr 15, 2008.

  1. Guest

    Hi,
    I have the following xml file which I would like to transform to
    another structure:

    <person>
    <dept>
    <gr_name>sale</gr_name>
    <gr_id>2<gr_id>
    ....
    <dept>
    <name>Peter Pan</name>
    <dept>
    </person>

    <person>
    <dept>
    <gr_name>production</gr_name>
    <gr_id>3<gr_id>
    ....
    <dept>
    <name>John John</name>
    <dept>
    </person>

    <person>
    <dept>
    <gr_name>package</gr_name>
    <gr_id>11<gr_id>
    ....
    <dept>
    <name>Roy Kemp</name>
    <dept>
    </person>
    ......

    New xml would look like this:
    <sale>Peter Pan</sale>
    <production_package_etc>John John<production_package_etc>
    <production_package_etc>Roy Kemp<production_package_etc>

    I have written some thing like this and it is not working. What is
    wrong her?


    <xsl:for-each select="//person">
    <xsl:if test="//person/dept/gr_id = 1">
    <sale> <xsl:value-of select="name" /><sale>
    </xsl:if>
    </xsl:for-each>

    <xsl:for-each select="//person">
    <xsl:if test="//person/dept/gr_id != 1">
    < production_package_etc> <xsl:value-of select="name" /
    >< production_package_etc>

    </xsl:if>
    </xsl:for-each>

    Thanks in advance.
    , Apr 15, 2008
    #1
    1. Advertising

  2. wrote:

    > I have the following xml file which I would like to transform to
    > another structure:
    >
    > <person>
    > <dept>

    ^^^^^^
    So here is a 'dept' start tag
    > <gr_name>sale</gr_name>
    > <gr_id>2<gr_id>
    > ....
    > <dept>

    ^^^^^^
    And here is another one
    > <name>Peter Pan</name>
    > <dept>

    ^^^^^^
    And here is yet another one while there are no matching end tags.
    Can you post a well-formed sample? Currently it is not clear how your
    input XML looks.


    > <xsl:for-each select="//person">
    > <xsl:if test="//person/dept/gr_id = 1">


    You need a relative XPath here e.g.
    <xsl:if test="dept/gr_id = 1">
    but it is even better to write templates e.g.
    <xsl:template match="person[dept/gr_id = 1]">


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Apr 15, 2008
    #2
    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. kcwolle
    Replies:
    0
    Views:
    364
    kcwolle
    Nov 27, 2003
  2. Dimitre Novatchev

    .Re: Grouping neighboring elements with xsl

    Dimitre Novatchev, Nov 28, 2003, in forum: XML
    Replies:
    0
    Views:
    470
    Dimitre Novatchev
    Nov 28, 2003
  3. Jody Greening
    Replies:
    5
    Views:
    668
    Jody Greening
    Jan 6, 2005
  4. Jody Greening
    Replies:
    0
    Views:
    335
    Jody Greening
    Jan 6, 2005
  5. howa

    Grouping of elements?

    howa, Sep 16, 2007, in forum: XML
    Replies:
    7
    Views:
    398
    Joseph Kesselman
    Sep 17, 2007
Loading...

Share This Page