Unable to XSLT transform an trx file (MS LoadT

2019-09-09 23:01发布

问题:

I am trying to transform a TRX file(XML) into HTML using XSLT, but can not get it to transform - at all

This is a mini version of my TRX/XML file

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="style.xslt" type="text/xslt"?>
<TestRun id="5d28f629-b34e-428c-9486-46368f6a6211" name="WIN8-VM-X 2014-09-15 10:15:32" runUser="WIN8-VM-X\jd" xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010">
   <Results>
    <LoadTestResult executionId="bc475dd2-93d9-4cb5-ac3d-6b088615b8a8" testId="5d7e6f36-158f-4167-aeb3-6f9e5a5d021d" testName="LoadTest1" computerName="WIN8-VM-X" duration="00:02:58.3848158" startTime="2014-09-15T10:15:38.7219789+02:00" endTime="2014-09-15T10:18:37.1067947+02:00" testType="fa4c85ff-1236-4463-b954-3ba2f0eabd95" outcome="Completed" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" relativeResultsDirectory="bc475dd2-93d9-4cb5-ac3d-6b088615b8a8" runId="11" controllerStartTime="2014-09-15T10:15:38.7219789+02:00" runStatus="Stopped" resultsStoreType="Database" resultsRepositoryConnectString="LAs4ogoKlDd+VpAl2ugknpXtkwYcrJ4K4xho6CJjXD1Qv6o/LM9aSaAM4Ac5r6Taj3snulskUGTQzbVVXe9oT7r6RKtPoK7hv4F5g28RXiKSn9lO+5p/Kw==" loadTestDuration="600" loadTestWarmupTime="0" previouslyViewed="false">
      <Summary>
        <PerformanceCounterResults>
          <PerformanceCounterResult isOverallResultCounter="true" higherIsBetter="true" machineName="WIN8-VM-X" categoryName="LoadTest:Scenario" counterName="User Load" instanceName="_Total" value="1" />
          <PerformanceCounterResult isOverallResultCounter="true" higherIsBetter="true" machineName="WIN8-VM-X" categoryName="LoadTest:Test" counterName="Total Tests" instanceName="_Total" value="10" />
          <PerformanceCounterResult isOverallResultCounter="true" higherIsBetter="false" machineName="WIN8-VM-X" categoryName="LoadTest:Test" counterName="Failed Tests" instanceName="_Total" value="9" />
          <PerformanceCounterResult isOverallResultCounter="true" higherIsBetter="true" machineName="WIN8-VM-X" categoryName="LoadTest:Test" counterName="Tests/Sec" instanceName="_Total" value="0.0560585819" />
          <PerformanceCounterResult isOverallResultCounter="true" higherIsBetter="false" machineName="WIN8-VM-X" categoryName="LoadTest:Test" counterName="Avg. Test Time" instanceName="_Total" value="13.0216" />
          <PerformanceCounterResult isOverallResultCounter="true" higherIsBetter="true" machineName="WIN8-VM-X" categoryName="LoadTest:Request" counterName="Total Requests" instanceName="_Total" value="130" />
          <PerformanceCounterResult isOverallResultCounter="true" higherIsBetter="false" machineName="WIN8-VM-X" categoryName="LoadTest:Request" counterName="Failed Requests" instanceName="_Total" value="18" />
          <PerformanceCounterResult isOverallResultCounter="true" higherIsBetter="true" machineName="WIN8-VM-X" categoryName="LoadTest:Request" counterName="Cached Requests" instanceName="_Total" value="0" />
          <PerformanceCounterResult isOverallResultCounter="true" higherIsBetter="true" machineName="WIN8-VM-X" categoryName="LoadTest:Request" counterName="Requests/Sec" instanceName="_Total" value="0.728761554" />
          <PerformanceCounterResult isOverallResultCounter="true" higherIsBetter="false" machineName="WIN8-VM-X" categoryName="LoadTest:Request" counterName="Avg. Response Time" instanceName="_Total" value="0.9909615" />
          <PerformanceCounterResult isOverallResultCounter="false" higherIsBetter="false" machineName="WIN8-VM-X" categoryName="Processor" counterName="% Processor Time" instanceName="_Total" value="43.6383438" />
          <PerformanceCounterResult isOverallResultCounter="false" higherIsBetter="true" machineName="WIN8-VM-X" categoryName="Memory" counterName="Available MBytes" instanceName="-" value="214" />
          <PerformanceCounterResult isOverallResultCounter="false" higherIsBetter="false" machineName="WIN8-VM-X" categoryName="Network Interface" counterName="Bytes Received/sec" instanceName="local area connection* 13" value="0" />
          <PerformanceCounterResult isOverallResultCounter="false" higherIsBetter="false" machineName="WIN8-VM-X" categoryName="Network Interface" counterName="Bytes Received/sec" instanceName="intel[r] 82574l gigabit network connection" value="5012.82275" />
          <PerformanceCounterResult isOverallResultCounter="false" higherIsBetter="false" machineName="WIN8-VM-X" categoryName="Network Interface" counterName="Bytes Received/sec" instanceName="isatap.localdomain" value="0" />
          <PerformanceCounterResult isOverallResultCounter="false" higherIsBetter="false" machineName="WIN8-VM-X" categoryName="Network Interface" counterName="Bytes Sent/sec" instanceName="local area connection* 13" value="6.840497" />
          <PerformanceCounterResult isOverallResultCounter="false" higherIsBetter="false" machineName="WIN8-VM-X" categoryName="Network Interface" counterName="Bytes Sent/sec" instanceName="intel[r] 82574l gigabit network connection" value="1746.74634" />
          <PerformanceCounterResult isOverallResultCounter="false" higherIsBetter="false" machineName="WIN8-VM-X" categoryName="Network Interface" counterName="Bytes Sent/sec" instanceName="isatap.localdomain" value="0" />
        </PerformanceCounterResults>
        <TestSummaries>
          <TestSummary scenarioName="Scenario1" testName="Testing" totalTests="10" testsFailed="9" averageDuration="13021" />
        </TestSummaries>
        <TransactionSummaries>
          <TransactionSummary scenarioName="Scenario1" testName="Testing" transactionName="Testing.TestingCoded" transactionCount="10" elapsedTime="6772" responseTime="6772" />
          <TransactionSummary scenarioName="Scenario1" testName="Testing" transactionName="Testing.TestingPartTwo" transactionCount="10" elapsedTime="6109" responseTime="6109" />
        </TransactionSummaries>
      </Summary>
    </LoadTestResult>
  </Results>
</TestRun>

And this is my XSLT file

<?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="html" indent="yes" />
  <xsl:template match="/TestRun/Results">
    <html>
      <head>
    <title>Test Results</title>
    </head>
      <body>
        <table>
          <xsl:for-each select="/LoadTestResult/Summary/PerformanceCounterResults/PerformanceCounterResult">
            <tr>
              <td>
                <xsl:value-of select="@counterName" />
              </td>
              <td>
                <xsl:value-of select="@value" />
              </td>
            </tr>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

Can anyone spot the error in the XSLT?

回答1:

The problem is with namespaces. Your XML looks like this at the start...

<TestRun xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010" ....

This means the TestRun element, and all its descendants, belong to that namespace. However, there is no sign of the namespace in your XSLT. Therefore, when you do this...

<xsl:template match="/TestRun/Results">

It is look for elements with no namespace, and such elements are different to ones in a namespace. Therefore the match is not matched.

The solution is to declare the namespace in the XSLT, and prefix all relevant elements in matching expressions with it.

Try this 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"
                xmlns:tt="http://microsoft.com/schemas/VisualStudio/TeamTest/2010">
  <xsl:output method="html" indent="yes" />
  <xsl:template match="/tt:TestRun/tt:Results">
    <html>
      <head>
    <title>Test Results</title>
    </head>
      <body>
        <table>
          <xsl:for-each select="tt:LoadTestResult/tt:Summary/tt:PerformanceCounterResults/tt:PerformanceCounterResult">
            <tr>
              <td>
                <xsl:value-of select="@counterName" />
              </td>
              <td>
                <xsl:value-of select="@value" />
              </td>
            </tr>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

Also note there was an error in your xsl:for-each. You did this...

But the initial / represents the document node, so it would look for the root element called LoadTestResult. You need to drop the / to make it a relative expression.



标签: xml xslt