在XSLT中实现查找

时间:2015-02-23 03:06:20

标签: xml xslt

在下方运行,并在底部输出输出。手动添加黑色背景 活动更正名称的列,以提供活动名称列的所需输出,此列此时会生成错误的值。我想实现以下逻辑。

  

伪;

     
      
  • 选择记录其中 级别 ==开始      
        
    • 设置 活动名称 = 说明
    •   
    • 捕获 ActivityId ActivityName 进入内存以供将来查找
    •   
  •   
  • 根据 ActivityId 匹配的捕获记录设置每条记录的ActivityName
  •   

您也可以在.NET Fiddle

找到C#的完整逻辑

XML

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="test.xsl" ?>
<root>
<E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent">
   <System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system">
        <SubType Name="Information">0</SubType>
        <Correlation ActivityID="a54221e2-ad37-434a-8f0d-101f7abc2221" />
    </System>
    <ApplicationData>Test21</ApplicationData>
</E2ETraceEvent>
<E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent">
   <System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system">
        <SubType Name="Information">0</SubType>
        <Correlation ActivityID="d30741c2-da73-434a-8f0d-101f7ceb2228" />
    </System>
    <ApplicationData>Test24</ApplicationData>
</E2ETraceEvent>
<E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent">
    <System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system">
        <SubType Name="Start">0</SubType>
        <Correlation ActivityID="00000000-0000-0000-0000-000000000000" />
    </System>
    <ApplicationData>Alpha</ApplicationData>
</E2ETraceEvent>
<E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent">
    <System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system">
        <SubType Name="Information">0</SubType>
        <Correlation ActivityID="00000000-0000-0000-0000-000000000000" />
    </System>
    <ApplicationData>Test31</ApplicationData>
</E2ETraceEvent>
<E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent">
    <System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system">
        <SubType Name="Verbose">0</SubType>
        <Correlation ActivityID="3b081a36-43d5-4ecc-b381-628c33316205" />
    </System>
    <ApplicationData>Test51</ApplicationData>
</E2ETraceEvent>
<E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent">
    <System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system">
        <SubType Name="Start">0</SubType>
        <Correlation ActivityID="aa5a5b9c-4b24-43af-9f49-32656385e17d" />
    </System>
    <ApplicationData>Bravo</ApplicationData>
</E2ETraceEvent>
<E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent">
   <System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system">
        <SubType Name="Start">0</SubType>
        <Correlation ActivityID="d30741c2-da73-434a-8f0d-101f7ceb2228" />
    </System>
    <ApplicationData>Charlie</ApplicationData>
</E2ETraceEvent>
<E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent">
    <System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system">
        <SubType Name="Error">0</SubType>
        <Correlation ActivityID="aa5a5b9c-4b24-43af-9f49-32656385e17d" />
    </System>
    <ApplicationData>Test71</ApplicationData>
</E2ETraceEvent>
<E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent">
   <System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system">
        <SubType Name="Transfer">0</SubType>
        <Correlation ActivityID="00000000-0000-0000-0000-000000000000" />
    </System>
    <ApplicationData>Test41</ApplicationData>
</E2ETraceEvent>    
</root>

XSLT

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

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:te="http://schemas.microsoft.com/2004/06/E2ETraceEvent"
                xmlns:s="http://schemas.microsoft.com/2004/06/windows/eventlog/system"
                xmlns:sd="http://schemas.microsoft.com/2004/08/System.Diagnostics"
                xmlns:tr="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord"
                exclude-result-prefixes="te s sd tr">
    <xsl:output method="html" indent="no"/>

    <xsl:template match="/">
        <html>
            <head>                  
            </head>
            <body>
                <div>
                    <div>
                    <table>
                        <thead>
                            <tr>                                
                                <th>Description</th>
                                <th>Level</th>
                                <th>Activity Name</th>
                                <th>Activity ID</th>
                            </tr>
                        </thead>
                        <tbody>                     
                            <xsl:for-each select="//te:E2ETraceEvent">

                            <xsl:variable name="level">
                                <xsl:value-of select=".//s:SubType/@Name"/>
                            </xsl:variable>

                            <xsl:variable name="description">
                                <xsl:value-of select=".//te:ApplicationData/text()"/>
                            </xsl:variable>

                            <tr>
                                <!-- APPLICATION DATA -->
                                <td>
                                    <xsl:value-of select="$description"/>
                                </td>

                                <!-- LEVEL -->
                                <td>
                                    <xsl:value-of select="$level"/>                                     
                                </td>

                                <!-- ACTIVITY NAME -->
                                <td>
                                    <xsl:value-of select="((. | preceding-sibling::te:E2ETraceEvent)[s:System/s:SubType/@Name='Start'])[last()]/te:ApplicationData"/>
                                </td>

                                <!-- ACTIVITY ID -->
                                <td>
                                    <xsl:value-of select=".//s:Correlation/@ActivityID"/>                                       
                                </td>
                            </tr>
                        </xsl:for-each>
                        </tbody>
                    </table>
                    </div>
                </div>
            </body>
        </html>
    </xsl:template> 
</xsl:stylesheet>

输出

result

1 个答案:

答案 0 :(得分:0)

您的伪代码有点神秘(就像没有记录,其中Level == Start )。此外,XSLT不会那样工作。在XSLT中,使用key完成查找。

尝试将其添加到样式表的顶层:

<xsl:key name="start" match="te:E2ETraceEvent[s:System/s:SubType/@Name='Start']" use="s:System/s:Correlation/@ActivityID" />

然后将此列添加到您的表中:

<td>
    <xsl:value-of select="key('start', s:System/s:Correlation/@ActivityID)/te:ApplicationData"/>    
</td>