如何使用 XSLT 从平面 XML 列表构建树? [英] How can I build a tree from a flat XML list using XSLT?

查看:22
本文介绍了如何使用 XSLT 从平面 XML 列表构建树?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用极简的 MVC 框架,其中 PHP 控制器DOM 模型 交给 XSLT 视图(参见 okapi).

i use a minimalist MVC framework, where the PHP controler hands the DOM model to the XSLT view (c.f. okapi).

为了构建导航树,我在 MYSQL 中使用了嵌套集.这样,我最终得到了一个如下所示的模型 XML:

in order to build a navigation tree, i used nested sets in MYSQL. this way, i end up with a model XML that looks as follows:

<tree>
    <node>
        <name>root</name>
        <depth>0</depth>
    </node>
    <node>
        <name>TELEVISIONS</name>
        <depth>1</depth>
    </node>
    <node>
        <name>TUBE</name>
        <depth>2</depth>
    </node>
    <node>
        <name>LCD</name>
        <depth>2</depth>
    </node>
    <node>
        <name>PLASMA</name>
        <depth>2</depth>
    </node>
    <node>
        <name>PORTABLE ELECTRONICS</name>
        <depth>1</depth>
    </node>
    <node>
        <name>MP3 PLAYERS</name>
        <depth>2</depth>
    </node>
    <node>
        <name>FLASH</name>
        <depth>3</depth>
    </node>
    <node>
        <name>CD PLAYERS</name>
        <depth>2</depth>
    </node>
    <node>
        <name>2 WAY RADIOS</name>
        <depth>2</depth>
    </node>
</tree>

表示如下结构:

    • 电视
      • 液晶显示
      • 等离子
      • MP3 播放器
        • 闪存

        如何使用 XSLT 将此平面 XML 列表转换为嵌套的 HTML 列表?

        How can I convert this flat XML list to a nested HTML list using XSLT?

        PS:这是Managing Hierarchical Data in MySQL 中的示例树.

        推荐答案

        那种形式的flat list在xslt中很难处理,因为你需要找到下一个分组的位置等等.你能不能用不同的xml?例如,使用平面 xml:

        That form of flat list is very hard to work with in xslt, as you need to find the position of the next grouping, etc. Can you use different xml? For example, with the flat xml:

        <?xml version="1.0" encoding="utf-8" ?>
        <tree>
          <node key="0">root</node>
          <node key="1" parent="0">TELEVISIONS</node>
          <node key="2" parent="1">TUBE</node>
          <node key="3" parent="1">LCD</node>
          <node key="4" parent="1">PLASMA</node>
          <node key="5" parent="0">PORTABLE ELECTRONICS</node>
          <node key="6" parent="5">MP3 PLAYERS</node>
          <node key="7" parent="6">FLASH</node>
          <node key="8" parent="5">CD PLAYERS</node>
          <node key="9" parent="5">2 WAY RADIOS</node>
        </tree>
        

        做起来很简单(非常有效):

        It becomes trivial to do (very efficiently):

        <?xml version="1.0" encoding="utf-8"?>
        <xsl:stylesheet version="1.0"
             xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
          <xsl:key name="nodeChildren" match="/tree/node" use="@parent"/>
          <xsl:template match="tree">
            <ul>
              <xsl:apply-templates select="node[not(@parent)]"/>
            </ul>
          </xsl:template>
          <xsl:template match="node">
            <li>
              <xsl:value-of select="."/>
              <ul>
                <xsl:apply-templates select="key('nodeChildren',@key)"/>
              </ul>
            </li>
          </xsl:template>
        </xsl:stylesheet>
        

        这是一种选择吗?

        当然,如果您将 xml 构建为层次结构,那就更容易了;-p

        Of course, if you build the xml as a hierarchy it is even easier ;-p

        这篇关于如何使用 XSLT 从平面 XML 列表构建树?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆