使用XSLT将XML转换为HTML

时间:2014-07-18 12:50:19

标签: xml xslt

我有一个xml文件,它包含除第一个元素之外的某些元素的嵌套。 xml看起来像这样:

    <?xml version="1.0" encoding="utf-8" standalone="no"?>
<!--This file represents the results of running a test suite-->
<test-results name="value" total="5" errors="0" failures="5" not-run="0" inconclusive="0" ignored="0" skipped="0" invalid="0" date="2014-07-18" time="17:09:15">
  <environment nunit-version="2.6.3.13283" clr-version="2.0.50727.5420" os-version="Microsoft Windows NT 6.1.7601 Service Pack 1" platform="Win32NT" cwd="value" machine-name="value" user="value" user-domain="value" />
  <culture-info current-culture="en-US" current-uiculture="en-US" />
  <test-suite type="Assembly" name="value" executed="True" result="Failure" success="False" time="1.863" asserts="0">
    <results>
      <test-suite type="Namespace" name="value" executed="True" result="Failure" success="False" time="1.849" asserts="0">
        <results>
          <test-suite type="Namespace" name="value" executed="True" result="Failure" success="False" time="1.848" asserts="0">
            <results>
              <test-suite type="Namespace" name="value" executed="True" result="Failure" success="False" time="1.848" asserts="0">
                <results>
                  <test-suite type="Namespace" name="Tests" executed="True" result="Failure" success="False" time="1.848" asserts="0">
                    <results>
                      <test-suite type="Namespace" name="value" executed="True" result="Failure" success="False" time="1.848" asserts="0">
                        <results>
                          <test-suite type="TestFixture" name="value" executed="True" result="Error" success="False" time="1.570" asserts="0">
                            <failure>
                              <message><![CDATA[SetUp : Svalue]]></message>
                              <stack-trace><![CDATA[value]]></stack-trace>
                            </failure>
                            <results>
                              <test-case name="value" executed="True" result="Failure" success="False" time="0.000" asserts="0">
                                <failure>
                                  <message><![CDATA[value]]></message>
                                  <stack-trace />
                                </failure>
                              </test-case>
                            </results>
                          </test-suite>
                          <test-suite type="ParameterizedFixture" name="Svalue" executed="True" result="Failure" success="False" time="0.008" asserts="0">
                            <results>
                              <test-suite type="TestFixture" name="Svalue" executed="True" result="Error" success="False" time="0.004" asserts="0">
                                <failure>
                                  <message><![CDATA[value]]></message>
                                  <stack-trace><![CDATA[value]]></stack-trace>
                                </failure>
                                <results>
                                  <test-case name="value" executed="True" result="Failure" success="False" time="0.000" asserts="0">
                                    <failure>
                                      <message><![CDATA[value]]></message>
                                      <stack-trace />
                                    </failure>
                                  </test-case>
                                </results>
                              </test-suite>
                            </results>
                          </test-suite>
                        </results>
                      </test-suite>
                    </results>
                  </test-suite>
                </results>
              </test-suite>
            </results>
          </test-suite>
        </results>
      </test-suite>
    </results>
  </test-suite>
</test-results>

这是我的xml。这是我试图使用的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:output method="xml" indent="yes"/>

    <xsl:template match="/">
      <html>
        <body>
          <table border="1">
            <tr>
              <th>Test Method</th>
              <th>Execution Time</th>
              <th>Asserts Performed</th>
              <th>Success</th>
              <th>Result</th>
              <th>Executed</th>
              <th>Failure Message</th>
              <th>ExceptionMessage</th>
              <th>Stack Trace</th>
            </tr>
            <xsl:for-each select="test-results/test-suite">
                <xsl:for-each select="//results/descendant::test-suite[last()]">
                    <tr>
                        <td><xsl:value-of select="./results/test-case/@name"/></td>
                        <td><xsl:value-of select="./results/test-case/@time"/></td>
                        <td><xsl:value-of select="./results/test-case/@asserts"/></td>
                        <td><xsl:value-of select="./results/test-case/@success"/></td>
                        <td><xsl:value-of select="./results/test-case/@result"/></td>
                        <td><xsl:value-of select="./results/test-case/@executed"/></td>
                        <td><xsl:value-of select="./results/test-case/failure/message/text()" disable-output-escaping="no"/></td>
                        <td><xsl:value-of select="./failure/message/text()" disable-output-escaping="no"/></td>
                        <td><xsl:value-of select="./failure/stack-trace/text()" disable-output-escaping="no"/></td>
                    </tr>
                </xsl:for-each>
            </xsl:for-each>
          </table>
        </body>
      </html>

    </xsl:template>
</xsl:stylesheet>

因此,您可以在此处看到,测试套件/结果对的嵌套在两种情况下都不同。这样我可能有多个案例。但有一点可以肯定标签不会改变。这种模式将得到维护。那么,我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

由于您在编辑中提供的输入XML文件在结构上与您之前的内容完全不同而且您没有发布,因此我仍然不太确定您要尝试实现的目标。你想要得到的输出的例子,但也许你在这样的事情之后呢?

样式表

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0" exclude-result-prefixes="msxsl"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:msxsl="urn:schemas-microsoft-com:xslt">

  <xsl:output method="xml" indent="yes"/>

  <xsl:strip-space elements="*"/>

  <xsl:template match="/">
    <html>
      <body>
        <table border="1">
          <tr>
            <th>Test Method</th>
            <th>Execution Time</th>
            <th>Asserts Performed</th>
            <th>Success</th>
            <th>Result</th>
            <th>Executed</th>
            <th>Failure Message</th>
            <th>ExceptionMessage</th>
            <th>Stack Trace</th>
          </tr>

          <!--
          Apply all <test-suite> descendants with a <result> child that has a
          <test-case> child
          -->
          <xsl:apply-templates select="descendant::test-suite[results/test-case]"/>
        </table>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="test-suite[results/test-case]">
    <tr>
      <xsl:apply-templates select="results/test-case"/>
      <xsl:apply-templates select="failure"/>
    </tr>
  </xsl:template>

  <xsl:template match="test-case">
    <xsl:apply-templates select="@name"/>
    <xsl:apply-templates select="@time"/>
    <xsl:apply-templates select="@asserts"/>
    <xsl:apply-templates select="@success"/>
    <xsl:apply-templates select="@result"/>
    <xsl:apply-templates select="@executed"/>
    <xsl:apply-templates select="failure/message"/>
  </xsl:template>

  <xsl:template match="test-case/@*">
    <td>
      <xsl:value-of select="."/>
    </td>
  </xsl:template>

  <xsl:template match="failure">
    <xsl:apply-templates select="message | stack-trace"/>
  </xsl:template>

  <xsl:template match="message | stack-trace">
    <td>
      <xsl:value-of select="text()"/>
    </td>
  </xsl:template>
</xsl:stylesheet>

输出

<?xml version="1.0"?>
<html>
  <body>
    <table border="1">
      <tr>
        <th>Test Method</th>
        <th>Execution Time</th>
        <th>Asserts Performed</th>
        <th>Success</th>
        <th>Result</th>
        <th>Executed</th>
        <th>Failure Message</th>
        <th>ExceptionMessage</th>
        <th>Stack Trace</th>
      </tr>
      <tr>
        <td>value</td>
        <td>0.000</td>
        <td>0</td>
        <td>False</td>
        <td>Failure</td>
        <td>True</td>
        <td>value</td>
        <td>SetUp : Svalue</td>
        <td>value</td>
      </tr>
      <tr>
        <td>value</td>
        <td>0.000</td>
        <td>0</td>
        <td>False</td>
        <td>Failure</td>
        <td>True</td>
        <td>value</td>
        <td>value</td>
        <td>value</td>
      </tr>
    </table>
  </body>
</html>