Subject: Re: [xsl] Nested element move to single element From: Syd Bauman <Syd_Bauman@xxxxxxxxx> Date: Sat, 30 Jan 2010 08:41:41 -0500 |
Well, I think the following XSLT2 (which may as well be XSLT1) does what I think you asked for. Putting aside the question "why?", it still may not be what you really want, though, depending on how consistent your input is. Others on the list may well have better ways to do this, of course. <?xml version="1.0" encoding="UTF-8"?> <!-- Note: stylesheet completely ignores nodes other --> <!-- than elements and text (i.e., attributes, comments, --> <!-- and processing instructions are dropped) --> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <!-- match only root <body> element --> <xsl:template match="/body"> <!-- spit out copy of self --> <xsl:copy> <!-- working on child nodes as output children --> <xsl:apply-templates/> </xsl:copy> </xsl:template> <!-- when I hit an input <item-group>, put out a --> <!-- copy of same, but process only <item> and <sub-item> --> <!-- children, in that order. --> <xsl:template match="item-group"> <xsl:apply-templates select="item"/> <xsl:apply-templates select="sub-item"/> </xsl:template> <!-- <item>s and <reference-targets> just get copied to --> <!-- output in a usual identity kinda way --> <xsl:template match="item|reference-targets"> <xsl:copy> <xsl:apply-templates/> </xsl:copy> </xsl:template> <!-- all <sub-item> and <sub-itemN> elements become an --> <!-- output <sub-item> that contains the input elements --> <!-- text content followed by what was the input element's --> <!-- younger sibling (now the output element's 2nd child) --> <xsl:template match="sub-item|sub-item1|sub-item2"> <sub-item> <xsl:apply-templates select="text()"/> <xsl:apply-templates select="following-sibling::*[1]"/> </sub-item> </xsl:template> </xsl:stylesheet>
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
[xsl] Nested element move to single, Selvaganesh | Thread | [xsl] xsl:copy-of issue, a kusa |
[xsl] Nested element move to single, Selvaganesh | Date | [xsl] Excluding an element with spe, Mark Wilson |
Month |