【问题标题】:How to get attribute values of an XML and code as thead values for html using xslt如何使用 xslt 获取 XML 的属性值和代码作为 html 的值
【发布时间】:2021-09-16 17:12:08
【问题描述】:

我目前正在使用 XSLT 从 XML 文档中提取值并创建新的 HTML 文档。我的部分 xml,XSLT 代码如下。我不知道如何从xml的属性值中显示html的thead值:

源xml:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="test.xslt"?>
<Summary>
  <test name="test">
    <xml_name name="ABC">
        <version num="1.0">
            <component name="APPS">
                <componenet_ver>104</componenet_ver>
            </component>
        <component name="Ner">
            <componenet_ver>1.0</componenet_ver>
            </component>
            <component name="HUNE">
                <componenet_ver>003</componenet_ver>
            </component>
            <component name="FADA">
                <componenet_ver>107</componenet_ver>
            </component>
            <component name="VEDA">
                <componenet_ver>8.8</componenet_ver>
            </component>
        </version>
    </xml_name>
  </test>
</Summary>

我试过的 XSLT 文件:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
  <html>
  <body>
    <h2>Compo. Name and Versions</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th><xsl:value-of select="/Summary/test/xml_name"/></th>    ### this line
        <th><xsl:value-of select="/Summary/test/xml_name/version"/></th>    ###  and this line
      </tr>
          <xsl:for-each select="/Summary/test/xml_name/version/component">
                  <tr>
                    <td><xsl:value-of select="@name"/></td>
                    <td><xsl:value-of select="componenet_ver"/></td>
                  </tr>
            </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

获取 o/p:

预期输出:

【问题讨论】:

    标签: html css xml xslt xpath


    【解决方案1】:

    您没有在 XPath 表达式中指定属性。
    所以只需使用以下代码:

    <xsl:template match="/">
      <html>
      <body>
        <h2>Compo. Name and Versions</h2>
        <table border="1">
          <tr bgcolor="#9acd32">
            <th><xsl:value-of select="/Summary/test/xml_name/@name"/></th>
            <th><xsl:value-of select="/Summary/test/xml_name/version/@num"/></th>
          </tr>
          <xsl:for-each select="/Summary/test/xml_name/version/component">
            <tr>
              <td><xsl:value-of select="@name"/></td>
              <td><xsl:value-of select="componenet_ver"/></td>
            </tr>
          </xsl:for-each>
        </table>
      </body>
      </html>
    </xsl:template>
    

    它的输出应该和预期的一样:

    【讨论】:

    • @ikegami:是的,我也猜到了。我一直不确定我是否应该提及它。但是因为这显然是错误的决定,所以我现在删除了它。
    猜你喜欢
    • 1970-01-01
    • 2014-01-28
    • 2021-12-20
    • 1970-01-01
    • 2015-09-01
    • 1970-01-01
    • 2021-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多