| 首页 >> 网络编程 >> XML >> XSL教程 >> 新闻正文 | [字体:大 中 小] [打印文档] |
| |
|
|
xml没有递增变量的标准方法一旦定义了一个变量,他就不能改变 这相当于java中的final字段不过在一些情况中结合模板的递归方法可以实现类 似的结果 假如xml文件为familyTree.xml <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="familyTree.xslt"?> <person name="Otto"> <person name="Sandra"> <person name="Lichao"> <person name="Zhangsan"/> </person> <person name="Eric"> <person name="HaLi"/> </person> <person name="Lisi"> <person name="HeLi"/> <person name="Andy"/> </person> </person> </person> 这段xml中每个<person>元素可以包含任意个数的<person>子元素 这就叫家族数,在显示这个家族树的时候应根据家族系来缩进文本是个恰当的 做法这就给人们之间关系给一个可视化的表示 例如 Otto Sandra Lichao Zhangsan Eric HaLi Lisi HeLi Andy 这样就得用递归的方法写xslt样式表 familyTree.xslt <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html"/> <xsl:template match="/"> <html> <body> <!-- select the top level person --> <xsl:apply-templates select="person"> <xsl:with-param name="level" select="'0'"/> </xsl:apply-templates> </body> </html> </xsl:template> <!-- Output information for a person and recursively select all children. --> <xsl:template match="person"> <xsl:param name="level"/> <!-- indent according to the level --> <div style="text-indent:{$level}em"> <xsl:value-of select="@name"/> </div> <!-- recursively select children, incrementing the level --> <xsl:apply-templates select="person"> <xsl:with-param name="level" select="$level + 1"/> </xsl:apply-templates> </xsl:template> </xsl:stylesheet> 和通常一样这个样式表以匹配文本的根节点作为开始,并输入一个基本的 html文档,然后它选择<person>根元素将level=0作为参数传递到匹配person的 模板:<xsl:apply-templates select="person"> <xsl:with-param name="level" select="'0'"/> </xsl:apply-templates> 而person模板使用一个html的div在一个新行上显示每个人的名字并以em为单位 设定文本的缩进。最后递归的调用person模板,将$level+1作为参数传递,尽 管这样并不会递增一个已有的变量但是他的确传递了一个新的局部变量到模板 中。该变量的值大于以前的值,与递归的处理的技巧不同,实际上在xslt中并 没有递增变量值的方法 |
