我很难编写一个从节点结构中生成面包屑测试的模板。到目前为止,它还没有正常工作,在我的思考中存在一些缺陷,我认为它应该如何走项路径。
考虑以下页面结构:
<!-- ===== SITE PAGE STRUCTURE ===================================== -->
<index>
<item section="home" id="index"></item>
<item section="service" id="index">
<item id="content-management-systems">
<item id="p1-1"/>
<item id="p1-2"/>
<item id="p1-3"/>
</item>
<item id="online-stores"></item>
<item id="search-engines-and-ir"></item>
<item id="web-applications"></item>
</item>
<item section="solutions" id="index">
<item id="document-clustering"></item>
</item>
<item section="company" id="index">
<item section="company" id="about"></item>
<item section="company" id="philosophy" ></item>
...
</item>
...
</item>此站点索引表示其层次结构中xml内容页的站点结构(将其视为菜单)。它包含一些部分,它们代表网站的各个部分,如主页、公司、服务、解决方案等。这些部分可以包含有页面的子部分,或者仅仅包含常规的内容页面。内容页(其xml内容,如标题、文本内容等)由项目树中的@id属性标识。@id属性主要用于获取将呈现给html的整个页面的内容。面包屑模板使用item节点@id属性来获取页面的标题(将显示在面包屑跟踪中)。
我尝试通过检查树中的目标节属性@节和目标页属性@id来实现以下模板,该模板遍历树。我希望通过比较祖先@ item_target属性和该轴中每个节点的@id和$item_target,沿着轴向下走,直到找到目标$item_target。
例如:属性*$item_section=service*和页id *目标项目_目标=p1-1*现在应该递归地“遍历”到分支"service“(深度1),检查目标页面@id是否在此级别上找到。在本例中,没有找到它,因此它会将下一个递归调用(通过应用-模板)进行到下一个项目节点级别(在本例中,目标项页p1-1将是content-management-systems,,因此跟踪过程就完成了:
结果应该是这样:
home >> service >>内容管理系统>> p1-1
但不幸的是,它并不正确,至少在每一种情况下都是如此。也许它可以更容易地解决。我尝试将其实现为一个递归模板,从顶部(0级)以叶的形式从目标页面(item节点)走到目标页面(item节点)。
<!-- walk item path to generate a breadcrumb trail -->
<xsl:template name="breadcrumb">
<a>
<xsl:attribute name="href">
<xsl:text>/</xsl:text>
<xsl:value-of select="$req-lg"/>
<xsl:text>/home/index</xsl:text>
</xsl:attribute>
<xsl:value-of select="'Home'"/>
</a>
<xsl:apply-templates select="$content/site/index" mode="Item-Path">
<xsl:with-param name="item_section" select="'service'"/>
<xsl:with-param name="item_target" select="'search-engines-and-ir'"/>
<xsl:with-param name="depth" select="0"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="item" mode="Item-Path">
<xsl:param name="item_section" />
<xsl:param name="item_target" />
<xsl:param name="depth" />
<!--
depth=<xsl:value-of select="$depth"/>
count=<xsl:value-of select="count(./node())"/><br/>
-->
<xsl:variable name="cur-id" select="@id"/>
<xsl:variable name="cur-section" select="@section"/>
<xsl:choose>
<xsl:when test="@id=$item_target">
>>
<a>
<xsl:attribute name="href">
<xsl:text>/</xsl:text>
<!-- req-lg: global langauge variable -->
<xsl:value-of select="$req-lg"/>
<xsl:text>/</xsl:text>
<xsl:value-of select="$item_section"/>
<xsl:text>/</xsl:text>
<xsl:if test="$depth = 2">
<xsl:value-of select="../@id"/>
<xsl:text>/</xsl:text>
</xsl:if>
<xsl:value-of select="@id"/>
</xsl:attribute>
<xsl:value-of
select="$content/page[@id=$cur-id]/title"/>
</a>
</xsl:when>
<xsl:otherwise>
<xsl:if test="ancestor-or-self::item/@section = $item_section and count(./node()) > 0">
>>:
<a>
<xsl:attribute name="href">
<xsl:text>/</xsl:text>
<!-- req-lg: global langauge variable -->
<xsl:value-of select="$req-lg"/>
<xsl:text>/</xsl:text>
<xsl:value-of select="$item_section"/>
<xsl:text>/</xsl:text>
<xsl:if test="$depth = 2">
<xsl:value-of select="../@id"/>
<xsl:text>/</xsl:text>
</xsl:if>
<xsl:value-of select="@id"/>
</xsl:attribute>
<xsl:value-of
select="$content/page[@id=$cur-id and @section=$item_section]/title"/>
</a>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
<xsl:apply-templates select="item" mode="Item-Path">
<xsl:with-param name="item_section" select="$item_section"/>
<xsl:with-param name="item_target" select="$item_target"/>
<xsl:with-param name="depth" select="$depth + 1"/>
</xsl:apply-templates>
</xsl:template>因此,作为模板面包屑、目标部分= 'service‘和目标页=’搜索引擎-和ir‘中的硬编码参数,我期望输出如下
home >> service >>搜索引擎和ir
但输出是
home >>内容管理系统>>搜索引擎和ir
这显然是不正确的。
有人能告诉我怎么纠正这个问题吗?避免深度检查将更加优雅,但到目前为止,我还没有想到其他的方法,我相信还有一个更优雅的解决方案。
我使用XSLT1.0 (libxml通过PHP5)。
希望我的问题足够明确,如果没有,请问:-)谢谢您的帮助提前!
发布于 2012-10-05 05:19:26
和这个一样简单
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:key name="kNodeById" match="item" use="@id"/>
<xsl:template match="/">
<xsl:text>home</xsl:text>
<xsl:call-template name="findPath">
<xsl:with-param name="pStart" select="'service'"/>
<xsl:with-param name="pEnd" select="'search-engines-and-ir'"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="findPath">
<xsl:param name="pStart"/>
<xsl:param name="pEnd"/>
<xsl:for-each select=
"key('kNodeById', $pEnd)
[ancestor::item[@section=$pStart]]
[1]
/ancestor-or-self::item
[not(descendant::item[@section=$pStart])]
">
<xsl:value-of select=
"concat('>>', @id[not(../@section)], @section)"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>想要的,正确的结果产生:
home>>service>>search-engines-and-irDo Note:
item (按文档顺序排列),id属性等于$pEnd,面包屑是从它的内部(其section属性等于$pStart的最祖先)生成到该item元素的。//的任何解决方案都高效得多,因为我们使用一个键来有效地定位"end“item元素。II. XSLT2.0解决方案:
更短、更容易--一个XPathe 2.0的单个表达式:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:key name="kNodeById" match="item" use="@id"/>
<xsl:template match="/">
<xsl:value-of select=
"string-join(
(
'home',
key('kNodeById', $pEnd)
[ancestor::item[@section=$pStart]]
[1]
/ancestor-or-self::item
[not(descendant::item[@section=$pStart])]
/(@id[not(../@section)], @section)[1]
),
'>>'
)
"/>
</xsl:template>
</xsl:stylesheet>发布于 2012-10-05 04:51:52
您只需遍历祖先或自我::axis。这很容易做到,不需要递归。例如..。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<html><body>
<xsl:call-template name="bread-crumbs">
<xsl:with-param name="items" select="*/item" />
<xsl:with-param name="section" select="'service'" />
<xsl:with-param name="leaf" select="'p1-2'" />
</xsl:call-template>
</body></html>
</xsl:template>
<xsl:template name="bread-crumbs">
<xsl:param name="items" />
<xsl:param name="section" />
<xsl:param name="leaf" />
<xsl:value-of select="concat($section,'>>')" />
<xsl:for-each select="$items/self::item[@section=$section]//item[@id=$leaf]/
ancestor-or-self::item[not(@section)]">
<xsl:value-of select="@id" />
<xsl:if test="position() != last()">
<xsl:value-of select="'>>'" />
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>...on你的样本输入产量..。
<html>
<body>service>>content-management-systems>>p1-2</body>
</html> ...which呈现为..。
service>>content-management-systems>>p1-2https://stackoverflow.com/questions/12739089
复制相似问题