如何将条件应用于xsl键

时间:2015-02-09 21:09:28

标签: xml xslt

我试图通过xslt显示xml数据。 我想将输出显示如下

// 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}

描述是应用程序数据文本,级别是子类型名称和活动名称将是描述。此活动名称将相同,直到级别值开始。我必须在活动名称下重复相同的描述值,直到你在级别列中得到另一个开始。

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>

1 个答案:

答案 0 :(得分:1)

我只是在这里猜测,但我认为你想做的只是:

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>

应用于您的输入示例,这将返回:

enter image description here