XSLT:根据父节点属性输出列表

发布于 2024-11-30 16:39:24 字数 3873 浏览 5 评论 0 原文

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

  • 第一。等级:显示
  • 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">

I'm struggling to create a list to be displayed on the condition of the parent node ('folder') with attribute 'folded' set to either 'yes' or 'no'.
The result shall display the first two levels of the list only and not the third level as below.

  • 1st. Level: display
  • 2nd. Level: display
  • 3rd. Level: NO display

The idea is to check the 'folder'-attribute <folder folded="yes"> with <xsl:if test="not(parent::yes)">.
That should qualify for the 3rd. Level to NOT being displayed, but somehow it doesn't do what I want it to do.
I probably use the wrong construct and/or syntax.
Assistance is highly appreciated, thanks.

The XML structure with some content:

<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>

The 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>

The Stylesheet HTML

<body>

<ul>
    <xsl:apply-templates mode="linklist" />
</ul>

</body>

The generated output (levels: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.

The anticipated output: (levels: 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 ...

I tried this template, but that outputs the last two nodes, I need the two first nodes.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

辞旧 2024-12-07 16:39:24

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

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

所有不具有folded 属性等于 no 将使用您现有的模板进行处理;那些这样做的人将被这个新的人捕获。

相反,如果您想要处理每个 folded 元素,其自己的 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" />

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):

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

All folder elements not having a folded attribute equal to no 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 own folded attribute equal to yes or that of its parent (as in the updated XML example), then use the following template:

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

You'll probably also want to include an empty template for hiding all other folder elements:

<xsl:template match="folder" mode="linklist" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文