6

我正在使用 XSL 读取三个相同的 XML 文档,除了它们的属性具有不同的值(我可能会读取许多 XML 文件)。我想计算“testName”属性=“TestOne”的元素的“结果”属性值“通过”或“失败”的次数。我正在使用以下方法实现此目的:

文件1.xml

<container>
    <build>
        <Tests>
            <Results>
                <Result testName="TestOne" outcome="Passed" ></Result>
                <Result testName="TestTwo"  outcome="Passed" ></Result>
            </Results>
        </Tests>    
    </build>
</container>

文件2.xml

<container>
    <build>
        <Tests>
            <Results>
                <Result testName="TestOne" outcome="Passed" ></Result>
                <Result testName="TestTwo"  outcome="Failed" ></Result>
            </Results>
        </Tests>    
    </build>
</container>

文件3.xml

<container>
    <build>
        <Tests>
            <Results>
                <Result testName="TestOne" outcome="Failed" ></Result>
                <Result testName="TestTwo"  outcome="Failed" ></Result>
            </Results>
        </Tests>    
    </build>
</container>

索引.xml

<?xml-stylesheet type="text/xsl" href="merge3.xsl"?>
<list>
    <entry name="File1.xml" />
    <entry name="File2.xml" />
    <entry name="File3.xml" />
</list>

合并2.xsl

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>

<xsl:template match="/">
    <xsl:for-each select="/list/entry">
        <xsl:apply-templates select="document(@name)/container/build/*[local-name()='Tests']" />
    </xsl:for-each>
</xsl:template>

<xsl:template match="*[local-name()='Results']">
    <xsl:variable name="name" select="'TestOne'" />
    <xsl:variable name="totalPassed" select="*[local-name()='Result'][@testName = 'TestOne'][@outcome = 'Passed']" />
    <xsl:variable name="totalFailed" select="*[local-name()='Result'][@testName = 'TestOne'][@outcome = 'Failed']" />

    <h2>Totals</h2>
    <table border="1" cellSpacing="0" cellPadding="5" >
        <tr bgcolor="#9acd32">
            <th>Test Name</th>
            <th>Total Passed</th>
            <th>Total Failed</th>
        </tr>
        <tr>
            <td><xsl:value-of select="$name"/></td>
            <td><xsl:value-of select="count($totalPassed)"/></td>
            <td><xsl:value-of select="count($totalFailed)"/></td>
        </tr>
    </table>
</xsl:template>

</xsl:stylesheet>

结果生成 3 个总计表。我的意图是显示一个 Totals 表,显示 TestOne 在所有 XML 文档中通过和失败的次数。似乎每个 XML 文档都被读取/选择,然后一次处理一个。我想在处理它们之前阅读并选择所有 XML 文件。

4

2 回答 2

9

You could gather all the Result elements from across the three documents using something like

<xsl:variable name="allResults"
  select="(/ | document('file2.xml') | document('file3.xml'))//Result" />

and then apply predicates to this to count the elements you're interested in, for example

<xsl:value-of select="
  count($allResults[@testName = 'TestOne'][@outcome = 'Failed'])" />

Instead of a fixed set of file names, if you have a main index.xml that lists all the files you want to combine, for example:

<list>
    <entry name="File1.xml" />
    <entry name="File2.xml" />
    <entry name="File3.xml" />
</list>

then you can use this index as the main input to your stylesheet and the allResults variable becomes:

<xsl:variable name="allResults"
  select="document(/list/entry/@name)//Result" />

When you pass a node set to the document function it takes the string value of each node in turn and treats that as the URI of a file to load, returning the resulting set of document root nodes.

Here's a complete example

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html"/>

  <xsl:variable name="allResults"
    select="document(/list/entry/@name)//Result" />

  <xsl:template match="/">
    <xsl:variable name="name" select="'TestOne'" />

    <h2>Totals</h2>
    <table border="1" cellSpacing="0" cellPadding="5" >
        <tr bgcolor="#9acd32">
            <th>Test Name</th>
            <th>Total Passed</th>
            <th>Total Failed</th>
        </tr>
        <tr>
            <td><xsl:value-of select="$name"/></td>
            <td><xsl:value-of select="count($allResults[@testName = $name]
                                              [@outcome = 'Passed'])"/></td>
            <td><xsl:value-of select="count($allResults[@testName = $name]
                                              [@outcome = 'Failed'])"/></td>
        </tr>
    </table>
  </xsl:template>

</xsl:stylesheet>
于 2013-08-31T16:25:27.307 回答
0

这与上面几乎相同,但没有变量。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
    <h2>Totals</h2>
    <table border="1" cellSpacing="0" cellPadding="5" >
        <tr bgcolor="#9acd32">
            <th>Test Name</th>
            <th>Total Passed</th>
            <th>Total Failed</th>
        </tr>
        <xsl:for-each select="/list/entry">
          <tr>
            <td><xsl:value-of select="@name"/></td>
            <td><xsl:value-of select="count(document(@name)/container/build/Tests/Results/Result[@outcome eq 'Passed'])"/></td>
            <td><xsl:value-of select="count(document(@name)/container/build/Tests/Results/Result[@outcome eq 'Failed'])"/></td>
          </tr>
        </xsl:for-each>
    </table>
</xsl:template>

</xsl:stylesheet>
于 2013-09-01T13:18:02.303 回答