Положение XSLT ()

У меня есть следующие данные XML, и обрабатываю HTML с помощью процессора XSLT.

<?xml version="1.0" encoding="utf-16"?>
<ScorecardSummary>
  <DivisionSummary>
    <DivisionName>
      <string> SYSTEM</string>
    </DivisionName>
    <ScorecardSummaryByDivision>
      <ScorecardSummaryByKPI>
        <Header>
          <string>Committed Time of Arrival</string>
          <string>Goal</string>
          <string>1D</string>
          <string>7D</string>
          <string>QTD</string>
          <string>YTD</string>
          <string>YTD Event Cars</string>
        </Header>
        <Data>
          <ScorecardContract>
            <TypeName>System</TypeName>
            <Goal>68</Goal>
            <GoalWarning>64.6</GoalWarning>
            <TotalCountYear>1234</TotalCountYear>
            <Value1D>79</Value1D>
            <Value7D>79.2</Value7D>
            <ValueQTD>79.1</ValueQTD>
            <ValueYTD>73.3</ValueYTD>
          </ScorecardContract>
          <ScorecardContract> 
             <!-- more data -->
          </ScorecardContract>
       </Data>
      </ScorecardSummaryByKPI>
      <ScorecardSummaryByKPI>
         <!-- more data -->
     </ScorecardSummaryByKPI>
    </ScorecardSummaryByDivision>
  </DivisionSummary>
</ScorecardSummary>

Я создаю таблицы в соответствии с разделом "Данные" части ScorecardSummaryByKPI, а заголовки таблиц соответствуют разделу "Заголовок" части ScorecardSummaryByKPI.

Вот XSL:

<?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"/>

<xsl:template match="/">
  <html>
    <body>
        <xsl:apply-templates select="ScorecardSummary/DivisionSummary">
        </xsl:apply-templates>
    </body>
  </html>
</xsl:template>

<xsl:template match="ScorecardSummary/DivisionSummary/DivisionName">
  <h1>
    <xsl:value-of select="current()/string"/>
  </h1>
</xsl:template>

<xsl:template match="ScorecardSummaryByDivision">
  <xsl:apply-templates select="current()/ScorecardSummaryByKPI"/>
</xsl:template>

<xsl:template match="ScorecardSummaryByKPI">
  <table border="1" cellspacing="0" cellpadding="5">
    <xsl:apply-templates select="current()/Header"/>
    <xsl:apply-templates select="current()/Data"/>
  </table>
</xsl:template>

<xsl:template match="Header">
  <tr>
    <TH bgcolor="#000066" width="250" height="30" style="color:white">
      <xsl:value-of select="string[1]"/>
    </TH>
    <TH bgcolor="#000066" height="30" style="color:white">
      <xsl:value-of select="string[2]"/>
    </TH>
    <TH bgcolor="#000066" height="30" style="color:white">
      <xsl:value-of select="string[3]"/>
    </TH>
    <TH bgcolor="#000066" height="30" style="color:white">
      <xsl:value-of select="string[4]"/>
    </TH>
    <TH bgcolor="#000066" height="30" style="color:white">
      <xsl:value-of select="string[5]"/>
    </TH>
    <TH bgcolor="#000066" height="30" style="color:white">
      <xsl:value-of select="string[6]"/>
    </TH>
  </tr>
</xsl:template>

<xsl:template match="Data">
  <xsl:for-each select="current()/ScorecardContract">
    <xsl:variable name="i" select="position()"/>
    <tr>
      <xsl:apply-templates select="TypeName"/>
      <xsl:apply-templates select="Goal"/>
      <xsl:apply-templates select="Value1D"/>
      <xsl:apply-templates select="Value7D"/>
      <xsl:apply-templates select="ValueQTD"/>
      <xsl:apply-templates select="ValueYTD"/>
    </tr>
  </xsl:for-each>
</xsl:template>

<xsl:template match="TypeName">
  <xsl:choose>
    <xsl:when test="self::node()[text()='System'] | self::node()[text()='Checkpoint']">
      <td bgcolor="lightgray" style="font-weight:bold">
        <xsl:value-of select="."/>
      </td>
    </xsl:when>
    <xsl:otherwise>
      <td>
        <xsl:value-of select="."/>
      </td>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="Goal">
  <td bgcolor="lightgray">
    <xsl:value-of select="format-number(.,'###,##%')"/>
  </td>
</xsl:template>

<xsl:template match="Value1D">
  <xsl:choose>
    <xsl:when test="position() != 3"> <!-- Here where I went wrong -->
      <td bgcolor="lightgreen"><xsl:value-of select="."/></td>
    </xsl:when>
    <xsl:otherwise>
      <td bgcolor="green">
        <xsl:value-of select="."/>
      </td>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="Value7D">
  <td bgcolor="lightgreen"><xsl:value-of select="."/></td>
</xsl:template>

<xsl:template match="ValueQTD">
  <td bgcolor="lightgreen"><xsl:value-of select="."/></td>
</xsl:template>

<xsl:template match="ValueYTD">
  <td bgcolor="lightgray"><xsl:value-of select="."/></td>
</xsl:template>

</xsl:stylesheet>

Всего создано 6 таблиц. Мой вопрос:

Как я могу пометить таблицы, чтобы генерировать разные стили? Например, как изменить стили для третьей таблицы?

1 ответ

Решение

Поскольку шаблон для родительского элемента просто применяет шаблоны к ScorecardSummaryByKPI элементы, вы можете использовать position() для ссылки на порядок этих элементов.

<xsl:template match="ScorecardSummaryByKPI">
  <table border="1" cellspacing="0" cellpadding="5">
<xsl:if test="position()=3">
   <xsl:attribute name="class">something</xsl:attribute>
</xsl:if>
    <xsl:apply-templates select="Header"/>
    <xsl:apply-templates select="Data"/>
  </table>
</xsl:template>

добавляет class="something" на третий стол. Обратите внимание, я удалил current()/ Вы можете удалить их все, они не делают ничего полезного.

Другие вопросы по тегам