You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用XSLT 1.0条件显示XML中的会议日期数据?

Solution for Conditional Date Display in XSLT 1.0

Let's fix this to properly handle the fact that each Meeting node has either a MeetingDate or a SpecialEvent/Date (never both). Here are two clean approaches you can use:

Approach 1: Explicit Conditional with xsl:choose

This is straightforward and makes the logic very clear, which is great for readability:

<div class="headingZoom">
  <xsl:text>Congregation Meeting Schedule for </xsl:text>
  <!-- Handle first Meeting's date -->
  <xsl:variable name="firstMeeting" select="MeetingWorkBook/Meeting[1]"/>
  <xsl:choose>
    <xsl:when test="$firstMeeting/MeetingDate">
      <xsl:value-of select="$firstMeeting/MeetingDate/@MonthFull"/>
      <xsl:text> </xsl:text>
      <xsl:value-of select="$firstMeeting/MeetingDate/@Year"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$firstMeeting/SpecialEvent/Date/@MonthFull"/>
      <xsl:text> </xsl:text>
      <xsl:value-of select="$firstMeeting/SpecialEvent/Date/@Year"/>
    </xsl:otherwise>
  </xsl:choose>
  <xsl:text> - </xsl:text>
  <!-- Handle last Meeting's date -->
  <xsl:variable name="lastMeeting" select="MeetingWorkBook/Meeting[last()]"/>
  <xsl:choose>
    <xsl:when test="$lastMeeting/MeetingDate">
      <xsl:value-of select="$lastMeeting/MeetingDate/@MonthFull"/>
      <xsl:text> </xsl:text>
      <xsl:value-of select="$lastMeeting/MeetingDate/@Year"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$lastMeeting/SpecialEvent/Date/@MonthFull"/>
      <xsl:text> </xsl:text>
      <xsl:value-of select="$lastMeeting/SpecialEvent/Date/@Year"/>
    </xsl:otherwise>
  </xsl:choose>
</div>

We use variables to store the first and last Meeting nodes so we don't repeat XPath queries, keeping the code cleaner. The test condition checks if the relevant date node exists in the meeting, then outputs the corresponding attributes.

Approach 2: Concise Node Set Selection

If you prefer more compact code, you can leverage XSLT 1.0's node set behavior. Since each Meeting only has one of the two date nodes, we can merge the possible nodes and select the first one that exists:

<div class="headingZoom">
  <xsl:text>Congregation Meeting Schedule for </xsl:text>
  <!-- First Meeting date -->
  <xsl:value-of select="(MeetingWorkBook/Meeting[1]/MeetingDate/@MonthFull | MeetingWorkBook/Meeting[1]/SpecialEvent/Date/@MonthFull)[1]"/>
  <xsl:text> </xsl:text>
  <xsl:value-of select="(MeetingWorkBook/Meeting[1]/MeetingDate/@Year | MeetingWorkBook/Meeting[1]/SpecialEvent/Date/@Year)[1]"/>
  <xsl:text> - </xsl:text>
  <!-- Last Meeting date -->
  <xsl:value-of select="(MeetingWorkBook/Meeting[last()]/MeetingDate/@MonthFull | MeetingWorkBook/Meeting[last()]/SpecialEvent/Date/@MonthFull)[1]"/>
  <xsl:text> </xsl:text>
  <xsl:value-of select="(MeetingWorkBook/Meeting[last()]/MeetingDate/@Year | MeetingWorkBook/Meeting[last()]/SpecialEvent/Date/@Year)[1]"/>
</div>

This works because the | operator combines the two node sets, and [1] picks the first node that exists (since only one will be present for each meeting). It's shorter, still readable once you understand the trick, and avoids explicit conditionals.

Both approaches will correctly output the date based on which node is present in each Meeting element, with no empty values or incorrect selections.

内容的提问来源于stack exchange,提问作者Andrew Truckle

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:18:28