【问题标题】:XSLT & XML Help - Not generating html with two different XML elementsXSLT 和 XML 帮助 - 不使用两个不同的 XML 元素生成 html
【发布时间】:2011-04-27 17:24:42
【问题描述】:

在某种情况下我无法将我的 XSLT 和 XML 结合起来,我被卡住了,下面是我的问题的一个示例:

XML:

<a>
<x>
    <y testname="test1">
      <object elementname="Name" elementvalue="true" elementvalue1="ABC" elementvalue2="ADF">
        <comparisonresult>false</comparisonresult>
      </object>
     </y>

</x>
</a>

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>
        <font face="Arial">
        <h2>HEADINGY</h2>
        <h4>Process 1</h4>
        <h4>More Process</h4>
            <h4>Additional </h4>
              <table border="1">
              <tr bgcolor="#dccdc">
                <th align="center">T1</th>
                <th align="center">T2</th>
                <th align="center">T3</th>
              </tr>
              </table>
          <h2>Main</h2>
          <xsl:for-each select="a/x/y">
            <h3>
              <xsl:value-of select="@testname" />
            </h3>
            <h4>Heading 1</h4>
            <table border="1" style="display:inline">
              <tr bgcolor="#CECEF6">
                <th align="center">Item1</th>
                <th align="center">Item2</th>
                <th align="center">Item3</th>
              </tr>
              <xsl:for-each select="object">
                <tr>
                  <td bgcolor="#F2F5A9">
                    <xsl:value-of select="@elementname" />
                  </td>
                  <td bgcolor="#F2F5A9">
                    <xsl:value-of select="@elementvalue1" />
                  </td>
                  <td bgcolor="#F2F5A9">
                    <xsl:value-of select="@elementvalue2" />
                  </td>

                </tr>
              </xsl:for-each>
            </table>
          </xsl:for-each>
        </font>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

如果您将这两个输入到http://www.w3schools.com/xml/tryxslt.asp?xmlfile=simple&xsltfile=simple 中,您将获得预期的输出。当我将以下内容添加到 XML 时,问题就出现了,所以它看起来像:

<a>
<x>
    <y testname="test1">
      <object elementname="Name" elementvalue="true" elementvalue1="ABC" elementvalue2="ADF">
        <comparisonresult>false</comparisonresult>
      </comparisonobject>
</y>
<x>

 <t testname="ComparisonResult">
    <step stepname="Add x" stepresult="Add x">
      <result>true</result>
    </step>
</t>



</a>

以及对应的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>
        <font face="Arial">
        <h2>HEADINGY</h2>
        <h4>Process 1</h4>
        <h4>More Process</h4>
          <xsl:for-each select="a/x/t">
             <xsl:for-each select="testname">
            <h4>Additional </h4>
              <table border="1">
              <tr bgcolor="#dccdc">
                <th align="center">T1</th>
                <th align="center">T2</th>
                <th align="center">T3</th>
              </tr>

 <xsl:for-each select="stepname">
                <tr>
                  <td bgcolor="#F2F5A9">
                    <xsl:value-of select="@stepname" />
                  </td>
                  <td bgcolor="#F2F5A9">
                    <xsl:value-of select="@step result" />
                  </td>
                  <td bgcolor="#F2F5A9">
                    <xsl:value-of select="@result" />
                  </td>
              </table>
          <h2>Main</h2>
          <xsl:for-each select="a/x/y">
            <h3>
              <xsl:value-of select="@testname" />
            </h3>
            <h4>Heading 1</h4>
            <table border="1" style="display:inline">
              <tr bgcolor="#CECEF6">
                <th align="center">Item1</th>
                <th align="center">Item2</th>
                <th align="center">Item3</th>
              </tr>
              <xsl:for-each select="object">
                <tr>
                  <td bgcolor="#F2F5A9">
                    <xsl:value-of select="@elementname" />
                  </td>
                  <td bgcolor="#F2F5A9">
                    <xsl:value-of select="@elementvalue1" />
                  </td>
                  <td bgcolor="#F2F5A9">
                    <xsl:value-of select="@elementvalue2" />
                  </td>

                </tr>
              </xsl:for-each>
            </table>
          </xsl:for-each>
        </font>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

我只是得到一个空白页。 我知道这里有很多东西要消化,但我真的卡住了!

谢谢

【问题讨论】:

  • 您的样式表格式不正确...
  • 您的 xml 格式也不正确。让你的 x 和 y 标签匹配。
  • 我可以在 XML 中看到错误,但不确定 XSLT 哪里出错了..

标签: xml xslt dom xml-parsing


【解决方案1】:

您的 &lt;xsl:for-each select="a/x/t"&gt; 与您的文档结构不匹配 - 它应该是 &lt;xsl:for-each select="a/t"&gt; ...但您嵌套的 for-each 会出现更多问题。

使用模板可以更好地实现您想要实现的目标。稍后我会添加一个示例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-20
    • 2021-03-21
    • 2010-09-24
    • 1970-01-01
    • 2017-11-06
    • 2021-10-04
    相关资源
    最近更新 更多