XSLT:根据父节点属性输出列表
我正在努力创建一个要在父节点(“文件夹”)的条件下显示的列表,并将属性“折叠”设置为“是”或“否”。 结果将仅显示列表的前两级,而不显示第三级,如下所示。
- 第一。等级:显示
- 2级。等级:显示
- 3级。级别:无显示
这个想法是用
)">。
那应该有资格进入第三名。级别为不显示,但不知何故它没有做我想要它做的事情。
我可能使用了错误的构造和/或语法。
非常感谢您的帮助,谢谢。
包含一些内容的 XML 结构:
<xbel>
<folder folded="yes">
<level>1</level>
<title>bookmarks</title>
<desc>my bookmarks</desc>
<folder folded="no">
<level>2</level>
<title>Android</title>
<desc>my Android</desc>
<bookmark href="http://www.phonesreview.co.uk/">
<title>HTC Sync 3.0.5422 Update: Aria, Desire, Hero, Legend</title>
<desc>The new HTC Sync 3.0.5422 update will be most welcome...</desc>
</bookmark>
<folder folded="no">
<level>3</level>
<title>Apps</title>
<desc>Android Apps</desc>
<bookmark href="http://www.androidzoom.com/">
<title>Android Communication Apps</title>
<desc>Download Communication Apps for Android.</desc>
</bookmark>
<bookmark href="http://www.htc.com/">
<title>HTC - Android</title>
<desc>Apps for HTC-Android.</desc>
</bookmark>
</folder>
</folder>
</folder>
</xbel>
XSLT:
<!--creates a nested list of elements named 'folder'-->
<xsl:template match="folder" mode="linklist">
<li>
<xsl:if test="folder/level = 2">
Level:<xsl:value-of select="level"/> /
Title:<xsl:value-of select="title"/> /
Desc:<xsl:value-of select="desc"/>
<ul>
<xsl:apply-templates mode="linklist" />
</ul>
</xsl:if>
</li>
</xsl:template>
<xsl:template match="bookmark" mode="linklist">
<li> <!-- this bookmark is just another item in the list of bookmarks -->
<!-- the title -->
<a rel="nofollow" href="{@href}"><xsl:value-of select="title"/></a>
<!-- the description -->
<xsl:if test="desc">
<span class="bookmarkDesc">
<xsl:value-of select="desc"/>
</span>
</xsl:if>
</li>
</xsl:template>
样式表 HTML
<body>
<ul>
<xsl:apply-templates mode="linklist" />
</ul>
</body>
生成的输出(级别:1-3)
Level:1 / Title:bookmarks / Desc:my bookmarks
Level:2 / Title:Android / Desc:my Android
HTC Sync 3.0.5422 Update: Aria, Desire, Hero, Legend ...
Level:3 / Title:Apps / Desc:Android Apps
Android Communication AppsDownload Communication Apps for Android.
HTC - AndroidApps for HTC-Android.
预期输出:(级别:1-2)
Level:1 / Title:bookmarks / Desc:my bookmarks
Level:2 / Title:Android / Desc:my Android
HTC Sync 3.0.5422 Update: Aria, Desire, Hero, Legend ...
我尝试了这个模板,但是输出了最后两个节点,我需要前两个节点。
<xsl:template match="folder[parent::folder/@folded = 'yes']" mode="linklist">
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了防止处理展开的
folder
元素,您可以进行的最简单的更改是添加一个包含它们的空模板(即不产生输出):所有不具有
folded
属性等于no
将使用您现有的模板进行处理;那些这样做的人将被这个新的人捕获。相反,如果您想要处理每个
folded
元素,其自己的folded
属性等于yes
或其父级的属性(如更新的 XML 中所示)示例),然后使用以下模板:您可能还需要包含一个空模板来隐藏所有其他
folder
元素:The simplest possible change you could make to prevent processing of unfolded
folder
elements is to add an empty template that swallows them (i.e. produces no output):All
folder
elements not having afolded
attribute equal tono
will be processed using your existing template; those that do will be captured by this new one.If instead you want to process each
folder
element having either its ownfolded
attribute equal toyes
or that of its parent (as in the updated XML example), then use the following template:You'll probably also want to include an empty template for hiding all other
folder
elements: