xml - How can I wrap a group of adjacent elements depending on the previous sibling using XSLT? -
i want wrap appendix elements, if first appendix element's preceding sibling part.
so if input like
<part> .. .. </part> <appendix href=".."> </appendix> <appendix href=".."> </appendix>
then output like
<part> .. .. </part> <appendices> <appendix href=".."> </appendix> <appendix href=".."> </appendix> </appendices>
i'm new xslt. i've tried has failed till now. appreciated.
with xslt 2.0 verbal description translates into
<?xml version="1.0" encoding="utf-8" ?> <xsl:transform xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="2.0"> <xsl:output indent="yes"/> <xsl:template match="*[appendix]"> <xsl:copy> <xsl:for-each-group select="*" group-adjacent="boolean(self::appendix)"> <xsl:choose> <xsl:when test="current-grouping-key() , preceding-sibling::*[1][self::part]"> <appendices> <xsl:apply-templates select="current-group()"/> </appendices> </xsl:when> <xsl:otherwise> <xsl:apply-templates select="current-group()"/> </xsl:otherwise> </xsl:choose> </xsl:for-each-group> </xsl:copy> </xsl:template> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> </xsl:transform>
Comments
Post a Comment