开发者

Create html table header from xml file in Java

开发者 https://www.devze.com 2023-03-07 00:20 出处:网络
I need some light here on this problem I\'m trying to solve: I want to create an html table from a xml file and I\'d like

I need some light here on this problem I'm trying to solve:

I want to create an html table from a xml file and I'd like to create a table header according to the elements in the xml file开发者_开发百科 as follows:

<xml>
  <fields>
    <field>
      <name>A</name>
    </field>
    <merge label="D">
      <field>
        <name>B</name>
      </field>
      <field>
        <name>C</name>
      </field>
    </merge>
  </fields>
</xml>

Should generate a table header like this:

/========================\    \
|           |     D      |     |
|     A     |------------|     |- table header
|           |   B  |  C  |     |
|========================|    /
|   .....   |  ..  | ... |
\========================/  

Any ideas in how to do this using Java?


Something like this will get you started with the simple case (without the 'merge' tag).

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:template match="/fields">
     <html xmlns="http://www.w3.org/1999/xhtml">
      <body>
        <h1>Doc Header</h1>
            <xsl:for-each select="/fields/field">
                <table border="1">
                    <tr>
                       <th><xsl:value-of select="name"/></th>
                    </tr>
                </table>
            </xsl:for-each>
      </body>
    </html>
</xsl:template>


I'd use an XSLT file and apply it with Xalan, Saxon or Jaxp

0

精彩评论

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

关注公众号