XML应用-利用XML 与XSL 开发一个易于修改和扩充的用户手册[2]

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 alexdoes 的 blog

当我们把包含手册内容的xml文件准备好之后,我们还需要做一个额外的工作,就是将此xml文件复制一份,两个文件分别取名为:pms_help.xml, pms_help_lf.xml,两个文件分别绑定不同的xsl样式文件,以取得不同的显示效果,除此以外,两个xml文件是完全相同的。下载两个xml文件:导航栏xml文件内容栏xml文件

最关键的部分: 开发xsl

1. 首先实现相对简单一点的对应左侧导航栏的xsl文件。

xml文件以一定的结构描述了我们要展现的内容,而在xsl当中,决定了这些内容将以何种方式展现,实际上是xsl是将xml文档格式化为浏览器可以理解的html格式文本,在xsl中首先需要定义根级的模板,实际对应了html文档的head以及body部分,代码如下:

	<xsl:template match="pms_help">

		<html>
			<head>
				<title>index</title>
				<style media="screen">
					<xsl:comment><![cdata[
						body {font-fimaly:'宋体',arial; font-size:9pt; color:#000000;}
					]]></xsl:comment>

				</style>
			</head>
			<body>
				<h2>index</h2>
				<p/>

				<xsl:for-each select="item">
					<xsl:apply-templates select="."/>
				</xsl:for-each>
			</body>
		</html>

	</xsl:template>
 

接下来实现对应处理item部分的模板:

	<xsl:variable name="allparentnode" select="ancestor::item/name"/>
		<xsl:variable name="allchildnode" select="child::item/name"/>

		<xsl:variable name="numofallparentnode" select="count($allparentnode) + 1"/>
		<xsl:variable name="numofallchildnode" select="count($allchildnode)"/>
		<!-- output the index of help begin -->

		<xsl:element name="div">
			<xsl:attribute name="onclick">
				window.open('pms_help.xml#<xsl:number count="item" level="multiple" format="01-01-01"/>','main')
				<!-- <xsl:value-of select="&quot;alert(this.id)&quot;" /> --></xsl:attribute>

			<xsl:attribute name="style"><xsl:value-of select="concat('text-indent:',(($numofallparentnode - 1) * 30),';white-space: nowrap')"/></xsl:attribute>
			<xsl:attribute name="id"><xsl:number count="item" level="multiple" format="01-01-01"/></xsl:attribute>

			<xsl:attribute name="onmouseover"><xsl:value-of select="&quot; this.style.color='red';this.style.cursor='hand' &quot;"/></xsl:attribute>
			<xsl:attribute name="onmouseout"><xsl:value-of select="&quot; this.style.color='black' &quot;"/></xsl:attribute>

			<xsl:choose>
				<xsl:when test="$numofallparentnode = 0">
					<img src="images/page/minus.gif" alt="" border="0"/>

				</xsl:when>
				<xsl:when test="$numofallparentnode != 0 and $numofallchildnode = 0">
					<img src="images/page/passage.gif" alt="" border="0"/>

				</xsl:when>
				<xsl:otherwise>
					<img src="images/page/minus.gif" alt="" border="0"/>
				</xsl:otherwise>

			</xsl:choose>
			<xsl:number count="item" level="multiple"/>_
			<xsl:value-of select="name"/>
		</xsl:element>

		<!-- 如果还存在下一级item,则递归调用此模板 -->
		<xsl:if test="count(item)>0">
			<xsl:apply-templates select="item"/>
		</xsl:if>
	</xsl:template>

 

在上面的代码中,我们通过如下的方式定义变量:

	<xsl:variable name="allparentnode" select="ancestor::item/name"/>

其中,allparentnode是我们为变量取的名字,后面使用select关键字为其赋值,ancestor关键字用于取出后面给定节点的所有父节点,item/name是我们在dtd中定义的节点名称。之后我们可以通过 $allparentnode 来引用这个变量。接下来我们使用:

<xsl:variable name="numofallparentnode" 

select="count($allparentnode) + 1"/>

取出当前节点所有父节点的数量。使用以下代码能够在xsl当中实现一个类似case语句的功能,用来判断是否是叶子节点,以显示不同的图片。

	<xsl:choose>
		<xsl:when test="$numofallparentnode = 0">
			<img src="images/page/minus.gif" alt="" border="0"/>

		</xsl:when>
		<xsl:when test="$numofallparentnode != 0 and $numofallchildnode = 0">
			<img src="images/page/passage.gif" alt="" border="0"/>

		</xsl:when>
		<xsl:otherwise>
			<img src="images/page/minus.gif" alt="" border="0"/>
		</xsl:otherwise>

	</xsl:choose>

由于我们的dtd中不限制item定义的级别,因此,我们需要递归的处理item的显示,使用如下语句:

本文关键:XML应用-利用XML 与XSL 开发一个易于修改和扩充的用户手册
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top