XSLT专业站 - 提供xslt和xml相关的资料书籍和教程  
XSLT专业站 - 提供xslt和xml相关的资料书籍和教程
网站地图  收藏本站
首页 | 热门文章 | 精彩实例 | 经典教程 | XSLT语法详解 | 资料下载 | 休闲天地 | 交流论坛
  当前位置:首页>精彩实例>文章内容
把数据库输表用 XSLT 转化为树状
来源:XSLT.org.cn 作者:XSLT专业站 发布时间:2007-08-04  
把数据库输表用 XSLT 转化为树状

 

题目:

把数据库输出的表用XSLT转化为树状的.

      输出表示例:

<root>
    <item id="1" parent="0" />
    <item id="2" parent="1" />
    <item id="3" parent="2" />
    <item id="4" parent="1" />
</root>


目标示例:

<root>
        <item id="1">
              <item id="2">
                  <item id="3" />
              </item>
              <item id="4"/>
        </item>
</root>

 



实现:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:key name="parentid" match="item" use="@parent"/>
<xsl:template match="/">
  <root>
   <xsl:apply-templates select="root/item[@parent='0']"/>
  </root>
</xsl:template>
<xsl:template match="item">
  <xsl:element name="{name(.)}">
   <xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute>
   <xsl:for-each select="key('parentid',@id)">
    <xsl:apply-templates select="."/>
   </xsl:for-each>
  </xsl:element>
</xsl:template>
</xsl:stylesheet>

 




标题: 把数据库输表用 XSLT 转化为树状
关键字:XSLT 树状 xsl:key
上一篇:利用 XSL 解析 RSS   下一篇:查找节点的父节点
  精彩实例热点文章
·XSLT 实例:使用不同的轴(Axis
·查找公共祖先节点
·利用 xslt 对 xml 进行缩进格式
·XSLT 实例:创建带有“前一页”
·利用 XSL 解析 RSS
·查找节点的父节点
·用 XML/XSL 来生成动态页面
·利用 XSL 对 XML 数据进行加密和
·XSLT 实例:统计作者出版物的销
·XSLT 实例:使用变量(Variable)
  精彩实例相关文章
·利用 XSL 解析 RSS
·查找节点的父节点
·查找公共祖先节点
·用 XML/XSL 来生成动态页面
·利用 xslt 对 xml 进行缩进格式
·利用 XSL 对 XML 数据进行加密和
·XSLT 实例:使用不同的轴(Axis
·XSLT 实例:统计作者出版物的销
·XSLT 实例:创建带有“前一页”
·XSLT 实例:使用变量(Variable)
Copyright© 2007 xslt.org.cn All rights reserved.