可以简化此xpath吗?

时间:2019-03-23 12:52:19

标签: xslt-1.0

我有此代码:

<!--Display the name of the brother assigned-->
<xsl:template match="Name | PrayerOpen | PrayerEnd">
  <td class="cellName">
    <xsl:choose>
      <xsl:when test="//MeetingWorkBook/Settings/ForeignGroupMode='1' and (
                 ../@BookmarkId='2' or ../../@BookmarkId='2' or 
                 ../../../@BookmarkId='2'or ../../../../@BookmarkId='2' or 
                 ../@BookmarkId='4' or ../../@BookmarkId='4' or 
                 ../../../@BookmarkId='4' or ../../../../@BookmarkId='4' or 
                 self::PrayerOpen or self::PrayerEnd)">
        <em><xsl:value-of select ="//Labels/MainHall"/></em>
      </xsl:when>
      <xsl:when test="//MeetingWorkBook/Settings/ForeignGroupMode='1' and .='' and self::Name">
        <em><xsl:value-of select ="//Labels/MainHall"/></em>
      </xsl:when>
      <xsl:when test="@Duplicate=1">
        <span class="textDuplicate">
          <xsl:value-of select="."/>
        </span>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="."/>
      </xsl:otherwise>
    </xsl:choose>
  </td>
</xsl:template>

工作正常。我有一系列路径,以便可以找到具有BookmarkId属性的正确节点。可以简化此代码吗?

基本上,基于当前节点的上下文,我想向上移动父级,直到其到达具有属性的Meeting节点为止。

示例XML

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="Workbook-S-140-PublicTalk-WatchtowerStudy-ServiceTalk-FGroup-v2.xsl"?>
<MeetingWorkBook>
    <Meeting BookmarkId="0" PageBreak="0" NumberClasses="1" SpecialEvent="0">
        <PrayerOpen>Name 1</PrayerOpen>
        <TFGW NumberClasses="1">
            <TFGWItem>
                <Name>Name 2</Name>
            </TFGWItem>
            <TFGWItem>
                <Name>Name 3</Name>
            </TFGWItem>
        </TFGW>
        <AYFM NumberClasses="1">
            <Teaching>
                <Name>Name 4</Name>
            </Teaching>
        </AYFM>
        <LAC CircuitVisit="0">
            <LACItem>
                <Name>Name 5</Name>
            </LACItem>
            <PrayerEnd>Name 6</PrayerEnd>
        </LAC>
    </Meeting>
</MeetingWorkBook>

1 个答案:

答案 0 :(得分:0)

根据评论中的建议,我最终使用了ancestor调用。所以我的模板现在看起来像这样:

  <!--Display the name of the brother assigned-->
  <xsl:template match="Name | PrayerOpen | PrayerEnd">
    <td class="cellName">
      <xsl:choose>
        <xsl:when test="//MeetingWorkBook/Settings/ForeignGroupMode='1' and (
                        ancestor::Meeting[@BookmarkId='2'] or 
                        ancestor::Meeting[@BookmarkId='4'] or self::PrayerOpen or self::PrayerEnd)">
          <em><xsl:value-of select ="//Labels/MainHall"/></em>
        </xsl:when>
        <xsl:when test="//MeetingWorkBook/Settings/ForeignGroupMode='1' and .='' and self::Name">
          <em><xsl:value-of select ="//Labels/MainHall"/></em>
        </xsl:when>
        <xsl:when test="@Duplicate=1">
          <span class="textDuplicate">
            <xsl:value-of select="."/>
          </span>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="."/>
        </xsl:otherwise>
      </xsl:choose>
    </td>
  </xsl:template>