[xsl] RE: loop elements based on attribute

Subject: [xsl] RE: loop elements based on attribute
From: "Kjellaug Johansen" <kjellaug.johansen@xxxxx>
Date: Thu, 14 Aug 2008 13:08:15 +0200
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[@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

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

Current Thread