2

我的问题是我的样式表没有正确处理我的 xml 文件。如果注释掉Machine-tag 的模板,我会看到我的错误表,如果我有我在此处发布的样式表,我只会看到生成的 Machine-tag 链接,但没有任何错误。

我有以下 XML 源文件:

XML:

<Machine HtmlUri="http://stackoverflow.com" Name="XY1">
  <Errors Count="2">
    <Error>
      <TimeStamp>2013-11-12T07:27:24.9766747+01:00</TimeStamp>
      <Machine>XY1</Machine>
      <Message> ... </Message>
      <InnerException />
      <StackTrace> ... </StackTrace>
    </Error>
    <Error>
      <TimeStamp>2013-11-12T07:27:24.9766747+01:00</TimeStamp>
      <Machine>XY1</Machine>
      <Message> ... </Message>
      <InnerException />
      <StackTrace> ... </StackTrace>
    </Error>
  </Errors>
</Machine>

我有这个样式表。我对 xslt 不是很熟悉,我对w3school.com等网站的所有研究都帮不了我。

XSLT:

<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:output method="html" indent="yes"/>
  <xsl:template match="/">
    <html>
      <meta http-equiv="content-type" content="text/html; charset=utf-8" />
      <body>
        <h2>Status File</h2>
        <xsl:apply-templates />
      </body>
    </html>
  </xsl:template>

  <xsl:template match="Machine">
    <h5>Machine Information:</h5>
    <div>
      <a href="{@HtmlUri}" target="_blank">
        <xsl:apply-templates select="@HtmlUri" />
      </a>
    </div>
  </xsl:template>

  <xsl:template match="Errors">
    <h5>Errors:</h5>
    <div>
      <table border="1">
        <tr bgcolor="#dd0000">
          <th>Machine</th>
          <th>TimeStamp</th>
          <th>Message</th>
        </tr>
        <xsl:for-each select="Error">
          <tr bgcolor="ff0000">
            <td>
              <xsl:value-of select="./Machine"/>
            </td>
            <td>
              <xsl:value-of select="./TimeStamp"/>
            </td>
            <td>
              <xsl:value-of select="./Message"/>
            </td>
          </tr>
        </xsl:for-each>
      </table>
    </div>
  </xsl:template>

</xsl:stylesheet>
4

1 回答 1

1

根据一些有用的评论,我解决了我的问题。

因此我需要一个非常通用的 XSLT 模板,我决定在我的模板中应用其他模板Machine

那是我现在工作的XSLT

  [...]
  <xsl:template match="Machine">
    <h5>Machine Information:</h5>
    <div>
      <a href="{@HtmlUri}" target="_blank">
        <xsl:apply-templates select="@HtmlUri" />
      </a>
    </div>
    <xsl:apply-templates />
  </xsl:template>
  [...]
于 2013-11-12T07:32:35.180 回答