parser

Написать ответ на текущее сообщение

 

 
   команды управления поиском

XML меню

tema 11.05.2005 11:14

Начал изучать XML и XSL.

Пробую выделять пункты меню: простой, внутри пункта, на этом пункте, с помощью параметра selected.

Можно ли задать параметр только у одного элемента, а спомощью xsl разобраться, при неограниченной вложенности?

#################################################################
$document[^xdoc::create{<?xml version="1.0" encoding="windows-1251" ?>
<page>
<title>Заголовок</title>
<description>Описание</description>
<menu level="1">
<menuitem selected="no" link="/about/" name="О авторе" img="" img_in="" img_yes="" id="1"></menuitem>
<menuitem selected="no" link="/company/" name="О компании" img="" img_in="" img_yes="" id="2"></menuitem>
<menuitem selected="no" link="/info/" name="Информация" img="" img_in="" img_yes="" id="3"></menuitem>
<menuitem selected="in" link="/feedback/" name="Контакты" img="" img_in="" img_yes="" id="4">
<menu level="2">
<menuitem selected="in" link="/feedback/phone/" name="Телефоны" img="" img_in="" img_yes="" id="5">
<menu level="3">
<menuitem selected="in" link="/feedback/phone/1/" name="Отдел 1" img="" img_in="" img_yes="" id="6">
<menu level="4">
<menuitem selected="yes" link="/feedback/phone/1/1/" name="1" img="" img_in="" img_yes="" id="7"></menuitem>
<menuitem selected="no" link="/feedback/phone/1/2/" name="2" img="" img_in="" img_yes="" id="8"></menuitem>
<menuitem selected="no" link="/feedback/phone/1/3/" name="3" img="" img_in="" img_yes="" id="9"></menuitem>
</menu>
</menuitem>
<menuitem selected="no" link="/feedback/phone/2/" name="Отдел 2" img="" img_in="" img_yes="" id="10"></menuitem>
<menuitem selected="no" link="/feedback/phone/3/" name="Отдел 3" img="" img_in="" img_yes="" id="11"></menuitem>
</menu>
</menuitem>
<menuitem selected="no" link="/feedback/place/" name="Расположение" img="" img_in="" img_yes="" id="12"></menuitem>
<menuitem selected="no" link="/feedback/mail/" name="Форма" img="" img_in="" img_yes="" id="13"></menuitem>
</menu>
</menuitem>
</menu>

<content>
text
</content>

</page>}]
#################################################################
$style[^xdoc::create{<?xml version="1.0" encoding="windows-1251" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:template match="/">
<html>
<head>
<title><xsl:value-of select="//title" /></title>
<meta name="description">
<xsl:attribute name="content"><xsl:value-of select="//description" /></xsl:attribute>
</meta>
<meta name="keywords" content="" />
<link rel="stylesheet" type="text/css" href="xml.css"/>
</head>
<body>

<table width="100%" cellspacing="0" cellpadding="0" border="1">
<tr>
<td width="20%" valign="top"><spacer width="1" height="1"/></td>
<td width="80%" valign="top">
menu
</td>
</tr>
<tr>
<td colspan="2"><spacer width="1" height="1"/></td>
</tr>
</table>

<table>
<tr>
<td width="20%" valign="top">
<xsl:apply-templates select="//menu[@level='1']" />
</td>
<td width="80%" valign="top">
<xsl:value-of select="//content" />
</td>
</tr>
</table>
</body>
</html>
</xsl:template>

<xsl:template match="//menu">
<xsl:choose>
<!-- level 1 -->
<xsl:when test="@level='1'">
<table>
<xsl:for-each select="menuitem">
<xsl:apply-templates select="." />
</xsl:for-each>
</table>&#xa0^;
</xsl:when>
<!-- level 2 -->
<xsl:when test="@level='2'">
<xsl:for-each select="menuitem">
<xsl:apply-templates select="." />
</xsl:for-each>
</xsl:when>
<!-- level 3 -->
<xsl:when test="@level='3'">
<xsl:for-each select="menuitem">
<xsl:apply-templates select="." />
</xsl:for-each>
</xsl:when>
<!-- level 4 -->
<xsl:when test="@level='4'">
<xsl:for-each select="menuitem">
<xsl:apply-templates select="." />
</xsl:for-each>
</xsl:when>
</xsl:choose>
</xsl:template>

<xsl:template match="//menu[@level='1']/menuitem">
<xsl:choose>
<xsl:when test="@selected='no'"><tr><td><xsl:attribute name="bgcolor">blue</xsl:attribute>
<a><xsl:attribute name="href"><xsl:value-of select="@link" /></xsl:attribute><xsl:value-of select="@name" /></a></td></tr>
</xsl:when>

<xsl:when test="@selected='in'"><tr><td><xsl:attribute name="bgcolor">red</xsl:attribute>
<a><xsl:attribute name="href"><xsl:value-of select="@link" /></xsl:attribute><xsl:value-of select="@name" /></a></td></tr>
<xsl:apply-templates select="menu[@level='2']" />
</xsl:when>

<xsl:when test="@selected='yes'"><tr><td><xsl:attribute name="bgcolor">green</xsl:attribute>
<a><xsl:attribute name="href"><xsl:value-of select="@link" /></xsl:attribute><xsl:value-of select="@name" /></a></td></tr>
<xsl:apply-templates select="menu[@level='2']" />
</xsl:when>
</xsl:choose>
</xsl:template>

<xsl:template match="//menu[@level='2']/menuitem">
<xsl:choose>
<xsl:when test="@selected='no'"><tr><td><xsl:attribute name="bgcolor">#666666</xsl:attribute>
--<a><xsl:attribute name="href"><xsl:value-of select="@link" /></xsl:attribute><xsl:value-of select="@name" /></a></td></tr>
</xsl:when>

<xsl:when test="@selected='in'"><tr><td><xsl:attribute name="bgcolor">black</xsl:attribute>
--<a><xsl:attribute name="href"><xsl:value-of select="@link" /></xsl:attribute><xsl:value-of select="@name" /></a></td></tr>
<xsl:apply-templates select="menu[@level='3']" />
</xsl:when>

<xsl:when test="@selected='yes'"><tr><td><xsl:attribute name="bgcolor">lime</xsl:attribute>
--<a><xsl:attribute name="href"><xsl:value-of select="@link" /></xsl:attribute><xsl:value-of select="@name" /></a></td></tr>
<xsl:apply-templates select="menu[@level='3']" />
</xsl:when>
</xsl:choose>
</xsl:template>

<xsl:template match="//menu[@level='3']/menuitem">
<xsl:choose>
<xsl:when test="@selected='no'"><tr><td><xsl:attribute name="bgcolor">maroon</xsl:attribute>
----<a><xsl:attribute name="href"><xsl:value-of select="@link" /></xsl:attribute><xsl:value-of select="@name" /></a></td></tr>
</xsl:when>

<xsl:when test="@selected='in'"><tr><td><xsl:attribute name="bgcolor">indigo</xsl:attribute>
----<a><xsl:attribute name="href"><xsl:value-of select="@link" /></xsl:attribute><xsl:value-of select="@name" /></a></td></tr>
<xsl:apply-templates select="menu[@level='4']" />
</xsl:when>

<xsl:when test="@selected='yes'"><tr><td><xsl:attribute name="bgcolor">yellow</xsl:attribute>
-----<a><xsl:attribute name="href"><xsl:value-of select="@link" /></xsl:attribute><xsl:value-of select="@name" /></a></td></tr>
<xsl:apply-templates select="menu[@level='4']" />
</xsl:when>
</xsl:choose>
</xsl:template>

<xsl:template match="//menu[@level='4']/menuitem">
<xsl:choose>
<xsl:when test="@selected='no'"><tr><td><xsl:attribute name="bgcolor">#333333</xsl:attribute>
------ <a><xsl:attribute name="href"><xsl:value-of select="@link" /></xsl:attribute><xsl:value-of select="@name" /></a></td></tr>
</xsl:when>

<xsl:when test="@selected='in'"><tr><td><xsl:attribute name="bgcolor">#333333</xsl:attribute>
------ <a><xsl:attribute name="href"><xsl:value-of select="@link" /></xsl:attribute><xsl:value-of select="@name" /></a></td></tr>
<xsl:apply-templates select="menu[@level='5']" />
</xsl:when>

<xsl:when test="@selected='yes'"><tr><td><xsl:attribute name="bgcolor">#333333</xsl:attribute>
------ <a><xsl:attribute name="href"><xsl:value-of select="@link" /></xsl:attribute><xsl:value-of select="@name" /></a></td></tr>
<xsl:apply-templates select="menu[@level='5']" />
</xsl:when>
</xsl:choose>
</xsl:template>

</xsl:stylesheet>}]

#################################################################
$transformedDoc[^document.transform[$style]]
$html[^transformedDoc.string[$.method[html]]]

<pre>
^taint[html][$html]
</pre>
$html