使用 XML 和 XSLT 进行树导航 [英] Tree navigation with XML and XSLT
问题描述
我有这个树结构的 XML (toc.xml):
<item name="child3-2" key="4294967612" subkey=""/></项目><item name="child2-2" key="4294962611" subkey=""><item name="d" key="4294974806" subkey=""/></项目><item name="child2-3" key="4294963611" subkey=""><item name="d" key="4294967661" subkey=""/><item name="PI" key="4294967659" subkey=""/><item name="q" key="4294967660" subkey=""/></项目><item name="child2-4" key="4294964611" subkey=""/><item name="child2-5" key="4294965611" subkey=""><item name="bb" key="4294967616" subkey=""/><item name="bb" key="4294967620" subkey=""/><item name="f" key="4294967615" subkey=""/></项目></项目></项目></toc>
每个键在文档中都是唯一的.
我有一个导入 toc XML 并尝试输出导航的 XSLT:
<xsl:output method="html" indent="no"/><xsl:variable name="id" select="/member/@id"/><xsl:template match="/member"><头><title><xsl:value-of select="/member/name"/></title>头部><身体><div class="导航"><xsl:apply-templates select="document('toc.xml')"/>
<div class="内容"><xsl:apply-templates/>
</html></xsl:模板></xsl>
并想在 HTML 文件中找到一个节点并输出以下 HTML:
...html...<div class="导航"><ul><li><a href="#">top</a><ul><li><a href="#">child1</li><ul><li><a href="#">child2</li><ul><li><a href="#">child3-1</a></li><li><a href="#">child3-2</a></li></ul></li></ul></li></ul></li>
...更多 html...
基本上我想搜索一个节点:item[@key='4294967611'] 并输出所有父节点和直接子节点.
我觉得这应该很容易,但我正在努力寻找有关如何执行此操作的信息.我的 XSLT 知识不是很好.
使用提供的输入(没有唯一的 @key
),这个样式表:
<xsl:param name="key" select="4294967611"/><xsl:template match="item"><xsl:when test="generate-id() =生成 ID(../item[@key=$key][1])和不(项目[@key=$key])"><xsl:call-template name="chain"><xsl:with-param name="parents" select="ancestor-or-self::item"/><xsl:with-param name="childs" select="item"/></xsl:call-template></xsl:when><xsl:否则><xsl:apply-templates/></xsl:否则></xsl:选择></xsl:模板><xsl:模板名称=链"><xsl:param name="parents"/><xsl:param name="childs"/><xsl:if test="$parents"><ul><li><a href="#"><xsl:value-of select="$parents[1]/@name"/></a><xsl:call-template name="chain"><xsl:with-param name="parents" select="$parents[position()!=1]"/><xsl:with-param name="childs" select="$childs"/></xsl:call-template><xsl:if test="count($parents)=1"><ul><xsl:for-each select="$childs"><li><a href="#"><xsl:value-of select="@name"/></a></xsl:for-each></xsl:if></xsl:if></xsl:模板></xsl:stylesheet>
输出:
<li><a href="#">top</a><ul><li><a href="#">child1</a><ul><li><a href="#">child2-1</a><ul><li><a href="#">child3-1</a><li><a href="#">child3-2</a>
如果 @key
是唯一的,这个样式表应该可以工作:
<xsl:key name="itemBykey" match="item" use="@key"/><xsl:param name="key" select="4294967611"/><xsl:template match="/"><xsl:for-each select="key('itemBykey',$key)"><xsl:call-template name="chain"><xsl:with-param name="parents" select="ancestor-or-self::item"/><xsl:with-param name="childs" select="item"/></xsl:call-template></xsl:for-each></xsl:模板><xsl:模板名称=链"><xsl:param name="parents"/><xsl:param name="childs"/><xsl:if test="$parents"><ul><li><a href="#"><xsl:value-of select="$parents[1]/@name"/></a><xsl:call-template name="chain"><xsl:with-param name="parents" select="$parents[position()!=1]"/><xsl:with-param name="childs" select="$childs"/></xsl:call-template><xsl:if test="count($parents)=1"><ul><xsl:for-each select="$childs"><li><a href="#"><xsl:value-of select="@name"/></a></xsl:for-each></xsl:if></xsl:if></xsl:模板></xsl:stylesheet>
I have this tree structured XML (toc.xml):
<?xml version="1.0" encoding="utf-8"?>
<toc>
<item name="top" key="4294967296" subkey="1">
<item name="child1" key="4294967611" subkey="">
<item name="child2-1" key="4294961611" subkey="">
<item name="child3-1" key="4294967613" subkey=""/>
<item name="child3-2" key="4294967612" subkey=""/>
</item>
<item name="child2-2" key="4294962611" subkey="">
<item name="d" key="4294974806" subkey=""/>
</item>
<item name="child2-3" key="4294963611" subkey="">
<item name="d" key="4294967661" subkey=""/>
<item name="PI" key="4294967659" subkey=""/>
<item name="q" key="4294967660" subkey=""/>
</item>
<item name="child2-4" key="4294964611" subkey=""/>
<item name="child2-5" key="4294965611" subkey="">
<item name="bb" key="4294967616" subkey=""/>
<item name="bb" key="4294967620" subkey=""/>
<item name="f" key="4294967615" subkey=""/>
</item>
</item>
</item>
</toc>
Each key will be unique in the document.
I have an XSLT that imports the toc XML and tries to output the navigation:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="no" />
<xsl:variable name="id" select="/member/@id" />
<xsl:template match="/member">
<html>
<head>
<title><xsl:value-of select="/member/name"/></title>
</head>
<body>
<div class="navigation">
<xsl:apply-templates select="document('toc.xml')" />
</div>
<div class="content">
<xsl:apply-templates />
</div>
</body>
</html>
</xsl:template>
</xsl>
And would like to find a node and output the following HTML inside the HTML file:
...html...
<div class="navigation">
<ul>
<li><a href="#">top</a><ul>
<li><a href="#">child1</li><ul>
<li><a href="#">child2</li><ul>
<li><a href="#">child3-1</a></li>
<li><a href="#">child3-2</a></li>
</ul></li>
</ul></li>
</ul></li>
</ul>
</div>
...more html...
Basically I want to search for a node: item[@key='4294967611'] and output all of the parent nodes and the direct children.
I feel this should be really easy but I'm struggling to find information about how to do this. My XSLT knowledge isn't great.
With the provided input (with no unique @key
), this stylesheet:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="key" select="4294967611"/>
<xsl:template match="item">
<xsl:choose>
<xsl:when test="generate-id() =
generate-id(../item[@key=$key][1])
and
not(item[@key=$key])">
<xsl:call-template name="chain">
<xsl:with-param name="parents" select="ancestor-or-self::item"/>
<xsl:with-param name="childs" select="item"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="chain">
<xsl:param name="parents"/>
<xsl:param name="childs"/>
<xsl:if test="$parents">
<ul>
<li>
<a href="#">
<xsl:value-of select="$parents[1]/@name"/>
</a>
<xsl:call-template name="chain">
<xsl:with-param name="parents" select="$parents[position()!=1]"/>
<xsl:with-param name="childs" select="$childs"/>
</xsl:call-template>
<xsl:if test="count($parents)=1">
<ul>
<xsl:for-each select="$childs">
<li>
<a href="#">
<xsl:value-of select="@name"/>
</a>
</li>
</xsl:for-each>
</ul>
</xsl:if>
</li>
</ul>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Output:
<ul>
<li>
<a href="#">top</a>
<ul>
<li>
<a href="#">child1</a>
<ul>
<li>
<a href="#">child2-1</a>
<ul>
<li>
<a href="#">child3-1</a>
</li>
<li>
<a href="#">child3-2</a>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
If @key
were unique, this stylesheet should work:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="itemBykey" match="item" use="@key"/>
<xsl:param name="key" select="4294967611"/>
<xsl:template match="/">
<xsl:for-each select="key('itemBykey',$key)">
<xsl:call-template name="chain">
<xsl:with-param name="parents" select="ancestor-or-self::item"/>
<xsl:with-param name="childs" select="item"/>
</xsl:call-template>
</xsl:for-each>
</xsl:template>
<xsl:template name="chain">
<xsl:param name="parents"/>
<xsl:param name="childs"/>
<xsl:if test="$parents">
<ul>
<li>
<a href="#">
<xsl:value-of select="$parents[1]/@name"/>
</a>
<xsl:call-template name="chain">
<xsl:with-param name="parents" select="$parents[position()!=1]"/>
<xsl:with-param name="childs" select="$childs"/>
</xsl:call-template>
<xsl:if test="count($parents)=1">
<ul>
<xsl:for-each select="$childs">
<li>
<a href="#">
<xsl:value-of select="@name"/>
</a>
</li>
</xsl:for-each>
</ul>
</xsl:if>
</li>
</ul>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
这篇关于使用 XML 和 XSLT 进行树导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!