基于特征属性的 XSLT 嵌套菜单转换 [英] XSLT nested menu transform based on featured attribute
问题描述
我正在编写一个将修改现有菜单输出的应用程序.我有很多方法可以想到做到这一点,我认为其中之一是 XSLT,但我是此类转换的完全初学者.
I am writing an application that will modify existing menu output. I have a number of ways I can think of to do this, one of them I think is XSLT but I am a complete beginner in such transforms.
我希望更改以下内容:
<ul>
<li><a href="#">Item1</a>
<ul>
<li class="featured"><a href="#">Item 1.1</a></li>
<li><a href="#">Item 1.2</a></li>
<li class="featured"><a href="">Item 1.3</a></li>
<li><a href="#">Item 1.4</a></li>
</ul>
</li>
</ul>
新的:
<ul>
<li><a href="#">Item1</a>
<ul>
<li>
<ul>
<li><a href="#">Item 1.1</a></li>
<li><a href="#">Item 1.2</a></li>
<li><a href="#">Item 1.3</a></li>
<li><a href="#">Item 1.4</a></li>
</ul>
</li>
<li>
<ul class="featured">
<li><a href="#">Item 1.1</a></li>
<li><a href="#">Item 1.3</a></li>
</ul>
</li>
</ul>
</li>
</ul>
我有以下问题:
我需要什么转换才能实现上述目的?
What transform would I need for the above?
我将如何限制为仅 n 级深度(在这种情况下,我们不希望包含 ul.ul.ul.li
项的子项,即项 xx)?
How would I constrain to only n-levels deep (in this case we would not want children of the ul.ul.ul.li
items, i.e. Item x.x, to be included)?
如果我只想将此转换应用于顶部 <ul>
具有属性 class="style1"
的样式,并且如果它已设置,该怎么办对 "style2"
应用了不同的转换?
What if I wanted to only apply this transform to a style where the top <ul>
has an attribute class="style1"
, and if it is set to "style2"
a different transform is applied?
推荐答案
我不是 100% 确定您为什么要添加额外级别的 ul
并且我不确定n-levels"的深层要求(一个更好的例子会有所帮助),但这应该会让你开始:
I'm not 100% sure why you are adding an extra level of ul
and I'm not sure about the "n-levels" deep requirement (a better example would help), but this should get you started:
XSLT 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ul[@class='style1']/li">
<xsl:copy>
<xsl:apply-templates select="@*|*[not(self::ul)]"/>
<ul>
<li>
<xsl:apply-templates select="ul"/>
</li>
<li>
<ul class="featured">
<xsl:apply-templates select="ul/li[@class='featured']"/>
</ul>
</li>
</ul>
</xsl:copy>
</xsl:template>
<xsl:template match="li">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
输出
<ul class="style1">
<li>
<a href="#">Item1</a>
<ul>
<li>
<ul>
<li>
<a href="#">Item 1.1</a>
</li>
<li>
<a href="#">Item 1.2</a>
</li>
<li>
<a href="">Item 1.3</a>
</li>
<li>
<a href="#">Item 1.4</a>
</li>
</ul>
</li>
<li>
<ul class="featured">
<li>
<a href="#">Item 1.1</a>
</li>
<li>
<a href="">Item 1.3</a>
</li>
</ul>
</li>
</ul>
</li>
</ul>
这篇关于基于特征属性的 XSLT 嵌套菜单转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!