Как преобразование XSLT ниже будет более кратким? Он работает, но я все еще не смог увидеть проблему декларативно (будучи относительным новичком XSLT), и считаю, что это решение является процедурным & довольно подробным решением. Я хотел бы видеть, как кто-то с интуитивным чувством к декларативному подходу решит/упростит его?Более краткий/эффективный XSLT
XSLT используется для построения вертикального навигационного элемента с тремя уровнями глубины. Элемент навигации расширяется/сворачивается в зависимости от выбранного узла. Классы Css (активные) также применяются в зависимости от выбранного уровня.
Требуется 2 параметра, id и query_string.
В XML это особый случайный узел «Новости» - все узлы-потомки этого узла имеют одинаковый идентификатор, поэтому для различения между ними используется query_string.
Вот XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="html" omit-xml-declaration="yes" indent="yes" />
<xsl:param name="id" select="'id:144016'"></xsl:param>
<xsl:param name="query_string" select="'year=2012&month=12'"/>
<xsl:template match="/">
<xsl:choose>
<xsl:when test="//siteMapNode[@id=$id and @query_string=$query_string]">
<xsl:apply-templates select="//siteMapNode[@id=$id and @query_string=$query_string]/ancestor-or-self::*[@depth=1]" />
</xsl:when>
<xsl:when test="//siteMapNode[@id=$id]/ancestor::*[@depth=1]">
<xsl:apply-templates select="//siteMapNode[@id=$id]/ancestor-or-self::*[@depth=1]" />
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="//siteMapNode[@id=$id]" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="siteMapNode/siteMapNode/siteMapNode">
<xsl:variable name="matchidandyearpartofquery" select="count(self::node()[@id=$id and @query_string=substring($query_string, 1, 9)])" />
<xsl:variable name="matchid" select="count(self::node()[@id=$id])" />
<xsl:variable name="matchdescendentid" select="count(self::node()//*[@id=$id and @query_string=substring($query_string, 1, 9)])" />
<li>
<xsl:if test="$matchidandyearpartofquery > 0 or $matchdescendentid > 0">
<xsl:attribute name="class">
<xsl:text>active</xsl:text>
</xsl:attribute>
</xsl:if>
<a>
<xsl:attribute name="href">
<xsl:value-of select="@url" />
</xsl:attribute>
<xsl:value-of select="@title" />
</a>
<xsl:choose>
<xsl:when test="self::node()[@id=$id and @query_string=substring($query_string, 1, 9)]">
<xsl:if test="./*">
<ul>
<xsl:apply-templates select="siteMapNode">
<xsl:sort select="@month" data-type="number" order="descending"/>
</xsl:apply-templates>
</ul>
</xsl:if>
</xsl:when>
<xsl:when test="self::node()//*[@id=$id and @query_string=substring($query_string, 1, 9)]">
<ul>
<xsl:apply-templates select="siteMapNode">
<xsl:sort select="@month" data-type="number" order="descending"/>
</xsl:apply-templates>
</ul>
</xsl:when>
<xsl:when test="self::node()[@id=$id]">
<xsl:if test="./*">
<xsl:if test="./*[not(@id=$id)]">
<ul>
<xsl:apply-templates select="siteMapNode">
<xsl:sort select="@month" data-type="number" order="descending"/>
</xsl:apply-templates>
</ul>
</xsl:if>
</xsl:if>
</xsl:when>
<xsl:when test="self::node()//*[@id=$id]">
<ul>
<xsl:apply-templates select="siteMapNode">
<xsl:sort select="@month" data-type="number" order="descending"/>
</xsl:apply-templates>
</ul>
</xsl:when>
<xsl:otherwise>
</xsl:otherwise>
</xsl:choose>
</li>
</xsl:template>
<xsl:template match="siteMapNode/siteMapNode/siteMapNode/siteMapNode">
<xsl:variable name="matchidandyearpartofquery" select="count(self::node()[@id=$id and @query_string=$query_string])" />
<xsl:variable name="matchid" select="count(self::node()[@id=$id])" />
<xsl:variable name="matchdescendentid" select="count(self::node()//*[@id=$id and @query_string=substring($query_string, 1, 9)])" />
<li>
<xsl:if test="$matchidandyearpartofquery > 0 or $matchdescendentid > 0">
<xsl:attribute name="class">
<xsl:text>active icon-nav-left</xsl:text>
</xsl:attribute>
</xsl:if>
<a>
<xsl:attribute name="href">
<xsl:value-of select="@url" />
</xsl:attribute>
<xsl:value-of select="@title" />
</a>
<xsl:choose>
<xsl:when test="self::node()[@id=$id and @query_string=$query_string]">
<xsl:if test="./*">
<ul>
<xsl:apply-templates select="siteMapNode">
<xsl:sort select="@month" data-type="number" order="descending"/>
</xsl:apply-templates>
</ul>
</xsl:if>
</xsl:when>
<xsl:when test="self::node()//*[@id=$id and @query_string=substring($query_string, 1, 9)]">
<ul>
<xsl:apply-templates select="siteMapNode">
<xsl:sort select="@month" data-type="number" order="descending"/>
</xsl:apply-templates>
</ul>
</xsl:when>
<xsl:when test="self::node()[@id=$id]">
<xsl:if test="./*">
<xsl:if test="./*[not(@id=$id)]">
<ul>
<xsl:apply-templates select="siteMapNode">
<xsl:sort select="@month" data-type="number" order="descending"/>
</xsl:apply-templates>
</ul>
</xsl:if>
</xsl:if>
</xsl:when>
<xsl:otherwise>
</xsl:otherwise>
</xsl:choose>
</li>
</xsl:template>
<xsl:template match="siteMapNode/siteMapNode/siteMapNode/siteMapNode/siteMapNode">
<xsl:variable name="matchidandyearpartofquery" select="count(self::node()[@id=$id and @query_string=$query_string])" />
<li>
<xsl:if test="$matchidandyearpartofquery > 0">
<xsl:attribute name="class">
<xsl:text>active icon-nav-left</xsl:text>
</xsl:attribute>
</xsl:if>
<a>
<xsl:attribute name="href">
<xsl:value-of select="@url" />
</xsl:attribute>
<xsl:value-of select="@title" />
</a>
</li>
</xsl:template>
</xsl:stylesheet>
И некоторые примеры XML вход
<siteMapNode id="id:144037" title="Home" url="index.jsp" depth="0" show_in_top="Yes" show_in_footer="No" use_as_default="No" query_string="" month="" year="">
<siteMapNode id="id:144037" title="Home" url="index.jsp" depth="1" show_in_top="Yes" show_in_footer="No" use_as_default="No" query_string="" month="" year="" />
<siteMapNode id="id:144513" title="Company" url="our-company/index.jsp" depth="1" show_in_top="Yes" show_in_footer="No" use_as_default="No" query_string="" month="" year="">
<siteMapNode id="id:144615" title="At a glance" url="our-company/at-a-glance.jsp" depth="2" show_in_top="No" show_in_footer="No" use_as_default="No" query_string="" month="" year="" />
<siteMapNode id="id:144005" title="Our Brands" url="our-company/our-brands.jsp" depth="2" show_in_top="No" show_in_footer="No" use_as_default="No" query_string="" month="" year="" />
<siteMapNode id="id:144629" title="Our Products" url="our-company/our-products.jsp" depth="2" show_in_top="No" show_in_footer="No" use_as_default="No" query_string="" month="" year="" />
<siteMapNode id="id:144638" title="Our Global Purpose" url="our-company/our-global-purpose.jsp" depth="2" show_in_top="No" show_in_footer="No" use_as_default="No" query_string="" month="" year="" />
<siteMapNode id="id:144002" title="Company History" url="our-company/company-history.jsp" depth="2" show_in_top="No" show_in_footer="No" use_as_default="No" query_string="" month="" year="" />
<siteMapNode id="id:144003" title="Leadership" url="our-company/leadership.jsp" depth="2" show_in_top="No" show_in_footer="No" use_as_default="No" query_string="" month="" year="" />
</siteMapNode>
<siteMapNode id="id:144016" title="News" url="news-press/newslisting.jsp" depth="1" show_in_top="Yes" show_in_footer="No" use_as_default="No" query_string="" month="" year="">
<siteMapNode id="id:144016" title="2012 Archive" url="news-press/newslisting.jsp?year=2012" depth="2" show_in_top="No" show_in_footer="No" use_as_default="No" query_string="year=2012" month="" year="2012">
<siteMapNode id="id:144016" title="December" url="news-press/newslisting.jsp?year=2012&month=12" depth="3" show_in_top="No" show_in_footer="No" use_as_default="No" query_string="year=2012&month=12" month="12" year="2012" />
<siteMapNode id="id:144016" title="April" url="news-press/newslisting.jsp?year=2012&month=4" depth="3" show_in_top="No" show_in_footer="No" use_as_default="No" query_string="year=2012&month=4" month="4" year="2012" />
</siteMapNode>
<siteMapNode id="id:144016" title="2013 Archive" url="news-press/newslisting.jsp" depth="2" show_in_top="No" show_in_footer="No" use_as_default="No" query_string="year=2013" month="" year=""/>
</siteMapNode>
</siteMapNode>
А вот пример ожидаемого вывода HTML (здесь, был выбран месяц декабрь в 2012 году из секции новостей)
<li class="active"><a href="news-press/newslisting.jsp?year=2012">2012 Archive</a>
<ul>
<li class="active icon-nav-left"><a href="news-press/newslisting.jsp?year=2012&month=12">December</a></li>
<li><a href="news-press/newslisting.jsp?year=2012&month=4">April</a></li>
</ul>
</li>
<li><a href="news-press/newslisting.jsp">2013 Archive</a></li>
Если вы могли бы привести пример вашего входного XML, что помогло бы. –
... и ожидаемый результат. –
Я добавил примеры обоих - лучше? – jag