简体   繁体   中英

How to convert XML (generated from dataset) to HTML using xslt

Trying to get the html from the xml I have generated following xml from a datset which means that I cannot change the structure of XML.I think I am missing on a second loop that loop through the A and B .

<Myds>
  <A>
    <col1>Row1</col1>
    <col2>1</col2>
    <col3>2</col3>
  </A>
  <A>
    <col1>Row2</col1>
    <col2>4</col2>
    <col3>3</col3>
  </A>
  <B>
    <col1>Row1</col1>
    <col2>1</col2>
    <col3>2</col3>
  </B>
  <B>
    <col1>Row2</col1>
    <col2>4</col2>
    <col3>3</col3>
  </B>
</Myds>

This is the XSL

     <xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
                <xsl:template match='/'>
                   <xsl:for-each select ='./Myds'>
                    <table>
                            <xsl:for-each select ='A'>
                                <tr>
                                    <td><xsl:value-of select='col1'/></td>
                                    <td><xsl:value-of select='col2'/></td>
                                    <td><xsl:value-of select='col3'/></td>
                                </tr> 
                            </xsl:for-each>
                    </table>
                     </xsl:for-each>
                </xsl:template>
        </xsl:stylesheet>

I have got this output

<?xml version="1.0" encoding="utf-8"?>
   <table>
   <tr><td>Row1</td><td>1</td><td>2</td></tr>
   <tr><td>Row2</td><td>4</td><td>3</td></tr>
   </table>

But I was expecting this

       <table>//(For Table A)
       <tr><td>Row1</td><td>1</td><td>2</td></tr>
       <tr><td>Row2</td><td>4</td><td>3</td></tr>
       </table>
       <table>//(For Table B)
       <tr><td>Row1</td><td>1</td><td>2</td></tr>
       <tr><td>Row2</td><td>4</td><td>3</td></tr>
       </table>

I think I need something like this but don't exactly how...

        <xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
                <xsl:template match='/'>
                   <xsl:for-each select ='./Myds'>
                    <table>
                            <xsl:for-each select ='UNIQUE SECOND NODE'>
                                <tr>
                                    <td><xsl:value-of select='col1'/></td>
                                    <td><xsl:value-of select='col2'/></td>
                                    <td><xsl:value-of select='col3'/></td>
                                </tr> 
                            </xsl:for-each>
                    </table>
                     </xsl:for-each>
                </xsl:template>
            </xsl:stylesheet>

xslt is case sensitive; for each row you'll need

<td><xsl:value-of select='col1'/></td>
<td><xsl:value-of select='col2'/></td>
<td><xsl:value-of select='col3'/></td>

and you'll need to use Muenchian grouping to get one <table> per like-named elements:

<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:key name="byTable" match="/Myds/*" use="name()" />
<xsl:template match='/'>
  <xsl:for-each select="Myds/*[generate-id() = generate-id(key('byTable', name())[1])]">
    <table>
      <xsl:comment>for <xsl:value-of select="name()"/></xsl:comment>
      <xsl:for-each select ="key('byTable', name())">
        <tr>
          <td><xsl:value-of select='col1'/></td>
          <td><xsl:value-of select='col2'/></td>
          <td><xsl:value-of select='col3'/></td>
        </tr>
      </xsl:for-each>
    </table>
  </xsl:for-each>  
</xsl:template>
</xsl:stylesheet>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM