这两个模板可以合并吗?

时间:2016-03-18 20:50:25

标签: xslt

我有这两个XSL模板:

  <xsl:template match="OpeningComments">
    <tr>
      <td class="cellTime">
        <xsl:value-of select="Time/@EndTime24"/>
      </td>
      <td class="textTheme">
        <span class="bulletTFGW">&#8226;</span>
        <span class="textTheme">
          <xsl:value-of select="Theme"/>
        </span>
        <span class="textDuration">
          (<xsl:value-of select="Time"/>)
        </span>
      </td>
      <td class="cellName">Chairman</td>
    </tr>
  </xsl:template>

  <xsl:template match="ClosingComments">
    <tr>
      <td class="cellTime">
        <xsl:value-of select="Time/@EndTime24"/>
      </td>
      <td class="textTheme">
        <span class="bulletLAC">&#8226;</span>
        <span class="textTheme">
          <xsl:value-of select="Theme"/>
        </span>
        <span class="textDuration">
          (<xsl:value-of select="Time"/>)
        </span>
      </td>
      <td class="cellName">Chairman</td>
    </tr>
  </xsl:template>

唯一的区别是:

  1. 一个是 OpeningComments ,另一个 ClosingComments
  2. 一个使用课堂风格 bulletTFGW ,另一个使用 bulletLAC
  3. 这些可以合理地组合成一个模板吗?或者这会使任务过于复杂?

    示例XML(缩减):

    <?xml version="1.0" encoding="UTF-8"?>
    <?xml-stylesheet type="text/xsl" href="WEEK-S-140.xsl"?>
    <MeetingWorkBook>
      <Meeting SpecialEvent="0" NumberClasses="1">
        <OpeningComments>
          <Theme>Opening Comments</Theme>
          <Time StartTime12="7:20" StartTime24="19:20" EndTime12="7:23" EndTime24="19:23">3 min.</Time>
        </OpeningComments>
    
        <LAC CircuitVisit="0">
          <ClosingComments>
            <Theme>Review/Preview/Announcements</Theme>
            <Time StartTime12="8:52" StartTime24="20:52" EndTime12="8:55" EndTime24="20:55">3 min.</Time>
          </ClosingComments>
        </LAC>
      </Meeting>
    </MeetingWorkBook>
    

2 个答案:

答案 0 :(得分:2)

是的,他们可以。一种干净的方法是使用命名模板并相应地调用它:

<xsl:template name="Comments">
  <xsl:param name="bulletClass" />

  <tr>
    <td class="cellTime">
      <xsl:value-of select="Time/@EndTime24"/>
    </td>
    <td class="textTheme">
      <span class="{ $bulletClass }">&#8226;</span>
      <span class="textTheme">
        <xsl:value-of select="Theme"/>
      </span>
      <span class="textDuration">
        (<xsl:value-of select="Time"/>)
      </span>
    </td>
    <td class="cellName">Chairman</td>
  </tr>
</xsl:template>

<xsl:template match="OpeningComments">
  <xsl:call-template name="Comments">
    <xsl:with-param name="bulletClass">bulletTFGW</xsl:with-param>
  </xsl:call-template>
</xsl:template>

<xsl:template match="ClosingComments">
  <xsl:call-template name="Comments">
    <xsl:with-param name="bulletClass">bulletLAC</xsl:with-param>
  </xsl:call-template>
</xsl:template>

另一种方法是只有一个模板,并根据当前元素名称选择类:

<xsl:template match="OpeningComments | ClosingComments">
  <xsl:param name="bulletClass" />

  <tr>
    <td class="cellTime">
      <xsl:value-of select="Time/@EndTime24"/>
    </td>
    <td class="textTheme">
      <span class="bullet{ concat(
        substring('TFGW', 1, 4 * boolean(self::OpeningComments)),
        substring('LAC' , 1, 3 * boolean(self::ClosingComments))
      ) }">&#8226;</span>
      <span class="textTheme">
        <xsl:value-of select="Theme"/>
      </span>
      <span class="textDuration">
        (<xsl:value-of select="Time"/>)
      </span>
    </td>
    <td class="cellName">Chairman</td>
  </tr>
</xsl:template>

答案 1 :(得分:2)

尝试:

<xsl:template match="OpeningComments | ClosingComments">
    <tr>
        <td class="cellTime">
            <xsl:value-of select="Time/@EndTime24"/>
        </td>
        <td class="textTheme">
            <span>
                <xsl:attribute name="class">
                    <xsl:choose>
                        <xsl:when test="self::OpeningComments">bulletTFGW</xsl:when>
                        <xsl:otherwise>bulletLAC</xsl:otherwise>
                    </xsl:choose>
                </xsl:attribute>
                <xsl:text>&#8226;</xsl:text>
            </span>
            <span class="textTheme">
                <xsl:value-of select="Theme"/>
            </span>
            <span class="textDuration">
                (<xsl:value-of select="Time"/>)
            </span>
        </td>
        <td class="cellName">Chairman</td>
    </tr>
</xsl:template>

这是假设XSLT 1.0。