html - 列表元素的以下兄弟姐妹
问题描述
我想创建一个列表项,格式正确。但是,我想将字符串 '或' 添加到替代项中。我的 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
解决方案
您对以下兄弟的陈述实际上是正确的。问题是Class
that 确实有一个 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>
推荐阅读
- python - Python:在另一个文件/类中分离一些实例方法的定义
- json - 如何在从 Web API 返回的 JSON 数据中格式化 DateTime
- html - 在悬停时更改特定 h4 的颜色
- oracle - 当需要在 oracle hrms 中定义第二个工资单时
- for-loop - 如何在 Robot Framework 中的多个变量上运行相同的关键字
- c# - 无法访问 web.config 的特定字段
- php - 如何避免在我的 PHP 应用程序运行到 Docker 容器中的配置上硬编码数据库凭据
- flutter - 我如何在颤振中读取带有相机预览的二维码
- spring - 如何在 Spring Security Framework 中动态调用 void configure(HttpSecurity http)
- angular - Hyperledger Composer - 在钱包/api 上发布返回错误:找不到中央目录的结尾