Kash
Kash

Reputation: 15

how to apply condition to xsl key

I am trying to display xml data through xslt. I would like to show the output as follow

// Description  Level         ActivityName ActivityID
// ===========  ============  ============ =======================
// Main Entry   Start         Main Entry   {00000000-0000-0000-0000-000000000000}
// Hello World  Information   Main Entry   {00000000-0000-0000-0000-000000000000}
// alpha        Start         alpha        {aa5a5b9c-4b24-43af-9f49-32656385e17d}
// Hello world  error         alpha        {aa5a5b9c-4b24-43af-9f49-32656385e17d}
// Hello world  Transfer      alpha        {00000000-0000-0000-0000-000000000000}

Description is the application data text and the level is Sub Type Name and activity Name will be the description. This activity name will be same until the level value is start. I have to repeat the same description value under activity name till you get an other start in level column.

XML

<root>
<E2ETraceEvent>
    <System>
        <SubType Name="Start">0</SubType>
        <Correlation ActivityID="00000000-0000-0000-0000-000000000000" />
    </System>
    <ApplicationData>Main Entry </ApplicationData>
</E2ETraceEvent>
<E2ETraceEvent>
    <System>
        <SubType Name="Information">0</SubType>
        <Correlation ActivityID="00000000-0000-0000-0000-000000000000" />
    </System>
    <ApplicationData>Hello World!</ApplicationData>
</E2ETraceEvent>
<E2ETraceEvent>
    <System>
        <SubType Name="Start">0</SubType>
        <Correlation ActivityID="aa5a5b9c-4b24-43af-9f49-32656385e17d" />
    </System>
    <ApplicationData>alpha </ApplicationData>
</E2ETraceEvent>
<E2ETraceEvent>
    <System >
        <SubType Name="Error">0</SubType>
        <Correlation ActivityID="00000000-0000-0000-0000-000000000000" />
    </System>
    <ApplicationData>Hello World!</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" >
  <xsl:output method="html" indent="no"/>
  <xsl:template match="/">
    <html>
      <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="/E2ETraceEvent">
                  <xsl:variable name="level_key" select="/E2ETraceEvent[contains(key,'kSubtypeName')]/value"/>
                  <xsl:variable name="level">
                    <xsl:value-of select="./SubType/@Name"/>
                  </xsl:variable>
                  <xsl:variable name="activityID">
                    <xsl:value-of select="./Correlation/@ActivityID" />
                  </xsl:variable>
                  <xsl:variable name="description">
                    <xsl:value-of select="./ApplicationData/text()" />
                  </xsl:variable>
                  <tr>
                    <td>
                      <xsl:value-of select="$description"/>
                    </td>
                    <td>
                      <xsl:value-of select="$level"/>
                    </td>
                    <td>
                      <xsl:choose>
                        <xsl:when test="$level = 'Start'">
                          <xsl:value-of select="$description"/>
                        </xsl:when>
                        <xsl:otherwise>
                          <xsl:value-of select="$description" />
                        </xsl:otherwise>
                      </xsl:choose>
                    </td>
                    <td>
                      <xsl:value-of select="$activityID"/>
                    </td>
                  </tr>
                </xsl:for-each>
              </tbody>
            </table>
          </div>
        </div>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

Upvotes: 0

Views: 294

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 116993

I am only guessing here, but I think you want to do simply:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:template match="/root">
    <table border="1">
        <thead>
            <th>Description</th>
            <th>Level</th>
            <th>Activity Name</th>
            <th>Activity ID</th>
        </thead>
        <tbody>
            <xsl:for-each select="E2ETraceEvent">
                <tr>
                    <td><xsl:value-of select="ApplicationData"/></td>
                    <td><xsl:value-of select="System/SubType/@Name"/></td>
                    <td>
                        <!-- description from the last 'Start' - including the current node -->
                        <xsl:value-of select="((. | preceding-sibling::E2ETraceEvent)[System/SubType/@Name='Start'])[last()]/ApplicationData"/>
                    </td>
                    <td><xsl:value-of select="System/Correlation/@ActivityID"/></td>
                </tr>
            </xsl:for-each>
        </tbody>
    </table>
</xsl:template>

</xsl:stylesheet>

Applied to your input example, this would return:

enter image description here

Upvotes: 1

Related Questions