Groups | Blog | Home
all groups > dotnet xml > may 2006 >

dotnet xml : Convert Question (From XML to XML) using XSLT?


Homer
5/29/2006 12:54:56 PM
Hi All,

I have following XML File:

<Header></Header>
<Category></Category>
<Item1></Item1>
<Item2></Item2>
<Category></Category>
<Item3></Item3>


Could someone please send me an example what is the XSLT text to
convert it to:

<Header>
<Category>
<Item1></Item1>
<Item2></Item2>
</Category>
<Category>
<Item3></Item3>
</Category>
</Header>



Thanks is advance,

Homer
Peter Flynn
5/29/2006 9:56:04 PM
[quoted text, click to view]

I hope you don't mean that. I hope you mean

<doc>
<Header></Header>
<Category></Category>
<Item></Item>
<Item></Item>
<Category></Category>
<Item></Item>
</doc>

(in other words, three occurrences of the Item element type, not one
each of three separately-named element types Item1, Item2, and Item3).

[quoted text, click to view]

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="Category">
<Category>
<xsl:apply-templates
select="following-sibling::Item
[generate-id(preceding-sibling::Category[1]) =
generate-id(current())]" mode="included"/>
</Category>
</xsl:template>

<xsl:template match="Item"/>

<xsl:template match="Item" mode="included">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>

///Peter
--
Homer
5/30/2006 6:06:49 AM
Thanks so much Peter. It was a Big help.
Homer
5/30/2006 11:24:23 AM
This is a small problem. I am getting following result:

<?xml version="1.0" encoding="UTF-8"?>
<root>
<Header/>
<Category>
<Item/>
<Item/>
</Category>
<Category>
<Item/>
</Category>
</root>

Instead of:

<?xml version="1.0" encoding="UTF-8"?>
<root>
<Header>
<Category>
<Item></Item>
<Item></Item>
</Category>
<Category>
<Item></Item>
</Category>
</Header>
</root>

Do you know why?
AddThis Social Bookmark Button