以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 XSL/XSLT/XSL-FO/CSS 』  (http://bbs.xml.org.cn/list.asp?boardid=8)
----  XSL无聊人第一期  (http://bbs.xml.org.cn/dispbbs.asp?boardid=8&rootid=&id=20240)


--  作者:孤独
--  发布时间:7/8/2005 4:39:00 PM

--  XSL无聊人第一期
1.废话
在现在的分类中,多级分类已经十分常见,而且大多数分类的数据通常只依靠数据库的一个表就可以实现.

众所周知,数据库在处理大量数据时会表现出极其强大的优势,但是在少量数据读取时,将会付出相当的代价.我想这同样不需要解释,各位一定清楚:)

因此,A先生决定把他在数据库中的分类转为用XMl方式实现,因为他的树状结构对于分类的描述是十分有好处的,而且它的速度在A先生当前表量的情况下应当会有不错的表现.

但A先生比较懒,他直接Select * from class for xml后就不管了.当然,各位也会知道接下来的工作了.

而且A先生听说XSLT在这方面会有不错的表现,因此它希望在后期的功能上也借助于XSLT来完成.

2.任务

1.把数据库输出的表用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>

2.显示出目标示例中某个节点的父节点,由父节点向子节点排列.

例如.得到一个<root id='3'/>的XML文件,以及一个 目标示例 (可以用Document函数打开,本演示用上面的示例做,就是树状那个).(filename:tree.xml)

那么输出的结果就是:1 - 2 - 3.

3.对节点进行查询.

例如.得到一个<root searchid='4'/>的XML文件,以及一个 目标示例(filename:tree.xml).

输出的结果就是:1 - 2(也就是输出从根节点到这个节点的路径)

4.查找公共祖先

例如得到一个<root id1='4' id2='3' />的XML文件,以及一个 目标示例(Filename:tree.xml).

输出的结果为:1.

希望各位快快使用XSL将此工作解决完毕,我们会对参与人员进行一定奖励.(由于第一期,奖励办法暂无,可由论坛内讨论决定).

将有N组XML测试文件来检测您的XSL:)
-----------------------------------------------------------------------------------------------------------------
感谢Qr审题,^_^...

各位直接贴在下面就可以,主要为了学习:)

[此贴子已经被作者于2005-7-9 9:59:55编辑过]

--  作者:菜籽
--  发布时间:7/8/2005 7:10:00 PM

--  
先报名一个啊
--  作者:lazyy
--  发布时间:7/8/2005 9:07:00 PM

--  
报名,速度可能会比较慢!呵呵
--  作者:菜籽
--  发布时间:7/9/2005 11:25:00 PM

--  
感觉题目看懂了,不好下手。。。。
楼主能不能给点字具体的怎么做,哪怕只是提示。。给大家一点热情。


--  作者:孤独
--  发布时间:7/9/2005 11:57:00 PM

--  
模板递归需要利用好:)
--  作者:菜籽
--  发布时间:7/10/2005 8:08:00 PM

--  
上面的是做成一个xsl文件,还是每个步骤,做一个,我感觉不好写啊
--  作者:孤独
--  发布时间:7/12/2005 8:31:00 AM

--  
以下是引用菜籽在2005-7-10 20:08:46的发言:
上面的是做成一个xsl文件,还是每个步骤,做一个,我感觉不好写啊


随便咯
--  作者:doubleG
--  发布时间:8/16/2005 11:37:00 AM

--  
嘿嘿,好久没来了,我先扔个砖头引玉吧....
只写了一来引(只限于第一个为根节点的方法):
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output method="xml"/>
 <xsl:template match="root">
  <xsl:copy>
   <xsl:call-template name="createChildNode">
    <xsl:with-param name="currentNode" select="item[position()=1]"/>
   </xsl:call-template>
  </xsl:copy>
 </xsl:template>
 <xsl:template name="createChildNode">
  <xsl:param name="currentNode"/>
  <xsl:variable name="currentID" select="$currentNode/@id"/>
  <xsl:element name="{name($currentNode)}">
   <xsl:attribute name="id"><xsl:value-of select="$currentID"/></xsl:attribute>
   <xsl:for-each select="$currentNode/following-sibling::*[@parent=$currentID]">
    <xsl:call-template name="createChildNode">
     <xsl:with-param name="currentNode" select="."/>
    </xsl:call-template>
   </xsl:for-each>
  </xsl:element>
 </xsl:template>
</xsl:stylesheet>

--  作者:Qr
--  发布时间:8/16/2005 1:18:00 PM

--  
厉害!学了两招。

doubleG斑竹好长时间没露面了,不少朋友时常惦记着。


--  作者:doubleG
--  发布时间:8/16/2005 4:01:00 PM

--  
呵呵,前一段时间想转行,后来考虑了这么久,决定还是先不转了,这不就又回来和大家混了。哈哈哈,孤独的这个方法不错,我们可以征集一下比较有趣和实际开发中遇到的问题,我想应该能够出个合集,能够解决一些常见的问题,论坛上有很多的问题都是雷同的,而且XSL1.0已经出来这么久了,能发现的问题解决方法也不少,我们应该把它整出来才好,像VC知识库一样,我们也可以出XML知识库啊!然后在出问题方面可以加大一下宣传力度,争取出一些好题,呵呵
--  作者:孤独
--  发布时间:8/16/2005 4:14:00 PM

--  
GG(姑且这么叫...意思相同)
终于回来拉...
不过,唉,似乎响应的人比较少...
偶不知道放不放偶写D乱78糟D东东呢..
XML吧,其实也不是一种技术....
我们可以理解讨论HTML的论坛,但是也许讨论ini文件的没有.XML吧其实也就是升级版本的INI...虽然xslt有一定用途,但数据转换用DOM也完全可以做.而且有些xml问题可能在比较热的技术社区就解决了,我们的确应该考虑下我们XML论坛的主要发展方向...
--  作者:admin
--  发布时间:8/16/2005 10:19:00 PM

--  
孤独,其实你写得不错啊。

至于XSLT vs. DOM这在架构上是有重大区别的。

XSLT,体现了一种document-oriented的计算

而DOM,完全是基于code的。

也就是说,用户可以在线修改XSLT,以实现页面定制,但用DOM则不行。

关于论坛发展方向,欢迎提供建议。还有doubleG提到的制作XSLT知识库,也是个很好的建议。。

以下是引用孤独在2005-8-16 16:14:38的发言:
GG(姑且这么叫...意思相同)
终于回来拉...
不过,唉,似乎响应的人比较少...
偶不知道放不放偶写D乱78糟D东东呢..
XML吧,其实也不是一种技术....
我们可以理解讨论HTML的论坛,但是也许讨论ini文件的没有.XML吧其实也就是升级版本的INI...虽然xslt有一定用途,但数据转换用DOM也完全可以做.而且有些xml问题可能在比较热的技术社区就解决了,我们的确应该考虑下我们XML论坛的主要发展方向...


[此贴子已经被作者于2005-8-17 12:20:38编辑过]

--  作者:Qr
--  发布时间:8/16/2005 10:43:00 PM

--  
完全赞同admin的观点。

“XML吧其实也就是升级版本的INI”一句感觉挺实在。不少软件已经在使用XML文件代替INI文件进行系统配置。偶个人在接触XML之初,也有孤独这种感觉,早把XML当作INI应用的实际编程中了:)


--  作者:sam
--  发布时间:8/19/2005 11:51:00 AM

--  
俺学习doubleG斑竹,也跟第一个
<?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>



--  作者:sam
--  发布时间:8/19/2005 1:30:00 PM

--  
偶再跟第二个
<?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="searchid" match="item" use="@id"/>
 <xsl:template match="/">
  <xsl:call-template name="print-path">
   <xsl:with-param name="sid" select="'3'"/>
  </xsl:call-template>
 </xsl:template>
 <xsl:template name="print-path">
  <xsl:param name="sid" select="'0'"/>
  <xsl:for-each select="key('searchid',$sid)/ancestor-or-self::*">
   <xsl:value-of select="@id"/>
  </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>
--  作者:sam
--  发布时间:8/19/2005 3:31:00 PM

--  
继续跟第四个,笨拙了
<?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="searchid" match="item" use="@id"/>
 <xsl:template match="/">
  <xsl:call-template name="same-ancestor">
   <xsl:with-param name="id1" select="'3'"/>
   <xsl:with-param name="id2" select="'4'"/>
  </xsl:call-template>
 </xsl:template>
 <xsl:template name="same-ancestor">
  <xsl:param name="id1" select="'0'"/>
  <xsl:param name="id2" select="'0'"/>
  <xsl:variable name="setstr">
   <xsl:for-each select="key('searchid',$id1)/ancestor-or-self::*">
    <xsl:variable name="tmpid" select="generate-id(.)"/>
    <xsl:if test="not(key('searchid',$id2)/ancestor-or-self::*[generate-id(.)=$tmpid])">
     <xsl:value-of select="../@id"/>--
   </xsl:if>
   </xsl:for-each>
  </xsl:variable>
  <xsl:choose>
   <xsl:when test="$setstr != ''">
    <xsl:value-of select="key('searchid',substring-before($setstr,'--'))/@id"/>
   </xsl:when>
   <xsl:otherwise>
    <xsl:value-of select="$id1"/>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>
</xsl:stylesheet>

--  作者:菜籽
--  发布时间:8/22/2005 7:41:00 PM

--  
sam好强!
--  作者:qjqyx
--  发布时间:9/23/2005 5:00:00 PM

--  
其实这个问题不难,我以前做过挺多这方面的东西
所以我也来报个名.
--  作者:pstianye
--  发布时间:11/13/2005 9:39:00 PM

--  
学习中,支持下
--  作者:98900969r
--  发布时间:11/23/2005 11:02:00 AM

--  
先顶一个!再跟一个。先做第一题,假设@parent=0的为根。

<?xml version="1.0" encoding="UTF-8"?>
<!-- flat to nested -->
<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:template match="/root">
    <root>
      <xsl:apply-templates select="item [@parent='0']"/>
    </root>
  </xsl:template>
 
  <xsl:template match="item">
    <item id="{@id}">
      <xsl:apply-templates select="../*  [@parent=current()/@id]"/>
   </item>
  </xsl:template>

</xsl:stylesheet>

[此贴子已经被作者于2005-11-23 15:40:52编辑过]

--  作者:98900969r
--  发布时间:11/23/2005 1:29:00 PM

--  
第二题。

<?xml version="1.0" encoding="UTF-8"?>
<!-- print ancestor or self -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" version="1.0" encoding="UTF-8" indent="yes"/>

  <xsl:param name="theTree" select=" 'tree.xml' "/>

  <xsl:template match="/">
    <xsl:call-template name="parent">
      <xsl:with-param name="childId"><xsl:value-of select="/root/@id"/></xsl:with-param>
    </xsl:call-template>
    <xsl:value-of select="/root/@id"/>
  </xsl:template>
 
  <xsl:template name="parent">
    <xsl:param name="childId"/>
    <xsl:variable name="parent" select="document($theTree)//item [./item/@id=$childId]"/>
    <xsl:if test="$parent">
      <xsl:call-template name="parent">
        <xsl:with-param name="childId" select="$parent/@id"/>
      </xsl:call-template>
      <xsl:value-of select="concat($parent/@id, '-')"/>
    </xsl:if>
  </xsl:template>
 
</xsl:stylesheet>



--  作者:98900969r
--  发布时间:11/23/2005 1:45:00 PM

--  
第三题

<?xml version="1.0" encoding="UTF-8"?>
<!-- print path -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" version="1.0" encoding="UTF-8" indent="yes"/>
  <xsl:param name="theTree" select=" 'tree.xml' "/>
  <xsl:variable name="nId" select="/root/@id"/>

  <xsl:template match="/">
    <xsl:call-template name="parent">
      <xsl:with-param name="childId">
        <xsl:value-of select="$nId"/>
      </xsl:with-param>
    </xsl:call-template>
  </xsl:template>
 
  <xsl:template name="parent">
    <xsl:param name="childId"/>
    <xsl:variable name="parent" select="document($theTree)//item [./item/@id=$childId]"/>
    <xsl:if test="$parent">
      <xsl:call-template name="parent">
        <xsl:with-param name="childId" select="$parent/@id"/>
      </xsl:call-template>
      <xsl:choose>
        <xsl:when test="$parent/item/@id=$nId">
          <xsl:value-of select="$parent/@id"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="concat($parent/@id, '-')"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:if>
  </xsl:template>
 
</xsl:stylesheet>

[此贴子已经被作者于2005-11-23 19:08:11编辑过]

--  作者:98900969r
--  发布时间:11/23/2005 3:16:00 PM

--  
第四题。
<?xml version="1.0" encoding="UTF-8"?>
<!-- print nearest common ancestor -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" version="1.0" encoding="UTF-8" indent="yes"/>
 
  <xsl:param name="theTree" select=" 'tree.xml' "/>
  <xsl:variable name="id1" select="/root/@id1"/>
  <xsl:variable name="id2" select="/root/@id2"/>

  <xsl:template match="/">
    <xsl:call-template name="up">
      <xsl:with-param name="nId" select="$id1"/>
    </xsl:call-template>
  </xsl:template>
 
  <xsl:template name="up">
    <xsl:param name="nId"/>
    <xsl:variable name="self" select="document($theTree)//item [@id=$nId]"/>
    <xsl:if test="$self">
      <xsl:choose>
        <xsl:when test="$self/descendant-or-self::item [@id=$id2]">
          <xsl:value-of select="$self/@id"/>
        </xsl:when>
       <xsl:otherwise>
         <xsl:call-template name="up">
           <xsl:with-param name="nId" select="document($theTree)//item/@id [../item/@id=$nId]"/>
         </xsl:call-template>
       </xsl:otherwise>
      </xsl:choose>
    </xsl:if>
  </xsl:template>
 
</xsl:stylesheet>

[此贴子已经被作者于2005-11-23 18:54:13编辑过]

--  作者:gangyaocn
--  发布时间:11/24/2005 7:56:00 PM

--  
强人还不少啊!学习中...
--  作者:sajia
--  发布时间:4/12/2006 10:41:00 PM

--  
哎哟~~看样子我还要努力学啊!!!!!!
98900969R:
     你从什么时候开始学XML呢?
     学会它用拉多少时间呢?
    请告诉我!我现在学拉1个月!!


--  作者:gujietom
--  发布时间:8/20/2006 7:08:00 PM

--  
还是觉得无法下手啊
--  作者:Renee
--  发布时间:10/25/2006 9:12:00 PM

--  
我是做COCOON的, 基本上我感觉老跟没娘的孩子似的. 老是找不到对口的论坛,可以讨论和问问题的.
前辈们要是知道有关的技术热区, 给小妹指点下吧


--  作者:txnnh
--  发布时间:12/20/2006 3:49:00 PM

--  
第二题。

<?xml version="1.0" encoding="UTF-8"?>
<!-- print ancestor or self -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" version="1.0" encoding="UTF-8" indent="yes"/>

  <xsl:param name="theTree" select=" 'tree.xml' "/>

  <xsl:template match="/">
    <xsl:call-template name="parent">
      <xsl:with-param name="childId"><xsl:value-of select="/root/@id"/></xsl:with-param>
    </xsl:call-template>
    <xsl:value-of select="/root/@id"/>
  </xsl:template>

  <xsl:template name="parent">
    <xsl:param name="childId"/>
    <xsl:variable name="parent" select="document($theTree)//item [./item/@id=$childId]"/>执行这一行到底得出怎么样的结果??
    <xsl:if test="$parent">
      <xsl:call-template name="parent">
        <xsl:with-param name="childId" select="$parent/@id"/>
      </xsl:call-template>
      <xsl:value-of select="concat($parent/@id, '-')"/>
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>


--  作者:baddy
--  发布时间:4/2/2007 7:52:00 PM

--  
报名!
--  作者:oracle
--  发布时间:5/18/2007 9:43:00 PM

--  
写的好啊
--  作者:insky
--  发布时间:7/12/2007 4:03:00 PM

--  
楼上高手如云啊~~

刚接触XML ,努力_ing


--  作者:asd_1948
--  发布时间:8/15/2007 4:21:00 PM

--  
学习!111
--  作者:fifastar
--  发布时间:9/5/2007 5:57:00 PM

--  
这个,暂时不太熟,不过会好好学学的
--  作者:lastvagrant
--  发布时间:11/2/2007 12:29:00 AM

--  
刚刚开始学习,不懂,看看热闹
--  作者:Meison
--  发布时间:12/25/2007 5:28:00 PM

--  
学习ing!
--  作者:xml-linguist
--  发布时间:9/29/2008 12:38:00 AM

--  
目前还是天书。
--  作者:wb1125
--  发布时间:1/22/2009 10:49:00 AM

--  
第一题:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
 <xsl:output method="xml"></xsl:output>
 <xsl:key name="keyid" match="item" use="@parent"></xsl:key>
 <xsl:template match="/">
  <xsl:variable name="NodeFirst" select="root/item[position()=1]"></xsl:variable>
  <xsl:call-template name="CreateNode">
   <xsl:with-param name="currentNode" select="$NodeFirst"></xsl:with-param>
  </xsl:call-template>
 </xsl:template>
 <xsl:template name="CreateNode">
  <xsl:param name="currentNode"/>
  <xsl:variable name="curentID" select="$currentNode/@id"></xsl:variable>
  <xsl:element name="{name($currentNode)}">
   <xsl:attribute name="id"><xsl:value-of select="$curentID"/></xsl:attribute>
   <xsl:for-each select="key('keyid',$curentID)">
    <xsl:call-template name="CreateNode">
     <xsl:with-param name="currentNode" select="."/>
    </xsl:call-template>
   </xsl:for-each>
  </xsl:element>
 </xsl:template>
</xsl:stylesheet>
想问下:在DoubleG的回复中:
<xsl:for-each select="$currentNode/following-sibling::*[@parent=$currentID]">
这里面双冒号 :: 有什么作用有什么用法。*是不是表示所有节点
W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
222.656ms