0

I have an XML schema where I need to filter specific elements from each node and group those filtered elements into desired XML format.

XML schema is as follows-

<Roots>
 <Root>
  <Result>
   <Row index="1">
    <id>001</id>
    <value>Test Value</value>
   </Row>
   <Row index="2">
    <id>001</id>
    <value>Test Value1</value>
   </Row>
   <Row index="3">
    <id>001</id>
    <value>Test Value2</value>
   </Row>
   <Row index="4">
    <id>002</id>
    <value>Test Value3</value>
   </Row>
   <Row index="5">
    <id>002</id>
    <value>Test Value4</value>
   </Row>
   <Row index="6">
    <id>003</id>
    <value>Test Value5</value>
   </Row>
   <Row index="7">
    <id>003</id>
    <value>Test Value6</value>
   </Row>
   <Row index="8">
    <id>003</id>
    <value>Test Value7</value>
   </Row>
  </Result>
 </Root>
</Root>

I want to select element <id> and <value> from each node <Row> grouping by <id>. The expected XML schema is as follows-

<html>
<body>
<table border="1">
  <tr>
   <td>001</td>
   <td>Test Value</td>
   <td>Test Value1</td>
   <td>Test Value2</td>
  </tr>
  <tr>
   <td>002</td>
   <td>Test Value3</td>
   <td>Test Value4</td>
  </tr>
  <tr>
   <td>003</td>
   <td>Test Value5</td>
   <td>Test Value6</td>
   <td>Test Value7</td>
  </tr>
</table>
</body>
</html>

Could you please help me for the above format. Working XSLT is as follows-

'<xsl:stylesheet version="1.0"     
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="Roots/Root/Result">
<html>
 <body>
  <table border="1">
   <xsl:for-each-group select="Row" group-by="id">
    <tr>
     <td><xsl:value-of select="id"/></td>
     <xsl:for-each select="current-group()">
        <td><xsl:value-of select="value"/></td>
    </xsl:for-each>
    </tr>
   </xsl:for-each-group>    
  </table>
 </body>
</html>
</xsl:template>
</xsl:stylesheet>'
QA Testing
  • 57
  • 7
  • Where is your XSLT? What have you tried? Did you look [at this SO question](https://stackoverflow.com/questions/3246216/looping-over-distinct-values) or [this other SO question](https://stackoverflow.com/questions/2291567/how-to-use-xslt-to-create-distinct-values)? – AntonH Mar 28 '17 at 16:42
  • For questions on XSLT grouping you need to specify XSLT 1.0 or 2.0 as the answers will be very different. And you need to give some indication of how far you got with the problem and where you got stuck - we're here to help you when you get stuck, not to write the code for you from first principles. – Michael Kay Mar 28 '17 at 17:25
  • Also, the usual meaning of "XML schema" is an XSD schema document: there is nothing remotely schema-like about your input. – Michael Kay Mar 28 '17 at 17:26
  • HI @MichaelKay I have added my XSLT script. – QA Testing Mar 30 '17 at 10:55
  • Well, for a start, you have two templates both with match="/" and no explicit priorities, and XSLT 1.0 leaves it very implementation-defined what happens in that situation. – Michael Kay Mar 30 '17 at 12:16
  • HI @MichaelKay I do not have much knowledge on how to resolve this. I am learning and trying to implement the concept but I couldn't be able to fix this. – QA Testing Apr 04 '17 at 15:27
  • Sorry, SO is good at providing specific answers to specific questions, but it's not really a substitute for studying a good text book or attending a training course. (Both of which are old-fashioned ideas, I know, but I've got enough grey hair to be allowed old-fashioned ideas...) – Michael Kay Apr 04 '17 at 15:37
  • Hi @MichaelKay I made the working XSLT and provided the same. Thank you. – QA Testing Apr 05 '17 at 07:48

1 Answers1

0

this is a standart grouping schema in XSLT 2.0 (i assume that you are using the 2.0 since you didnt specify).

https://www.xml.com/pub/a/2003/11/05/tr.html

Check this link and it will help you a lot to shape your algorithm to do what you want.

If you can share the XSLT code then we could be able to help you more.

Good Luck

Sojimanatsu
  • 619
  • 11
  • 28