如果现有节点为空,则定位下一个兄弟内容

时间:2017-08-02 20:28:54

标签: xslt-1.0

以下是XML片段:

<?xml version="1.0" encoding="utf-8"?>
<AssignmentHistory Version="171804">
    <W20160229>
        <ReviewQuestion>Why will God’s Kingdom have to crush the earthly rulerships depicted in the image? (Da 2:44)</ReviewQuestion>
        <StudentItems>
            <Item>
                <Name Counsel="9" NextCounsel="0" Completed="1">Finlay Truckle</Name>
                <Type>Bible Reading (Main)</Type>
                <Description>Bible Reading</Description>
            </Item>
            <Item>
                <Name Counsel="38" NextCounsel="0" Completed="1">Name</Name>
                <Type>#1 Student (Main)</Type>
                <Description>Initial Call</Description>
            </Item>
            <Item>
                <Name>Name</Name>
                <Type>Assistant</Type>
                <Description>Initial Call</Description>
            </Item>
            <Item>
                <Name Counsel="41" NextCounsel="0" Completed="1">Name</Name>
                <Type>#2 Student (Main)</Type>
                <Description>Return Visit</Description>
            </Item>
            <Item>
                <Name>¬DELETED¬</Name>
                <Type>Assistant</Type>
                <Description>Return Visit</Description>
            </Item>
            <Item>
                <Name Counsel="45" NextCounsel="0" Completed="1">Name</Name>
                <Type>#3 Student (Main)</Type>
                <Description>Bible Study</Description>
            </Item>
            <Item>
                <Name>Name</Name>
                <Type>Assistant</Type>
                <Description>Bible Study</Description>
            </Item>
        </StudentItems>
    </W20160229>
    <W20160404/>
    <W20160411>
        <ReviewQuestion>What did the immense tree in Nebuchadnezzar’s dream represent? (Da 4:10, 11, 20-22)</ReviewQuestion>
        <StudentItems>
            <Item>
                <Name Counsel="11" NextCounsel="0" Completed="1">Name</Name>
                <Type>Bible Reading (Main)</Type>
                <Description>Bible Reading</Description>
            </Item>
            <Item>
                <Name Counsel="0" NextCounsel="0" Completed="1">Name</Name>
                <Type>#1 Student (Main)</Type>
                <Description>Initial Call</Description>
            </Item>
            <Item>
                <Name>Name</Name>
                <Type>Assistant</Type>
                <Description>Initial Call</Description>
            </Item>
            <Item>
                <Name Counsel="37" NextCounsel="0" Completed="1">Name</Name>
                <Type>#2 Student (Main)</Type>
                <Description>Return Visit</Description>
            </Item>
            <Item>
                <Name>Name</Name>
                <Type>Assistant</Type>
                <Description>Return Visit</Description>
            </Item>
            <Item>
                <Name Counsel="0" NextCounsel="0" Completed="1">Name</Name>
                <Type>#3 Student (Main)</Type>
                <Description>Bible Study</Description>
            </Item>
            <Item>
                <Name>Name</Name>
                <Type>Assistant</Type>
                <Description>Bible Study</Description>
            </Item>
        </StudentItems>
    </W20160411>
</AssignmentHistory>

现在,在我的XSL脚本中,我像这样链接到上面的文档(现在只是一个片段):

<tr>
  <td class="cellComments" colspan="4">
    <xsl:variable name="AssignHistory" select="document('AssignHistory.xml')"/>
    <xsl:variable name="week" select="Date/@NextWeek"/>
    <xsl:variable name="NextReviewQuestion" select="$AssignHistory/AssignmentHistory/*[name()=$week]/ReviewQuestion"/>
    <xsl:if test="normalize-space($NextReviewQuestion) != ''">
      <span class="textReviewQuestionLabel">
        <xsl:value-of select="//Labels/NextReviewQuestion"/>&#160;
      </span>
      <span class="textReviewQuestion">
        <xsl:value-of select="$NextReviewQuestion"/>
      </span>
      <br />
    </xsl:if>
    <br />
    <br />
    <br />
    <span style="font-size: 8pt;">
      <xsl:apply-templates select="$AssignHistory/AssignmentHistory/*[name()=$week]/StudentItems">
        <xsl:with-param name="MainHall" select="//Labels/MainHall"/>
        <xsl:with-param name="AuxClass1" select="//Labels/AuxClass1"/>
        <xsl:with-param name="AuxClass2" select="//Labels/AuxClass2"/>
      </xsl:apply-templates>
    </span>
  </td>
</tr>

现在这是问题所在。看周:<W20160404/>?本周是特别活动。我们没有开会,因为我们都去了其他地方进行集会。因此,没有细节。

所以这就是我想要做的事情:

如果我试图获取详细信息的那一周是空的(特殊事件),则尝试从下一个兄弟中获取值(如果有的话)。否则,如果它不是空的(正常会议),就像我现在一样使用返回的值。

对于学生的一些脚本来说确实有点复杂。但原则是一样的。如果需要,请使用以下兄弟。

我确信我的措​​辞比它需要的要复杂得多。

更新

我会在适当的时候改革这个问题。

以下是历史记录摘要:

<?xml version="1.0" encoding="utf-8"?>
<AssignmentHistory Version="171804">
  <W20160229>
    <ReviewQuestion>Why will God’s Kingdom have to crush the earthly rulerships depicted in the image? (Da 2:44)</ReviewQuestion>
  </W20160229>
  <W20160404/>
  <W20160411>
    <ReviewQuestion>What did the immense tree in Nebuchadnezzar’s dream represent? (Da 4:10, 11, 20-22)</ReviewQuestion>
  </W20160411>
  <W20170803>
    <ReviewQuestion>Test question</ReviewQuestion>
  </W20170803>
</AssignmentHistory>

以下是主要的XML代码段:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="TestTransform.xsl"?>
<MeetingWorkBook>
    <Meeting>
        <Date ThisWeek="W20160229" NextWeek="W20160404">Date 1</Date>
    </Meeting>
    <Meeting>
        <Date ThisWeek="W20160404" NextWeek="W20160411">Date 2</Date>
    </Meeting>
    <Meeting>
        <Date ThisWeek="W20160411" NextWeek="W20170803">Date 3</Date>
    </Meeting>
</MeetingWorkBook>

以下是简化的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" indent="yes" version="4.01"
    doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
    doctype-public="//W3C//DTD XHTML 1.0 Transitional//EN"/>

  <xsl:template match="/">
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
      </head>
      <body>
        <table>
          <xsl:for-each select="MeetingWorkBook/Meeting">
            <tr>
              <td>
                <xsl:value-of select="Date/@ThisWeek"/>
              </td>
              <td>
                <xsl:value-of select="Date/@NextWeek"/>
              </td>
              <td>
                <xsl:variable name="AssignHistory" select="document('TestHist.xml')"/>
                <xsl:variable name="week" select="Date/@NextWeek"/>
                <xsl:variable name="NextReviewQuestion" select="$AssignHistory/AssignmentHistory/*[name()=$week]/ReviewQuestion"/>
                <xsl:if test="normalize-space($NextReviewQuestion) != ''">
                  <xsl:text>Question: </xsl:text>
                  <xsl:value-of select="$NextReviewQuestion"/>
                </xsl:if>
              </td>
            </tr>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

现在,如果您在IE中查看主XML文件,您将获得:

  

W20160229 W20160404

     

W20160404 W20160411问题:尼布甲尼撒梦中巨大的树代表了什么? (大4:10,11,20-22)

     

W20160411 W20170803问题:测试问题

这显示了问题。日期是虚构的。所以这是我想在第一次尝试返回空节点时使用以下兄弟“ReviewQuestion”的上下文。

更新2

根据您对答案的出色修订,我能够让我的脚本更简单地按预期工作。我需要一个条件标签前缀“问题:”,因此我将代码分解了一些。所以目前我有:

  

  <xsl:template match="/">
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
      </head>
      <body>
        <table>
          <xsl:for-each select="MeetingWorkBook/Meeting">
            <tr>
              <td>
                <xsl:value-of select="Date/@ThisWeek"/>
              </td>
              <td>
                <xsl:value-of select="Date/@NextWeek"/>
              </td>
              <td>
                <xsl:variable name="AssignHistory" select="document('TestHist.xml')"/>
                <xsl:variable name="week" select="Date/@NextWeek"/>

                <xsl:variable name="history-week" select="$AssignHistory/AssignmentHistory/*[name()=$week]"/>
                <xsl:variable name="NextReviewQuestion" select="($history-week | $history-week/following-sibling::*)/ReviewQuestion"/>
                <xsl:if test="normalize-space($NextReviewQuestion) != ''">
                  <xsl:text>Question: </xsl:text>
                  <xsl:value-of select="$NextReviewQuestion"/>
                </xsl:if>
              </td>
            </tr>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

我现在要解决的唯一问题是我最初也有这个代码:

  <xsl:apply-templates select="$AssignHistory/AssignmentHistory/*[name()=$week]/StudentItems">
    <xsl:with-param name="MainHall" select="//Labels/MainHall"/>
    <xsl:with-param name="AuxClass1" select="//Labels/AuxClass1"/>
    <xsl:with-param name="AuxClass2" select="//Labels/AuxClass2"/>
  </xsl:apply-templates>

我现在需要再做同样的事情。如果第一个没有返回任何内容,我们需要使用以下兄弟“StudentItems”。

然后我们就完成了。

1 个答案:

答案 0 :(得分:1)

考虑以下简化示例:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="AssignmentHistory">
    <table border="1">
        <xsl:apply-templates/>
    </table>
</xsl:template>

<xsl:template match="*[starts-with(name(), 'W')]">
    <tr>
        <td>
            <xsl:value-of select="name()" />
        </td>
        <xsl:choose>
            <xsl:when test="*">
                <xsl:apply-templates/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:apply-templates select="following-sibling::*[*][1]/*"/>
            </xsl:otherwise>
        </xsl:choose>
    </tr>
</xsl:template>

<xsl:template match="ReviewQuestion">
    <td>
        <xsl:value-of select="." />
    </td>
</xsl:template>

<xsl:template match="StudentItems">
    <!-- ??? -->
</xsl:template>

</xsl:stylesheet>

当应用于给定的XML示例时,结果(呈现)将为:

enter image description here

加了:

关于问题的更新部分中提出的问题:

尝试更换此部分:

<td>
    <xsl:variable name="AssignHistory" select="document('TestHist.xml')"/>
    <xsl:variable name="week" select="Date/@NextWeek"/>
    <xsl:variable name="NextReviewQuestion" select="$AssignHistory/AssignmentHistory/*[name()=$week]/ReviewQuestion"/>
    <xsl:if test="normalize-space($NextReviewQuestion) != ''">
        <xsl:text>Question: </xsl:text>
        <xsl:value-of select="$NextReviewQuestion"/>
    </xsl:if>
</td>

使用:

<td>
    <xsl:variable name="AssignHistory" select="document('TestHist.xml')"/>
    <xsl:variable name="week" select="Date/@NextWeek"/>

    <xsl:variable name="history-week" select="$AssignHistory/AssignmentHistory/*[name()=$week]"/>
    <xsl:value-of select="($history-week | $history-week/following-sibling::*)[normalize-space(ReviewQuestion)][1]/ReviewQuestion"/>
</td>