首页 > 解决方案 > 列表元素的以下兄弟姐妹

问题描述

我想创建一个列表项,格式正确。但是,我想将字符串 '' 添加到替代项中。我的 XPath 控件无法正常工作。如果我有 aList和一个基础SmallList,该函数将被忽略。

如果列表不存在,则一切都按预期进行。但是,我没有得出正确的表达方式。我正在使用 XSLT 1.0。下面是一个例子。

XML:

<University>
    <Class>
        <Student>John Doe</Student>
        <List>
            <SmallList>
                <Student>Jane Doe</Student>
            </SmallList>
        </List>
    </Class>
    <AlternativeClass>
        <Class>
            <Student>Richard Roe</Student>
        </Class>
    </AlternativeClass>
</University>

结果应该是什么样子:

<div>
   <p data-role="heading">University-Heading</p>
   <ul>
      <li>John Doe
         <ul>
            <li>Jane Doe</li>
         </ul>
      </li>
      <li class="parablock bold_">or</li>
      <li>Richard Roe</li>
   </ul>
</div>

正如我提到<li class="parablock bold_>or<li>的失踪。如果我想删除List-Block,结果看起来像预期的那样(例如本例中的 Jane Doe)。

<div>
   <p data-role="heading">University-Heading</p>
   <ul>
      <li>John Doe</li>
      <li class="parablock bold_">or</li>
      <li>Richard Roe</li>
   </ul>
</div>

following-sibling 的构造应如下所示(BBB 的following-sibling 应为 XXX):

<AAA>
    <BBB>
        <CCC/>
    </BBB>
    <XXX>
        <DDD/>
        <EEE/>
    </XXX>
</AAA>

我的 XSLT 看起来像这样:

<xsl:template match="University">
    <div>
        <!-- HEADING FOR UNIVERSITY -->
        <p data-role="heading">
            <xsl:text>University-Heading</xsl:text>
        </p>
        <ul>
            <xsl:apply-templates/>
        </ul>
    </div>
</xsl:template>
<!-- CLASS -->
<xsl:template match="Class">
    <xsl:apply-templates/>
    <!-- 'OR' BEFORE ALTERNATIVE CLASS -->
    <!-- I GUESS THAT THE MISTAKE IS IN HERE -->
    <xsl:if test="following-sibling::*[1][self::AlternativeClass]">
        <li class="parablock bold_">
            <xsl:text>or</xsl:text>
        </li>
    </xsl:if>
</xsl:template>
<!-- CLASS-NODE WITH LIST -->
<xsl:template match="Class[List]">
    <xsl:apply-templates select="Student"/>
</xsl:template>
<!-- ALTERNATIVECLASS -->
<xsl:template match="AlternativeClass">
    <xsl:apply-templates/>
</xsl:template>
<!-- STUDENT IN CLASS AND SMALLLIST -->
<xsl:template match="SmallList/Student | Class/Student">
    <li>
        <xsl:if test="name(preceding-sibling::*[1]) = 'Para'">
            <xsl:attribute name="class">parablock</xsl:attribute>
        </xsl:if>
        <xsl:apply-templates/>
        <xsl:apply-templates select="following-sibling::*[1][self::List]"/>
    </li>
</xsl:template>
<!-- LIST -->
<xsl:template match="List">
    <ul>
        <xsl:apply-templates/>
    </ul>
</xsl:template>
<!-- SMALLLIST -->
<xsl:template match="SmallList">
    <xsl:apply-templates/>
</xsl:template>

.NET XSLT 小提琴:https ://xsltfiddle.liberty-development.net/bdxtqV

标签: htmlxmlxsltxpath

解决方案


您对以下兄弟的陈述实际上是正确的。问题是Classthat 确实有一个 follow AlternativeClass,也有一个子List元素,这意味着它被这个模板匹配(因为它有额外的条件,所以模板获得更高的优先级)

<xsl:template match="Class[List]">
    <xsl:apply-templates select="Student"/>
</xsl:template>

由于这没有以下兄弟检查,因此不会创建您的预期输出。

您也可以将条件添加到此语句中

<xsl:template match="Class[List]">
    <xsl:apply-templates select="Student"/>
    <xsl:if test="following-sibling::*[1][self::AlternativeClass]">
        <li class="parablock bold_">
            <xsl:text>or</xsl:text>
        </li>
    </xsl:if>
</xsl:template>

虽然这有点重复代码,但您可以将该逻辑放在命名模板中,或者您可以将两个“类”模板合并为一个......

<xsl:template match="Class">
    <xsl:choose>
        <xsl:when test="List">
            <xsl:apply-templates select="Student"/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:apply-templates />
        </xsl:otherwise>
    </xsl:choose>
    <xsl:if test="following-sibling::*[1][self::AlternativeClass]">
        <li class="parablock bold_">
            <xsl:text>or</xsl:text>
        </li>
    </xsl:if>
</xsl:template>

推荐阅读