Subject: Re: [xsl] Convert xml to another xml structure. From: Ragulf Pickaxe <ragulf.pickaxe@xxxxxxxxx> Date: Thu, 17 Nov 2005 07:28:46 +0100 |
Hi Raj, > Is it possible to arrange this XML to the following > XML element using XSL and using level attributes. Short answer: Yes. I suppose that the real XML is wellformed.... <xsl:template match="/"> <!-- This solution assumes that *[@level='1'] is the root of the document --> <xsl:apply-templates select="*[@level='1']"> </xsl:template> <xsl:template match="*"> <xsl:param name="current-level" select="'1'"/> <xsl:variable name="current-id" select="generate-id()"/> <xsl:copy> <!-- Shallow copy of the current node --> <xsl:copy-of select="@*"/> <!-- Copy all the attributes --> <!-- Get the logical children (will be called recursively): --> <xsl:apply-templates select="following-sibling::*[@level=$current-level+1][generate-id(preceding-s ibling::*[@level=$current-level])=$current-id]"><xsl:with-param name="current-level" select="$current-level+1"/></xsl:apply-templates> </xsl:copy> </xsl:template> If you change the elements name so that they are the same (say X), then you can use X instead of * in this solution. This will make the templates more specific. This solution, I think, does not have anything that has not been discussed with you before in various posts. First predicate ([...]) ensures that the element in question will be at a level deeper that the current one. Second predicate ([...]) ensures that the template will not call children of a sibling parent. Others on this list have suggested that you try out some of the tutorials that exists on the net. If you can get hold of "XSLT, programmers reference (2nd ed)" by Michael Kay, I think there is an example of this problem in that book. It is a great book for general understanding XSL as well as the reference that its title says it is. Regards, Ragulf Pickaxe :-)
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
[xsl] Convert xml to another xml st, Arulraj | Thread | Re: [xsl] Grouping / unique list wi, Alexander Johannesen |
[xsl] MS SQL Server Views Only, Arthur Maloney | Date | Re: [xsl] selecting the following-s, Mukul Gandhi |
Month |