xsl-list
[Top] [All Lists]

[xsl] RE: loop elements based on attribute

2008-08-14 04:07:37
Now, I've got another problem. I want a list of Pkt-elements to be
wrapped inside a List-element.

I want this xml:

<test>
<A>test</A>
 <Pkt type="pkt">Text</Pkt>
 <Pkt type="uavs">Text 1</Pkt>
 <Pkt type="pkt">More text</Pkt>
 <Pkt type="uavs">More text 1</Pkt>
 <Pkt type="uavs">More text 2</Pkt>
 <B>test</B>
 <A>test</A>
 <Pkt type="pkt">Lot of text</Pkt>
 <Pkt type="pkt">Tons of text</Pkt>
 <Pkt type="uavs">Tons of text 1</Pkt>
 </test>

To look like this:

<test>
        <A>test</A>
        <List>
                <Pkt type="pkt">Text</Pkt>
                <Pkt type="uavs">Text 1</Pkt>
                <Pkt type="pkt">More text</Pkt>
                <Pkt type="uavs">More text 1</Pkt>
                <Pkt type="uavs">More text 2</Pkt>
        </List>
        <B>test</B>
        <A>test</A>
        <List>
                <Pkt type="pkt">Lot of text</Pkt>
                <Pkt type="pkt">Tons of text</Pkt>
                <Pkt type="uavs">Tons of text 1</Pkt>
        </List>
 </test>

Anybody who can help me?

Kjellaug.


-------------------------------

Thanks, Mukul! It works great!

Kjellaug.

------------------------------

Below is the solution for this ...

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
               version="2.0">

  <xsl:output method="xml" indent="yes" />

  <xsl:template match="test">
    <result>
      <xsl:for-each-group select="*" group-starting-with="Pkt[(_at_)type =
'pkt']">
          <Pkt>
            <xsl:for-each select="current-group()">
              <A><xsl:value-of select="." /></A>
            </xsl:for-each>
          </Pkt>
      </xsl:for-each-group>
    </result>
  </xsl:template>

</xsl:stylesheet>


-- 
Regards,
Mukul Gandhi

------------------------------

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>