xsl中怎么由param控制循环?
在写xsl中有一个变量 level =5
我怎么能更具这个变量循环
如java中的
for(i=level;i>0;i--)
{
}
问题点数:20、回复次数:19Top
1 楼karma(无为MS MVP)回复于 2002-03-28 17:52:29 得分 0
if the paramter is not too big, try
<xsl:for-each select="(document('')//namespace::*)[position() <= $level]">
.....
</xsl:for-each>Top
2 楼wangwenyou(王文友)回复于 2002-03-28 17:56:41 得分 10
我想,这是你要的东西
其实很简单,不过如果你装了CookTop的话会更省事,它预置了很多类似的模板
<!-- Use: include this call with the number of iterations
This sample will loop from 1 to 10 including 1 and 10
-->
<xsl:call-template name="for.loop">
<xsl:with-param name="i">1</xsl:with-param>
<xsl:with-param name="count">10</xsl:with-param>
</xsl:call-template>
<!-- Rename "old name" elements to "new name" -->
<xsl:template name="for.loop">
<xsl:param name="i"/>
<xsl:param name="count"/>
<xsl:if test="$i <= $count">
<!-- 循环体 -->
</xsl:if>
<xsl:if test="$i <= $count">
<xsl:call-template name="for.loop">
<xsl:with-param name="i">
<!-- Increment index-->
<xsl:value-of select="$i + 1"/>
</xsl:with-param>
<xsl:with-param name="count">
<xsl:value-of select="$count"/>
</xsl:with-param>
</xsl:call-template>
</xsl:if>
</xsl:template>Top
3 楼hymmyh(羽)回复于 2002-03-28 18:15:50 得分 0
再问一下:---》
在一个过程中能不能直接用
<xsl:param name="Levelnum" select="$Levelnum+1"/>
来改变变量,
有什么别的方法改变变量的值?Top
4 楼karma(无为MS MVP)回复于 2002-03-28 18:21:26 得分 0
no, in XSLT, once you set a value for a parameter, it is not modifiableTop
5 楼wangwenyou(王文友)回复于 2002-03-28 18:39:33 得分 0
可以,这是一个递归的概念Top
6 楼wangwenyou(王文友)回复于 2002-03-28 18:43:27 得分 0
<xsl:with-param name="Levelnum">
<xsl:value-of select="$Levelnum+1"/>
</xsl:with-param>
和<xsl:param name="Levelnum" select="$Levelnum+1"/>
是一回事。
Top
7 楼hymmyh(羽)回复于 2002-03-28 19:02:58 得分 0
我是想用xml和xsl生成树,中间要用到增量,但单用递规增量还没有想好怎么作,有没有这方面的例子?Top
8 楼wangwenyou(王文友)回复于 2002-03-28 19:12:02 得分 0
有两种途径:
一、你可以使用编码来表明数据的父子关系:如Code为0101的记录的父节点编码为01,它的子节点编码为0101??。这样可以使用for-each轻松实现树的显示;
二、如果你永远不想将数据放进数据库,你可以使用树型嵌套的结构来存放你的XML数据,然后使用XSLT通过遍历来生成树(显示给用户的)。Top
9 楼hymmyh(羽)回复于 2002-03-28 19:17:54 得分 0
我想把如下xml
<root value="客户服务中心" right="0" display="true" url="content.asp?page=usa">
<folder value="服务" right="0" display="false" url="content.asp?page=usa">
<document value="服务1" right="0" display="false" url="content.asp?page=usa"/>
<document value="服务2" right="0" display="false" url="content.asp?page=usa"/>
</folder>
<folder value="咨询" right="0" display="false" url="content.asp?page=usa">
<document value="咨询1" right="0" display="false" url="content.asp?page=usa"/>
<document value="咨询2" right="0" display="false" url="content.asp?page=usa"/>
<folder value="咨询3" right="0" display="false" url="content.asp?page=usa">
<document value="咨询31" right="0" display="false" url="content.asp?page=usa"/>
<document value="咨询32" right="0" display="false" url="content.asp?page=usa"/>
</folder>
</folder>
</root>
转成树。生成html后加上javascript来控制树的展开和关闭。Top
10 楼wangwenyou(王文友)回复于 2002-03-28 19:50:24 得分 0
看下面的例子,很简陋,我还有一个嵌套结构跟你的差不多的,但是没加Javasrcipt控制,所有给你这个,你参照它修改应该不太难
Index.xml
<?xml version="1.0" encoding="GB2312"?>
<?xml:stylesheet type="text/xsl" href="Index.xsl"?>
<Root>
<Name>WebOA</Name>
<Node>
<Name>参考文献</Name>
<Describe>现有的OA系统的白皮书、界面和演示</Describe>
<Code>04</Code>
<Type/>
<Path>Reference</Path>
<Link>Reference</Link>
</Node>
<Node>
<Name>技术文摘</Name>
<Describe>相关技术的文档和书籍</Describe>
<Code>02</Code>
<Type/>
<Path>Technology</Path>
<Link>Technology</Link>
</Node>
<Node>
<Name>开发工具</Name>
<Describe>Java和Xml的开发工具;服务器组件</Describe>
<Code>03</Code>
<Type/>
<Path>DevelopTools</Path>
<Link>DevelopTools</Link>
<Node>
<Name>项目</Name>
<Describe>建模、项目管理、团队开发管理工具</Describe>
<Code>0301</Code>
<Type/>
<Path>DevelopTools/Project</Path>
<Link>DevelopTools/Project</Link>
<Node>
<Name>Rational Rose2001</Name>
<Describe>Rational Rose EnterpriseEdition.2001</Describe>
<Code>030101</Code>
<Type>zip</Type>
<Path>DevelopTools/Project/Case/Rational</Path>
<Link>DevelopTools/Project/Case/Rational/RationalRoseEnterpriseEdition.2001.03.00.271.000.zip</Link>
</Node>
Top
11 楼wangwenyou(王文友)回复于 2002-03-28 19:51:01 得分 0
<Node>
<Name>Rational Rose2001注册机</Name>
<Describe>ROSE 2001破解文件</Describe>
<Code>030101</Code>
<Type>zip</Type>
<Path>DevelopTools/Project/Case/Rational</Path>
<Link>DevelopTools/Project/Case/Rational/Rational2001.zip</Link>
</Node>
<Node>
<Name>Rational Rose2001汉化包</Name>
<Describe>覆盖Rational\Rose下的roseres.dll文件</Describe>
<Code>030101</Code>
<Type>zip</Type>
<Path>DevelopTools/Project/Case/Rational</Path>
<Link>DevelopTools/Project/Case/Rational/CRational2001.zip</Link>
</Node>
<Node>
<Name>Rational Rose2000</Name>
<Describe>Rational Rose2000 Enterprise Edition 建模工具</Describe>
<Code>030102</Code>
<Type>exe</Type>
<Path>DevelopTools/Project/Case/Rational</Path>
<Link>DevelopTools/Project/Case/Rational/RationalRose2000EnterpriseEdition.exe</Link>
</Node>
<Node>
<Name>Rational Rose2000注册机</Name>
<Describe>ROSE 2000 ENTERPRISE EDITION V6.5</Describe>
<Code>030102</Code>
<Type>zip</Type>
<Path>DevelopTools/Project/Case/Rational/rs2k.zip</Path>
<Link>DevelopTools/Project/Case/Rational/rs2k.zip</Link>
</Node>
Top
12 楼wangwenyou(王文友)回复于 2002-03-28 19:51:59 得分 0
<Node>
<Name>Rational ClearCase</Name>
<Describe>Rational ClearCase v4.1 软件配置管理,版本控制工具</Describe>
<Code>030103</Code>
<Type>zip</Type>
<Path>DevelopTools/Project/Case/Rational</Path>
<Link>DevelopTools/Project/Case/Rational/clearcase_v4.1.nt_i386.zip</Link>
</Node>
<Node>
<Name>ClearCase4.1破解</Name>
<Describe>Rational ClearCase v4.1破解文件</Describe>
<Code>030104</Code>
<Type>zip</Type>
<Path>DevelopTools/Project/Case/Rational</Path>
<Link>DevelopTools/Project/Case/Rational/ClearCase4.1破解.zip</Link>
</Node>
</Node>
<Node>
<Name>Java</Name>
<Describe>Java开发工具 相关技术文章参看《教程》中的Java主题</Describe>
<Code>0302</Code>
<Type/>
<Path>DevelopTools/Java</Path>
<Link>DevelopTools/Java</Link>
<Node>
<Name>JBuilder4.0</Name>
<Describe>Borland 的Java开发工具;Serial:xa22-?hrs5-2ubgs Key:f2j-46g</Describe>
<Code>030201</Code>
<Type/>
<Path>DevelopTools/Java/JBuilder4</Path>
<Link>DevelopTools/Java/JBuilder4</Link>
</Node>
Top
13 楼hymmyh(羽)回复于 2002-03-28 19:56:51 得分 0
还有xsl文件,Top
14 楼hymmyh(羽)回复于 2002-03-28 20:08:08 得分 0
先谢谢 wangwenyou(王文友)
能不能把xsl文件也贴出来。
我这儿头头急着要这棵树。Top
15 楼hymmyh(羽)回复于 2002-03-28 20:34:46 得分 0
我要下,能不能把xml和xsl email我一份,hym@sunyard.comTop
16 楼wangwenyou(王文友)回复于 2002-03-29 09:22:18 得分 10
不是啊,我都快疯了,昨天本来是想给你完整的文件(因为比较忙,没时间将原来的数据筛选一下),但CSDN只让我连续回复三次,所以发不出来。
我是在微软IE对XML文件使用的缺省XSL文件基础上修改的,这是最省事的办法。
通过wingate上网的,那头没配SMTP,每次发信都要上WEB页,太费事,所以现在不喜欢写信了,勿怪:)
<?xml version="1.0" encoding="GB2312" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<HTML>
<HEAD>
<STYLE>
BODY {font:x-small 'Verdana'; margin-right:1.5em}
<!-- container for expanding/collapsing content -->
.c {cursor:hand}
<!-- button - contains +/-/nbsp -->
.b {color:red; font-family:'Courier New'; font-weight:bold; text-decoration:none}
<!-- element container -->
.e {margin-left:2em; text-indent:-1em; margin-right:1em;font:'宋体'}
<!-- comment or cdata -->
.k {margin-left:2em; text-indent:-1em; margin-right:1em}
<!-- tag -->
.t {color:balck}
<!-- tag in xsl namespace -->
.xt {color:black}
<!-- attribute in xml or xmlns namespace -->
.ns {color:red}
<!-- attribute in dt namespace -->
.dt {color:green}
<!-- markup characters -->
.m {color:blue}
<!-- text node -->
.tx {font-weight:bold}
<!-- multi-line (block) cdata -->
.db {text-indent:0px; margin-left:1em; margin-top:0px; margin-bottom:0px;
padding-left:.3em; border-left:1px solid #CCCCCC; font:small Courier}
<!-- single-line (inline) cdata -->
.di {font:small Courier}
<!-- DOCTYPE declaration -->
.d {color:blue}
<!-- pi -->
.pi {color:blue}
<!-- multi-line (block) comment -->
.cb {text-indent:0px; margin-left:1em; margin-top:0px; margin-bottom:0px;
padding-left:.3em; font:small Courier; color:#888888}
<!-- single-line (inline) comment -->
.ci {font:small Courier; color:#888888}
PRE {margin:0px; display:inline}
.row {font:9pt 宋体; border-bottom:1px solid #CC88CC}
.Level1 {font:bold 9pt 宋体;background-color:"#DDFFDD";}
.Level2 {background-color:"#FFDDDD";}
.Level3 {background-color:"#DDDDFF";}
</STYLE>
<SCRIPT>
<xsl:comment>
<![CDATA[
//
// 排序
//
function openPage(url)
{
newnormwin=window.open(url,"NewNormWin","toolbar=yes,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=" + (screen.availWidth-2) + ",height=" + (screen.availHeight) + ",top=0,left=0");
//var larg = screen.availWidth-2;
//var altez = screen.availHeight;
//newnormwin.resizeTo(larg,altez);
}
]]>
</xsl:comment>
</SCRIPT>
<SCRIPT><xsl:comment><![CDATA[
// Detect and switch the display of CDATA and comments from an inline view
// to a block view if the comment or CDATA is multi-line.
function f(e)
{
// if this element is an inline comment, and contains more than a single
// line, turn it into a block comment.
if (e.className == "ci") {
if (e.children(0).innerText.indexOf("\n") > 0)
fix(e, "cb");
}
// if this element is an inline cdata, and contains more than a single
// line, turn it into a block cdata.
if (e.className == "di") {
if (e.children(0).innerText.indexOf("\n") > 0)
fix(e, "db");
}
// remove the id since we only used it for cleanup
e.id = "";
}
// Fix up the element as a "block" display and enable expand/collapse on it
function fix(e, cl)
{
// change the class name and display value
e.className = cl;
e.style.display = "block";
// mark the comment or cdata display as a expandable container
j = e.parentElement.children(0);
j.className = "c";
// find the +/- symbol and make it visible - the dummy link enables tabbing
k = j.children(0);
k.style.visibility = "visible";
k.href = "#";
}
// Change the +/- symbol and hide the children. This function works on "element"
// displays
function ch(e)
{
// find the +/- symbol
mark = e.children(0).children(0);
// if it is already collapsed, expand it by showing the children
if (mark.innerText == "+")
{
mark.innerText = "-";
for (var i = 1; i < e.children.length; i++)
e.children(i).style.display = "block";
}
// if it is expanded, collapse it by hiding the children
else if (mark.innerText == "-")
{
mark.innerText = "+";
for (var i = 1; i < e.children.length; i++)
e.children(i).style.display="none";
}
}
// Change the +/- symbol and hide the children. This function work on "comment"
// and "cdata" displays
function ch2(e)
{
// find the +/- symbol, and the "PRE" element that contains the content
mark = e.children(0).children(0);
contents = e.children(1);
// if it is already collapsed, expand it by showing the children
if (mark.innerText == "+")
{
mark.innerText = "-";
// restore the correct "block"/"inline" display type to the PRE
if (contents.className == "db" || contents.className == "cb")
contents.style.display = "block";
else contents.style.display = "inline";
}
// if it is expanded, collapse it by hiding the children
else if (mark.innerText == "-")
{
mark.innerText = "+";
contents.style.display = "none";
}
}
// Handle a mouse click
function cl()
{
e = window.event.srcElement;
// make sure we are handling clicks upon expandable container elements
if (e.className != "c")
{
e = e.parentElement;
if (e.className != "c")
{
return;
}
}
e = e.parentElement;
// call the correct funtion to change the collapse/expand state and display
if (e.className == "e")
ch(e);
if (e.className == "k")
ch2(e);
}
// Dummy function for expand/collapse link navigation - trap onclick events instead
function ex()
{}
// Erase bogus link info from the status window
function h()
{
window.status=" ";
}
// Set the onclick handler
document.onclick = cl;
]]></xsl:comment></SCRIPT>
</HEAD>
<BODY class="st">
<xsl:apply-templates/>
</BODY>
</HTML>
</xsl:template>
Top
17 楼wangwenyou(王文友)回复于 2002-03-29 09:23:18 得分 0
<!-- Template for elements with comment, pi and/or cdata children -->
<xsl:template match="*[node()$and$$not$textNode()]">
<DIV class="e">
<DIV class="c">
<A href="#" onclick="return false" onfocus="h()" class="b">-</A>
<IMG STYLE="border:0">
<xsl:attribute name="src">icon<xsl:value-of select="Type"/>.gif
</xsl:attribute>
<xsl:attribute name="width">16</xsl:attribute>
<xsl:attribute name="height">16</xsl:attribute>
<xsl:attribute name="alt"></xsl:attribute>
</IMG>
<SPAN class="row">
<!--名称链接到Link所指向的地址 -->
<A>
<xsl:attribute name="Href">
javascript:openPage("<xsl:value-of select="Link"/>")
</xsl:attribute>
<xsl:value-of select="Name"></xsl:value-of>
</A>
<xsl:value-of select="Describe"/>
<A>
<xsl:attribute name="Href">
<xsl:value-of select="Path"></xsl:value-of>
</xsl:attribute>
<IMG STYLE="border:0">
<xsl:attribute name="src">go.gif</xsl:attribute>
<xsl:attribute name="width">22</xsl:attribute>
<xsl:attribute name="height">15</xsl:attribute>
<xsl:attribute name="alt"></xsl:attribute>
</IMG>
</A>
</SPAN>
<!--<xsl:apply-templates select="@*"/> -->
</DIV>
<DIV>
<xsl:apply-templates/>
</DIV>
</DIV>
</xsl:template>
<!-- Template for elements with only text children -->
<xsl:template match="*[textNode()$and$$not$(comment()$or$pi()$or$cdata())]">
<!--
<DIV class="e"><DIV STYLE="margin-left:1em;text-indent:-2em">
<SPAN class="b"><xsl:entity-ref name="nbsp"/></SPAN>
<SPAN>
<xsl:attribute name="class">
<xsl:if match="xsl:*">x</xsl:if>t
</xsl:attribute>
</SPAN><xsl:apply-templates select="@*"/><xsl:value-of/>
<SPAN>
<xsl:attribute name="class">
<xsl:if match="xsl:*">x
</xsl:if>t
</xsl:attribute>
</SPAN>
</DIV></DIV>
-->
</xsl:template>
</xsl:stylesheet>Top
18 楼wangwenyou(王文友)回复于 2002-03-29 09:25:08 得分 0
如果需求很简单,你可以省略对PI和Comment的处理
---------The End----------Top
19 楼hymmyh(羽)回复于 2002-03-29 11:15:42 得分 0
谢谢了,呵呵,在你給我这个之前,我只好用jsp加javabean
用jdom解析xml
直接生成html的树了。
呵呵,还是很谢谢,我要copy下去研究一下了。
谢谢2位。Top




