我正在编写一个修改现有菜单输出的应用程序。我有很多方法可以做到这一点,其中一个我认为是XSLT,但我是这种变换的完全初学者。
我希望更改以下内容:
<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>
我有以下问题:
我需要对上述内容进行哪些转换?
我如何限制只有n级深度(在这种情况下,我们不希望包含ul.ul.ul.li
项目的子项,即项目x.x?
如果我只希望将此变换应用于顶部<ul>
具有属性class="style1"
的样式,并且如果设置为"style2"
,则会应用不同的变换,该怎么办?< / p>
答案 0 :(得分:1)
你可以尝试这个XSLT
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:php="http://php.net/xsl" exclude-result-prefixes="php" version="1.0">
<xsl:output method="html" version="1.0" encoding="utf-8" indent="yes" omit-xml-declaration="no"/>
<xsl:template match="/">
<div id="menu">
<ul id="mega">
<xsl:for-each select="//Menu/MainMenu">
<xsl:choose>
<xsl:when test="@isdisplay=1 and @iscategory=1">
<xsl:element name="li">
<xsl:attribute name="style">
border-left:#ffffff 1px solid;
</xsl:attribute>
<xsl:attribute name="class">
<xsl:value-of select="@class"/>
</xsl:attribute>
<xsl:variable name="name">
<xsl:value-of select="@name"/>
</xsl:variable>
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of select="php:function('ClsXSLTFunction::makeFriendlyURL','Category.php',$name,'','','')"/>
</xsl:attribute>
<xsl:value-of select="@displayname"/>
</xsl:element>
<xsl:if test="count(SubMenu) > 0">
<xsl:choose>
<xsl:when test="@name='Outdoor Gear'">
<div>
<xsl:apply-templates select="//Menu/MainMenu[@name='Outdoor Gear']">
</xsl:apply-templates>
</div>
</xsl:when>
<xsl:when test="@name='Clothing / Footwear'">
<div style="width:490px !important;">
<xsl:apply-templates select="//Menu/MainMenu[@name='Clothing / Footwear']"></xsl:apply-templates>
</div>
</xsl:when>
<xsl:when test="@name='Electronics'">
<div style="width:200px !important;">
<xsl:apply-templates select="//Menu/MainMenu[@name='Electronics']"></xsl:apply-templates>
</div>
</xsl:when>
<xsl:when test="@name='Gifts'">
<div style="width:200px !important;">
<xsl:apply-templates select="//Menu/MainMenu[@name='Gifts']"></xsl:apply-templates>
</div>
</xsl:when>
<xsl:when test="@name='Petzl'">
<div style="width:200px !important;">
<xsl:apply-templates select="//Menu/MainMenu[@name='Petzl']"></xsl:apply-templates>
</div>
</xsl:when>
</xsl:choose>
</xsl:if>
</xsl:element>
</xsl:when>
<xsl:when test="@isdisplay=1 and @iscategory=0">
<xsl:element name="li">
<xsl:attribute name="style">
border-left:#ffffff 1px solid;
</xsl:attribute>
<xsl:attribute name="class">
<xsl:value-of select="@class"/>
</xsl:attribute>
<xsl:variable name="name">
<xsl:value-of select="@name"/>
</xsl:variable>
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of select="php:function('ClsXSLTFunction::makeFriendlyURL','',$name,'','','')"/>
</xsl:attribute>
<xsl:value-of select="@displayname"/>
</xsl:element>
</xsl:element>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</ul>
</div>
</xsl:template>
<xsl:template match="//Menu/MainMenu[@name='Outdoor Gear']">
<span class="main">
<h2>
<xsl:variable name="name">
<xsl:value-of select="SubMenu[@name='Water Sport']/@name"/>
</xsl:variable>
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of select="php:function('ClsXSLTFunction::makeFriendlyURL','Category.php','Outdoor Gear',$name,'','')"/>
</xsl:attribute>
<xsl:value-of select="SubMenu[@name='Water Sport']/@displayname" disable-output-escaping="yes"/>
</xsl:element>
</h2>
<xsl:for-each select="SubMenu[@name='Water Sport']/SubSubMenu">
<xsl:if test="@isdisplay=1">
<span class="inner">
<xsl:variable name="name">
<xsl:value-of select="//Menu/MainMenu[@name='Outdoor Gear']/SubMenu[@name='Water Sport']/@name"/>
</xsl:variable>
<xsl:variable name="name1">
<xsl:value-of select="@name"/>
</xsl:variable>
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of select="php:function('ClsXSLTFunction::makeFriendlyURL','Category.php','Outdoor Gear',$name,$name1,'')"/>
</xsl:attribute>
<xsl:value-of select="@displayname" disable-output-escaping="yes"/>
</xsl:element>
</span>
</xsl:if>
<xsl:for-each select="ChildMenu">
<xsl:if test="@isdisplay=1">
<span class="inner">
<xsl:variable name="name">
<xsl:value-of select="../../@name"/>
</xsl:variable>
<xsl:variable name="name1">
<xsl:value-of select="../@name"/>
</xsl:variable>
<xsl:variable name="name2">
<xsl:value-of select="@name"/>
</xsl:variable>
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of select="php:function('ClsXSLTFunction::makeFriendlyURL','Category.php','Outdoor Gear',$name,$name1,$name2)"/>
</xsl:attribute>
<xsl:value-of select="@displayname" disable-output-escaping="yes"/>
</xsl:element>
</span>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
<br clear="all" />
<h2>
<xsl:variable name="name">
<xsl:value-of select="SubMenu[@name='Archery']/@name"/>
</xsl:variable>
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of select="php:function('ClsXSLTFunction::makeFriendlyURL','Category.php','Outdoor Gear',$name,'','')"/>
</xsl:attribute>
<xsl:value-of select="SubMenu[@name='Archery']/@displayname" disable-output-escaping="yes"/>
</xsl:element>
</h2>
<xsl:for-each select="SubMenu[@name='Archery']/SubSubMenu">
<xsl:if test="@isdisplay=1">
<span class="inner">
<xsl:variable name="name">
<xsl:value-of select="//Menu/MainMenu[@name='Outdoor Gear']/SubMenu[@name='Archery']/@name"/>
</xsl:variable>
<xsl:variable name="name1">
<xsl:value-of select="@name"/>
</xsl:variable>
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of select="php:function('ClsXSLTFunction::makeFriendlyURL','Category.php','Outdoor Gear',$name,$name1,'')"/>
</xsl:attribute>
<xsl:value-of select="@displayname" disable-output-escaping="yes"/>
</xsl:element>
</span>
</xsl:if>
<xsl:for-each select="ChildMenu">
<xsl:if test="@isdisplay=1">
<span class="inner">
<xsl:variable name="name">
<xsl:value-of select="../../@name"/>
</xsl:variable>
<xsl:variable name="name1">
<xsl:value-of select="../@name"/>
</xsl:variable>
<xsl:variable name="name2">
<xsl:value-of select="@name"/>
</xsl:variable>
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of select="php:function('ClsXSLTFunction::makeFriendlyURL','Category.php','Outdoor Gear',$name,$name1,$name2)"/>
</xsl:attribute>
<xsl:value-of select="@displayname" disable-output-escaping="yes"/>
</xsl:element>
</span>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
<h2>
<xsl:variable name="name">
<xsl:value-of select="SubMenu[@name='Climbing']/@name"/>
</xsl:variable>
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of select="php:function('ClsXSLTFunction::makeFriendlyURL','Category.php','Outdoor Gear',$name,'','')"/>
</xsl:attribute>
<xsl:value-of select="SubMenu[@name='Climbing']/@displayname" disable-output-escaping="yes"/>
</xsl:element>
</h2>
<xsl:for-each select="SubMenu[@name='Climbing']/SubSubMenu">
<xsl:if test="@isdisplay=1">
<span class="inner">
<xsl:variable name="name">
<xsl:value-of select="//Menu/MainMenu[@name='Outdoor Gear']/SubMenu[@name='Climbing']/@name"/>
</xsl:variable>
<xsl:variable name="name1">
<xsl:value-of select="@name"/>
</xsl:variable>
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of select="php:function('ClsXSLTFunction::makeFriendlyURL','Category.php','Outdoor Gear',$name,$name1,'')"/>
</xsl:attribute>
<xsl:value-of select="@displayname" disable-output-escaping="yes"/>
</xsl:element>
</span>
</xsl:if>
<xsl:for-each select="ChildMenu">
<xsl:if test="@isdisplay=1">
<span class="inner">
<xsl:variable name="name">
<xsl:value-of select="../../@name"/>
</xsl:variable>
<xsl:variable name="name1">
<xsl:value-of select="../@name"/>
</xsl:variable>
<xsl:variable name="name2">
<xsl:value-of select="@name"/>
</xsl:variable>
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of select="php:function('ClsXSLTFunction::makeFriendlyURL','Category.php','Outdoor Gear',$name,$name1,$name2)"/>
</xsl:attribute>
<xsl:value-of select="@displayname" disable-output-escaping="yes"/>
</xsl:element>
</span>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
<br clear="all" />
<h2>
<xsl:variable name="name">
<xsl:value-of select="SubMenu[@name='Fishing']/@name"/>
</xsl:variable>
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of select="php:function('ClsXSLTFunction::makeFriendlyURL','Category.php','Outdoor Gear',$name,'','')"/>
</xsl:attribute>
<xsl:value-of select="SubMenu[@name='Fishing']/@displayname" disable-output-escaping="yes"/>
</xsl:element>
</h2>
<xsl:for-each select="SubMenu[@name='Fishing']/SubSubMenu">
<xsl:if test="@isdisplay=1">
<span class="inner">
<xsl:variable name="name">
<xsl:value-of select="//Menu/MainMenu[@name='Outdoor Gear']/SubMenu[@name='Fishing']/@name"/>
</xsl:variable>
<xsl:variable name="name1">
<xsl:value-of select="@name"/>
</xsl:variable>
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of select="php:function('ClsXSLTFunction::makeFriendlyURL','Category.php','Outdoor Gear',$name,$name1,'')"/>
</xsl:attribute>
<xsl:value-of select="@displayname" disable-output-escaping="yes"/>
</xsl:element>
</span>
</xsl:if>
<xsl:for-each select="ChildMenu">
<xsl:if test="@isdisplay=1">
<span class="inner">
<xsl:variable name="name">
<xsl:value-of select="../../@name"/>
</xsl:variable>
<xsl:variable name="name1">
<xsl:value-of select="../@name"/>
</xsl:variable>
<xsl:variable name="name2">
<xsl:value-of select="@name"/>
</xsl:variable>
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of select="php:function('ClsXSLTFunction::makeFriendlyURL','Category.php','Outdoor Gear',$name,$name1,$name2)"/>
</xsl:attribute>
<xsl:value-of select="@displayname" disable-output-escaping="yes"/>
</xsl:element>
</span>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</span>
<span class="main">
<h2>
<xsl:variable name="name">
<xsl:value-of select="SubMenu[@name='Track / Field / Court / Indoors']/@name"/>
</xsl:variable>
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of select="php:function('ClsXSLTFunction::makeFriendlyURL','Category.php','Outdoor Gear',$name,'','')"/>
</xsl:attribute>
<xsl:value-of select="SubMenu[@name='Track / Field / Court / Indoors']/@displayname" disable-output-escaping="yes"/>
</xsl:element>
</h2>
<xsl:for-each select="SubMenu[@name='Track / Field / Court / Indoors']/SubSubMenu">
<xsl:if test="@isdisplay=1">
<span class="inner">
<xsl:variable name="name">
<xsl:value-of select="//Menu/MainMenu[@name='Outdoor Gear']/SubMenu[@name='Track / Field / Court / Indoors']/@name"/>
</xsl:variable>
<xsl:variable name="name1">
<xsl:value-of select="@name"/>
</xsl:variable>
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of select="php:function('ClsXSLTFunction::makeFriendlyURL','Category.php','Outdoor Gear',$name,$name1,'')"/>
</xsl:attribute>
<xsl:value-of select="@displayname" disable-output-escaping="yes"/>
</xsl:element>
</span>
</xsl:if>
<xsl:for-each select="ChildMenu">
<xsl:if test="@isdisplay=1">
<span class="inner">
<xsl:variable name="name">
<xsl:value-of select="../../@name"/>
</xsl:variable>
<xsl:variable name="name1">
<xsl:value-of select="../@name"/>
</xsl:variable>
<xsl:variable name="name2">
<xsl:value-of select="@name"/>
</xsl:variable>
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of select="php:function('ClsXSLTFunction::makeFriendlyURL','Category.php','Outdoor Gear',$name,$name1,$name2)"/>
</xsl:attribute>
<xsl:value-of select="@displayname" disable-output-escaping="yes"/>
</xsl:element>
</span>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
<br clear="all" />
<h2>
<xsl:variable name="name">
<xsl:value-of select="SubMenu[@name='Hiking and Camping']/@name"/>
</xsl:variable>
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of select="php:function('ClsXSLTFunction::makeFriendlyURL','Category.php','Outdoor Gear',$name,'','')"/>
</xsl:attribute>
<xsl:value-of select="SubMenu[@name='Hiking and Camping']/@displayname" disable-output-escaping="yes"/>
</xsl:element>
</h2>
<xsl:for-each select="SubMenu[@name='Hiking and Camping']/SubSubMenu">
<xsl:if test="@isdisplay=1">
<span class="inner">
<xsl:variable name="name">
<xsl:value-of select="//Menu/MainMenu[@name='Outdoor Gear']/SubMenu[@name='Hiking and Camping']/@name"/>
</xsl:variable>
<xsl:variable name="name1">
<xsl:value-of select="@name"/>
</xsl:variable>
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of select="php:function('ClsXSLTFunction::makeFriendlyURL','Category.php','Outdoor Gear',$name,$name1,'')"/>
</xsl:attribute>
<xsl:value-of select="@displayname" disable-output-escaping="yes"/>
</xsl:element>
</span>
</xsl:if>
<xsl:for-each select="ChildMenu">
<xsl:if test="@isdisplay=1">
<span class="inner">
<xsl:variable name="name">
<xsl:value-of select="../../@name"/>
</xsl:variable>
<xsl:variable name="name1">
<xsl:value-of select="../@name"/>
</xsl:variable>
<xsl:variable name="name2">
<xsl:value-of select="@name"/>
</xsl:variable>
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of select="php:function('ClsXSLTFunction::makeFriendlyURL','Category.php','Outdoor Gear',$name,$name1,$name2)"/>
</xsl:attribute>
<xsl:value-of select="@displayname" disable-output-escaping="yes"/>
</xsl:element>
</span>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</span>
</xsl:template>
<xsl:template match="//Menu/MainMenu[@name='Clothing / Footwear']">
<span class="main">
<h2>
<xsl:variable name="name">
<xsl:value-of select="SubMenu[@name='Mountain Running']/@name"/>
</xsl:variable>
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of select="php:function('ClsXSLTFunction::makeFriendlyURL','Category.php','Clothing / Footwear',$name,'','')"/>
</xsl:attribute>
<xsl:value-of select="SubMenu[@name='Mountain Running']/@displayname" disable-output-escaping="yes"/>
</xsl:element>
</h2>
<xsl:for-each select="SubMenu[@name='Mountain Running']/SubSubMenu">
<xsl:if test="@isdisplay=1">
<span class="inner">
<xsl:variable name="name">
<xsl:value-of select="../@name"/>
</xsl:variable>
<xsl:variable name="name1">
<xsl:value-of select="@name"/>
</xsl:variable>
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of select="php:function('ClsXSLTFunction::makeFriendlyURL','Category.php','Clothing / Footwear',$name,$name1,'')"/>
</xsl:attribute>
<xsl:value-of select="@displayname" disable-output-escaping="yes"/>
</xsl:element>
</span>
</xsl:if>
<xsl:for-each select="ChildMenu">
<xsl:if test="@isdisplay=1">
<span class="inner">
<xsl:variable name="name">
<xsl:value-of select="../../@name"/>
</xsl:variable>
<xsl:variable name="name1">
<xsl:value-of select="../@name"/>
</xsl:variable>
<xsl:variable name="name2">
<xsl:value-of select="@name"/>
</xsl:variable>
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of select="php:function('ClsXSLTFunction::makeFriendlyURL','Category.php','Clothing / Footwear',$name,$name1,$name2)"/>
</xsl:attribute>
<xsl:value-of select="@displayname" disable-output-escaping="yes"/>
</xsl:element>
</span>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</span>
</xsl:template>
<xsl:template match="//Menu/MainMenu[@name='Electronics']">
<table width="100%" border="0" cellspacing="2" cellpadding="2">
<tr>
<td height="5"></td>
</tr>
<xsl:for-each select="SubMenu">
<tr>
<td class="">
<xsl:variable name="name">
<xsl:value-of select="@name"/>
</xsl:variable>
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of select="php:function('ClsXSLTFunction::makeFriendlyURL','Category.php','Electronics',$name,'','')"/>
</xsl:attribute>
<xsl:value-of select="@displayname" disable-output-escaping="yes"/>
</xsl:element>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
<xsl:template match="//Menu/MainMenu[@name='Gifts']">
<table width="100%" border="0" cellspacing="2" cellpadding="2">
<tr>
<td height="5"></td>
</tr>
<xsl:for-each select="SubMenu">
<tr>
<td class="">
<xsl:variable name="name">
<xsl:value-of select="@name"/>
</xsl:variable>
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of select="php:function('ClsXSLTFunction::makeFriendlyURL','Category.php','Gifts',$name,'','')"/>
</xsl:attribute>
<xsl:value-of select="@displayname" disable-output-escaping="yes"/>
</xsl:element>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
<xsl:template match="//Menu/MainMenu[@name='Petzl']">
<table width="100%" border="0" cellspacing="2" cellpadding="2">
<tr>
<td height="5"></td>
</tr>
<xsl:for-each select="SubMenu">
<tr>
<td class="">
<xsl:variable name="name">
<xsl:value-of select="@name"/>
</xsl:variable>
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of select="php:function('ClsXSLTFunction::makeFriendlyURL','Category.php','Petzl',$name,'','')"/>
</xsl:attribute>
<xsl:value-of select="@displayname" disable-output-escaping="yes"/>
</xsl:element>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
如果您遇到任何问题,请告诉我。
答案 1 :(得分:1)
我不是100%肯定你为什么要添加额外级别的ul
而我不确定“n级”深度要求(更好的例子会有所帮助),但这应该得到你开始了:
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>