Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
114 views
in Technique[技术] by (71.8m points)

xml - how to get preceding-sibling value when i have name space on the root elements?

With reference to my previous question how to get preceding-sibling value when i have name space on the root elements? I also have some requirements to follow. The output suppose to be like this based on the requirements. I am getting blank Activity Name.

// Description Level        Activity Name  Activity ID
// =========== ============ ============= ======================================
// Main Entry   Start        Main Entry   {00000000-0000-0000-0000-000000000000}
// Hello World! Information  Main Entry   {00000000-0000-0000-0000-000000000000}
// Test5        Verbose                   {3b081a36-43d5-4ecc-b381-628c33316205}    
// alpha        Start        alpha        {aa5a5b9c-4b24-43af-9f49-32656385e17d}    
// Test7        Error        alpha        {aa5a5b9c-4b24-43af-9f49-32656385e17d}    
// Test1        Information               {d30741c2-da73-434a-8f0d-101f7ceb2228}    
// Test11       Transfer     Main Entry   {00000000-0000-0000-0000-000000000000}

Requirements

  • 1st record, there is no start for this guid so activity name will be empty
  • 2nd record, it is a start level so gets it's own description
  • 3rd record, the guid has a Start level so get's that Activity Name
  • 4th record, there is no start for this guid so activity name will be empty
  • 5th record, a new start level get's its own description
  • 6th record, the guid has a Start level so get's that Activity Name
  • 7th record, the guid has a Start level so get's that Activity Name

XML

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="sample.xsl" ?>
<root>
<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>Main Entry </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>Hello World!</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>Test5</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>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="Error">0</SubType>
        <Correlation ActivityID="aa5a5b9c-4b24-43af-9f49-32656385e17d" />
    </System>
    <ApplicationData>Test7 </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>Test1</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>Test11</ApplicationData>
</E2ETraceEvent>    
</root>

XSL

<?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::E2ETraceEvent)[System/SubType/@Name='Start'])[last()]/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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This part of your code does not use the required prefixes when addressing the elements in the source XML - therefore nothing is selected:

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

it should probably look more like this:

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

I say probably, because the result received after fixing this does not match the result you show us at the beginning of your post. But that's a problem that was (or should have been) solved in your previous question.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...