xml - 使用 XML 和 XSLT 进行树形导航

标签 xml xslt xhtml treeview

我有这个树形结构的 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>

每个键在文档中都是唯一的。

我有一个 XSLT,它导入目录 XML 并尝试输出导航:

<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>

并且想要找到一个节点并在 HTML 文件中输出以下 HTML:

...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...

基本上我想搜索一个节点:item[@key='4294967611']并输出所有父节点和直接子节点。

我觉得这应该很容易,但我正在努力寻找有关如何做到这一点的信息。我的 XSLT 知识不太丰富。

最佳答案

使用提供的输入(没有唯一的@key),此样式表:

<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>

输出:

<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>

如果 @key 是唯一的,则此样式表应该有效:

<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 - 使用 XML 和 XSLT 进行树形导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3385792/

相关文章:

xml - 使用 XSLT 解析文本文件

javascript - DOMParser 与模板和 innerHTML 的优势

xml - XSLT:如何用所有 sibling 的串联替换第一个 sibling ?

html - 如何使用 CSS 打破 <td> 中的长单词?

css - 等待图像加载后再显示

html - 帮我设计今天的日程表

java - DOM 解析 xml 文件而不转换实体引用

c# - 使用 Linq 递归查询 XML

xml - XSLT 身份转换

xml - 支持 XPath 2.0 的 Java XSLT 处理器