XSLT:对于每个节点转换,如果发现A = 2且A = 1,则执行此操作

时间:2010-03-19 15:04:14

标签: xslt

示例1:

<time>
   <timestamp>01:00</timestamp>
   <event>arrived<event>
</time>
<time>
   <timestamp>02:00</timestamp>
   <event>left<event>
</time>

示例2:

<time>
   <timestamp>02:00</timestamp>
   <event>left<event>
</time>

XSLT需要做:

  • FOR EACH node DO:
  • IF event = arrival THEN set eventtype = atdestination
  • 如果找到IF event = left并且找到event = arrival那么设置新节点类型= leftdestination ELSE set type = left

XSLT应用于示例1:

<event>
   <time>01:00</time>
   <type>atdestination</type>
<event>
<event>
   <time>02:00</time>
   <type>leftdestination</type>
<event>

XSLT应用于示例2:

<event>
   <time>02:00</time>
   <type>left</type>
<event>

1 个答案:

答案 0 :(得分:2)

<xsl:template match="time">
  <event>
    <xsl:apply-templates select="*" />
  </event>
</xsl:template>

<xsl:template match="timestamp">
  <time><xsl:value-of select="." /></time>
</xsl:template>

<xsl:template match="type">
  <type>
    <xsl:choose>
      <xsl:when test=".='arrived'">
        <xsl:text>atdestination</xsl:text>
      </xsl:when>
      <xsl:when test=".='left' and ../../event[type='arrived']">
        <xsl:text>leftdestination</xsl:text>
      </xsl:when>
      <xsl:when test=".='left' and not(../../event[type='arrived'])">
        <xsl:text>left</xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text>undefined</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </type>
</xsl:template>