Re: Convert xml to another xml structure.
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 :-)
PURCHASE STYLUS STUDIO ONLINE TODAY!
Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!
Download The World's Best XML IDE!
Accelerate XML development with our award-winning XML IDE - Download a free trial today!
Subscribe in XML format