mathewrond Posted July 13, 2010 Share Posted July 13, 2010 I am having an XML structure as below<rows><row><head>123</head><id>1</id><body1>b1</body1><body2>b2</body2></row><row><head>123</head><id>1</id><body1>b3</body1><body2>b4</body2></row><row><head>456</head><id>2</id><body1>b5</body1><body2>b6</body2></row></rows>I am trying to show head information based on the <id> node. If the ID is same then I dont have to repeat the header information something like belowHEAD - 123BODYb1, b2b3, b4HEAD - 456BODYb5, b6I am not sure how to form for-each-group conditon for this. Can anyone help please? Link to comment Share on other sites More sharing options...
Martin Honnen Posted July 13, 2010 Share Posted July 13, 2010 Simply do for-each-group select="row" group-by="id": <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> <xsl:output method="text"/> <xsl:template match="rows"> <xsl:for-each-group select="row" group-by="id"> <xsl:text>HEAD - </xsl:text> <xsl:value-of select="head"/> <xsl:text>BODY</xsl:text> <xsl:value-of select="for $r in current-group() return string-join($r/*[starts-with(local-name(), 'body')], ', ')" separator=""/> <xsl:text></xsl:text> </xsl:for-each-group> </xsl:template></xsl:stylesheet> Link to comment Share on other sites More sharing options...
mathewrond Posted July 13, 2010 Author Share Posted July 13, 2010 Thank you very much for your reply. Could you please explain me what the for $r logic does here? Also the output is in a single line and not in multiple lines as in rows. Simply do for-each-group select="row" group-by="id":<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> <xsl:output method="text"/> <xsl:template match="rows"> <xsl:for-each-group select="row" group-by="id"> <xsl:text>HEAD - </xsl:text> <xsl:value-of select="head"/> <xsl:text>BODY</xsl:text> <xsl:value-of select="for $r in current-group() return string-join($r/*[starts-with(local-name(), 'body')], ', ')" separator=""/> <xsl:text></xsl:text> </xsl:for-each-group> </xsl:template></xsl:stylesheet> Link to comment Share on other sites More sharing options...
mathewrond Posted July 13, 2010 Author Share Posted July 13, 2010 I think I got what you are trying to do, thanks very much for your help. Thank you very much for your reply. Could you please explain me what the for $r logic does here? Also the output is in a single line and not in multiple lines as in rows. Link to comment Share on other sites More sharing options...
Martin Honnen Posted July 13, 2010 Share Posted July 13, 2010 As for line breaks missing in the output, that is caused by the forum software messing up the code sample, it has several (& # 1 0; without the spaces) to create line breaks but in the forum those simply show up as blanks. So for separator=" " use separator="& # 1 0;" (without the spaces) and in the xsl:text use that as well to create line breaks.$r is a variable bound to current-group() and then outputting the concatenation of those bodyx elements. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.