Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 用于表化XML的通用XSLT_Html_Xml_Xslt_Xmlstarlet - Fatal编程技术网

Html 用于表化XML的通用XSLT

Html 用于表化XML的通用XSLT,html,xml,xslt,xmlstarlet,Html,Xml,Xslt,Xmlstarlet,我正在创建一个XSLT,它足够通用,可以创建任何输入XML数据的名称值表 例如 输出应如下所示: <table> <tr> <td>Field1</td> <td>value1</td> </tr> <tr> <td>Field2</td> <td>value2</td> </tr

我正在创建一个XSLT,它足够通用,可以创建任何输入XML数据的名称值表

例如

输出应如下所示:

<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>
我希望避免在XSLT代码中使用xml标记名,以便使其足够通用。不确定这是否可能。
有什么办法吗?

这个XSLT是通用的、面向推的,应该做到以下几点:

<?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>

下面是对@ABach解决方案的改进,它试图创建嵌套表:

<?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>
我没有尝试过对混合内容做任何非常巧妙的处理。

另一种方法是使用:

它产生:

<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>

如果有人正在寻找一种快速的方法使某些XML在浏览器中可读,我已经在Michael Kay的优秀解决方案中添加了一些CSS:

<?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>

向上面由Michael Kay启动的美丽解决方案添加属性:

<?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>
玩一点css,它会产生下面这样的东西…不是通常把树视图和表视图结合在一起…凯先生,尊敬的


但它会在a中生成嵌套的,这是无效的HTML。@MichaelKay-我不这么认为;转换生成OP请求的输出,正如您所看到的,它没有任何嵌套在。我遗漏了什么吗?他要求提供一个通用的解决方案,可以处理所有可能的输入,而不仅仅是一个可以处理他提供的示例输入的解决方案。有没有一种简单的方法来修改它,以便在html中生成的根元素没有列?如果你有一个新的需求,开始一个新的问题来解释它。
<?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>
<?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>