Solution 1 :

As you said:

Any pointer will be very helpful

This minimal XSLT stylesheet is to get you started:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns_xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns_xs="http://www.w3.org/2001/XMLSchema" 
    exclude-result-prefixes="xs" version="2.0">
  <xsl:output method="html" encoding="UTF-8" indent="yes"/>
  <xsl:template match="/">
    <table>
        <xsl:apply-templates select="/root/dq"/>
    </table>
  </xsl:template>
  <xsl:template match="row">
    <tr>
      <xsl:apply-templates/>
    </tr>
  </xsl:template>
  <xsl:template match="row/child::*">
    <td>
      <xsl:apply-templates/>
    </td>
  </xsl:template>
</xsl:stylesheet>

It will produce the following HTML table structure

<table>     
  <tr>  
    <td>Test 1</td>
    <td>Test 2</td>
  </tr>   
  <tr>      
    <td>Test 21</td>      
    <td>Test 22</td>
  </tr>
</table>

Problem :

I am new to XSLT. I am trying the parse the below XML and extract the values for each <row> element. There could be any number of child <row> elements under <row>, but I will have the names of the fields under <row>. Is there a way to loop and print this in a table using XSLT? Any pointer will be very helpful.

<root>
  <dq>    
    <row>
      <rowa>Test 1</rowa>
      <rowb>Test 2</rowb>
    </row>  
    <row>
      <rowa>Test 21</rowa>
      <rowb>Test 22</rowb>
    </row>
   </dq> 
</root>

I am trying to get an HTML table, which will be rendered like:

Test 1 
Test 2
Test 21 
Test 22

Comments

Comment posted by Yitzhak Khabinsky

Please edit your post and add desired output based on the input XML. Also, what did you try to do? What is not working? Overall, you need to provide a minimal reproducible example.

Comment posted by here

As Yitzhak rightly points out SO users are encouraged to provide a Minimal Reproducible Example, a reprex. You can find out how to do so

Comment posted by michael.hor257k

What if there is

Comment posted by Thomas Hansen

@michael.hor257k — Good point! Changed the last template to make it a bit more robust.

By