使用 xslt 在 HTML 表格中显示 xml 元素

问题描述:

我是 XSLT 新手.我有一个 XML 文件,我想使用 xslt 将 xml 文件中的信息显示到表格中.但我可以像这样连续获取信息:

I am new in XSLT.I have an XML file and I want to use xslt to show the information in the xml file into a table. but I can the information in a row like this:

apfel 8.97 Fa.Krause Kirschen 10.45 Fa.Helbig apfel 12.67 Fa.李比希这是我的 XML 文件:

apfel 8.97 Fa. Krause Kirschen 10.45 Fa. Helbig apfel 12.67 Fa. Liebig this is my XML file:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="/First.xsl"?>
<lieferungen xmlns="urn:myspace:lieferungen"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:myspace:lieferungen ....">
  <artikel id="3526">
    <name>apfel</name>
    <preis stueckpreis="true">8.97</preis>
    <lieferant>Fa. Krause</lieferant>
  </artikel>
  <artikel id="7866">
    <name>Kirschen</name>
    <preis stueckpreis="false">10.45</preis>
    <lieferant>Fa. Helbig</lieferant>
  </artikel>
</lieferungen>

这是我的 XSLT:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
  <xsl:template match="/">
    <html>
      <h1>The First XSLT in diesem Jahr</h1>
      <table>
        <tr>
          <td>Name</td>
          <td>Artikel</td>
          <td>Preis</td>
          <td>Liferant</td>
        </tr>
        <xsl:for-each select="artikel">
          <tr>
            <td>
              <xsl:value-of select="name"/>
            </td>
            <td>
              <xsl:value-of select="preis"/>
            </td>
            <td>
              <xsl:value-of select="lieferant"/>
            </td>
          </tr>
        </xsl:for-each>
      </table>
    </html>
  </xsl:template>
</xsl:stylesheet>

您的 xslt 只有一些小问题.主要的一点是,您的 XML 有一个默认命名空间.因此,您需要在样式表中添加带有前缀的命名空间.就像是:xmlns:my="urn:myspace:lieferungen"

There are only small problems in your xslt. The main one is, that your XML has an default namespace. Therefore you need to add this namespace with an prefix to your stylesheet. Something like: xmlns:my="urn:myspace:lieferungen"

您必须使用新前缀 my 来访问元素表单默认命名空间.例如:

Than you have to use the new prefix my for any access to an element form default namespace. E.g.:

<xsl:for-each select="my:artikel">

第二:您的 for-each 循环遍历 artikel,因此您需要在liefrant 元素中才能完成这项工作.将您的模板更改为

Second: Your for-each loop iterate over artikel therefor you need to be in liefrant element to make this work. Change your template to <xsl:template match="/*">

因此试试这个:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:my="urn:myspace:lieferungen" >
    <xsl:template match="/*">
        <html>
            <h1>The First XSLT in diesem Jahr</h1>
            <table>
                <tr>
                    <td>Name</td>
                    <td>Artikel</td>
                    <td>Preis</td>
                    <td>Liferant</td>
                </tr>
                <xsl:for-each select="my:artikel">
                    <tr>
                        <td>
                            <xsl:value-of select="my:name"/>
                        </td>
                        <td>
                            <xsl:value-of select="my:preis"/>
                        </td>
                        <td>
                            <xsl:value-of select="my:lieferant"/>
                        </td>
                    </tr>
                </xsl:for-each>
            </table>
        </html>
    </xsl:template>
</xsl:stylesheet>

这将生成以下输出:

<html xmlns:my="urn:myspace:lieferungen">
  <h1>The First XSLT in diesem Jahr</h1>
  <table>
    <tr>
      <td>Name</td>
      <td>Artikel</td>
      <td>Preis</td>
      <td>Liferant</td>
    </tr>
    <tr>
      <td>apfel</td>
      <td>8.97</td>
      <td>Fa. Krause</td>
    </tr>
    <tr>
      <td>Kirschen</td>
      <td>10.45</td>
      <td>Fa. Helbig</td>
    </tr>
  </table>
</html>