Generic XSLT to tabluate XML

2019-04-08 18:56发布

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 ?

标签: html xml xslt
4条回答
聊天终结者
2楼-- · 2019-04-08 19:28

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.

查看更多
爷、活的狠高调
3楼-- · 2019-04-08 19:34

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>
查看更多
Fickle 薄情
4楼-- · 2019-04-08 19:42

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>
查看更多
贼婆χ
5楼-- · 2019-04-08 19:42

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>
查看更多
登录 后发表回答