grouping elements

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ofuuzo1@yahoo.no

    grouping elements

    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_i d>
    ....
    <dept>
    <name>Peter Pan</name>
    <dept>
    </person>

    <person>
    <dept>
    <gr_name>produc tion</gr_name>
    <gr_id>3<gr_i d>
    ....
    <dept>
    <name>John John</name>
    <dept>
    </person>

    <person>
    <dept>
    <gr_name>packag e</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_pac kage_etc>John John<production _package_etc>
    <production_pac kage_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:val ue-of select="name" /><sale>
    </xsl:if>
    </xsl:for-each>

    <xsl:for-each select="//person">
    <xsl:if test="//person/dept/gr_id != 1">
    < production_pack age_etc<xsl:val ue-of select="name" /
    >< production_pack age_etc>
    </xsl:if>
    </xsl:for-each>

    Thanks in advance.
  • Martin Honnen

    #2
    Re: grouping elements

    ofuuzo1@yahoo.n o 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_i d>
    ....
    <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

    Comment

    Working...