0

我正在努力创建一个在父节点(“文件夹”)的条件下显示的列表,其中属性“折叠”设置为“是”或“否”。结果应仅显示列表的前两级,而不是如下所示的第三级。

  • 第一个。级别:显示
  • 第二。级别:显示
  • 第三。级别:无显示

这个想法是检查“文件夹”属性<folder folded="yes"><xsl:if test="not(parent::yes)">这应该有资格获得第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">
4

1 回答 1

1

为了防止处理展开的folder元素,您可以进行的最简单的更改是添加一个吞下它们的空模板(即不产生输出):

<xsl:template match="folder[@folded='no']" mode="linklist"/>

所有folder没有folded属性 equal 的元素no都将使用您现有的模板进行处理;那些这样做的人将被这个新的捕获。

相反,如果您想处理folder其自身folded属性等于yes或等于其父属性的每个元素(如更新的 XML 示例中所示),则使用以下模板:

<xsl:template match="folder[@folded='yes' or ../@folded='yes']" mode="linklist">
    <!-- body elided -->
</xsl:template>

您可能还需要包含一个空模板来隐藏所有其他folder元素:

<xsl:template match="folder" mode="linklist" />
于 2011-08-22T14:29:58.897 回答