开发者

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

开发者 https://www.devze.com 2023-02-20 10:52 出处:网络
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 an

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&开发者_如何学JAVAgt;<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>
0

精彩评论

暂无评论...
验证码 换一张
取 消