Generic XSLT to tabluate XML
Asked Answered
H

5

6

I am working to create an XSLT which is generic enough to create a table of name-value of any input XML data.

Eg.

<root>
    <Field1>value1</Field1>
    <Field2>value2</Field2>
    <Field3>value3</Field3>
</root>

Output should look like :

<table>
   <tr>
      <td>Field1</td>
      <td>value1</td>
  </tr>
   <tr>
      <td>Field2</td>
      <td>value2</td>
  </tr>
   <tr>
      <td>Field3</td>
      <td>value3</td>
  </tr>
</table>

I want to avoid using xml tag names in XSLT code, so as to make it generic enough. Not sure if this is possible at all . Any ideas how to go about this ?

Hondo answered 12/7, 2013 at 0:51 Comment(0)
N
8

Here's a refinement of the solution from @ABach, which attempts to create nested tables:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="/">
    <table>
      <xsl:apply-templates/>
    </table>
  </xsl:template>

  <xsl:template match="*">
    <tr>
      <td>
        <p><xsl:value-of select="name()"/></p>
      </td>
      <td>
        <p><xsl:value-of select="."/></p>
      </td>
    </tr>
  </xsl:template>

  <xsl:template match="*[*]">
    <tr>
      <td>
        <p><xsl:value-of select="name()"/></p>
      </td>
      <td>
        <table>
          <xsl:apply-templates/>
        </table>
      </td>
    </tr>
  </xsl:template>

</xsl:stylesheet>

I haven't attempted to do anything very clever with mixed content.

Nun answered 12/7, 2013 at 8:32 Comment(2)
is there a simple way to modify this, so that there will be no column for the root element produced in the html?Rounders
If you have a new requirement, start a new question to explain it.Nun
W
2

I've added a bit of CSS to the excellent solution from Michael Kay, if anyone is looking for a quick way to make some XML readable in a browser:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:strip-space elements="*"/>


  <xsl:template match="/">
    <style>
      body {font-family: sans-serif;}
      td {padding: 4px;}
    </style>
    <table>
      <xsl:apply-templates/>
    </table>
  </xsl:template>

  <xsl:template match="*">
    <tr>
      <td style="background-color: #aaa;">
        <p><xsl:value-of select="name()"/></p>
      </td>
      <td style="background-color: #ccc;">
        <p><xsl:value-of select="."/></p>
      </td>
    </tr>
  </xsl:template>

  <xsl:template match="*[*]">
    <tr>
      <td style="border:2px solid #c55; font-size:120%;">
        <p><xsl:value-of select="name()"/></p>
      </td>
      <td style="">
        <table>
          <xsl:apply-templates/>
        </table>
      </td>
    </tr>
  </xsl:template>

</xsl:stylesheet>
Waterlog answered 12/7, 2013 at 0:51 Comment(0)
C
1

This XSLT is generic, push-oriented, and should do the trick:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="/*">
    <table>
      <xsl:apply-templates/>
    </table>
  </xsl:template>

  <xsl:template match="*">
    <tr>
      <td>
        <xsl:value-of select="name()"/>
      </td>
      <td>
        <xsl:apply-templates/>
      </td>
    </tr>
  </xsl:template>

</xsl:stylesheet>
Corroboree answered 12/7, 2013 at 2:57 Comment(3)
But it will produce a <tr> nested inside a <td>, which is not valid HTML.Nun
@MichaelKay - I don't believe so; the transformation produces the OP's requested output, which, as you can see, doesn't have any <tr>s nested inside a <td>. Am I missing something?Corroboree
He asked for a generic solution that could handle all possible inputs, not simply one that could handle his supplied example input.Nun
M
0

An alternative using :

xmlstarlet select -D -t \
    -o '<table>' \
    -m '/root/*' \
        -o '<tr>' \
        -o '<td>' -c 'name()' -o '</td>' \
        -o '<td>' -c 'text()' -o '</td>' \
        -o '</tr>' \
    -b \
    -o '</table>' \
xmlfile | xmlstarlet unescape | xmlstarlet format -o -s 2

It yields:

<table>
  <tr>
    <td>Field1</td>
    <td>value1</td>
  </tr>
  <tr>
    <td>Field2</td>
    <td>value2</td>
  </tr>
  <tr>
    <td>Field3</td>
    <td>value3</td>
  </tr>
</table>
Metallist answered 12/7, 2013 at 9:30 Comment(0)
W
0

Adding attributes to the beautiful solution started by Michael Kay above:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:strip-space elements="*"/>


  <xsl:template match="/">
    <style>
      body {font-family: sans-serif;}
      td {padding: 4px;}
    </style>
    <table>
      <xsl:apply-templates/>
    </table>
  </xsl:template>

  <xsl:template match="*">
    <tr>
      <td style="background-color: #aaa;">
        <p><xsl:value-of select="name()"/></p>
      </td>
      <td style="background-color: #ccc;">
        <p><xsl:value-of select="."/></p>
      </td>
    </tr>
  </xsl:template>

  <xsl:template match="*[*]">
    <tr>
      <td style="border:2px solid #c55; font-size:120%; font-style:oblique;">
        <p><xsl:value-of select="name()"/></p>
      </td>
    <xsl:for-each select="@*">
        <p>
            <xsl:value-of select="name()" /> : <xsl:value-of select="." />
        </p>
    </xsl:for-each>
      <td style="">
        <table>
          <xsl:apply-templates/>
        </table>
      </td>
    </tr>
  </xsl:template>

</xsl:stylesheet>

Play a bit with css and it will produce something like below...not that usual to combine a tree view with table view in one...Mr Kay, respect!

enter image description here

Wisent answered 15/5, 2020 at 15:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.