我有以下形式的XML数据创建XML数据的TreeView:如何使用XSLT
<table>
<col>
<name>Addresses</name>
</col>
<col>
<name>Addresses/Address1</name>
</col>
<col>
<name>Addresses/Address2</name>
</col>
<col>
<name>Addresses/Address1/Flat Number</name>
</col>
<col>
<name>Employee Name</name>
</col>
<col>
<name>Phone Number</name>
</col>
<col>
<name>Profession</name>
</col>
<col>
<name>Employee Name/First Name</name>
</col>
<col>
<name>Employee Name/Last Name</name>
</col>
<col>
<name>Employee Name/First_Name</name>
</col>
<col>
<name>Accounts/Account Name/First/Saving</name>
</col>
<col>
<name>Accounts/Account_Name/Second</name>
</col>
</table>
现在我想创建像在使用这个XML XSLT HTML结构的树形。树视图的结构将类似于以下结构:
- 地址
- 地址1
- 地址2
- 员工姓名
- 名
- 姓
- 帐户
- 帐户名称
请注意,“/”是树状的第一级节点和第一次出现后的子字符串“/”是第一次出现之前子匹配的第一级节点的第二级节点,这只是一个2级树视图。
此外,显示的值是唯一的值。即使这些值在XML中重复,我们也只需要选择唯一的一个值。需要考虑的另一个条件是,在示例中给出的“”的值与“”中的值不相同,因此我们需要在用“”替换“ - ”后显示值。
<xsl:variable name="currentNode" select="//table/col" />
<xsl:key name="uniqueCategoryKey" match="record" use="name"/>
<xsl:key name="uniqueSubCategoryKey" match="record" use="substring-before(substring-after(concat(name,'/'),'/'),'/')"/>
<xsl:template match="/">
<xsl:call-template name="treeTemplate" />
</xsl:template>
<xsl:template name="treeTemplate">
<div id ="newtreeview">
<ul>
<xsl:for-each select="$currentNode[generate-id() = generate-id(key('uniqueCategoryKey', name))]">
<xsl:variable name="category" select="name"/>
<xsl:if test="string-length($category) > 0 and not(contains($category,'/'))">
<li>
<a href="#">
<xsl:value-of select="$category"/>
</a>
<xsl:if test="//record[contains(name, concat($category,'/'))]">
<ul>
<xsl:for-each select="$currentNode[generate-id() = generate-id(key('uniqueSubCategoryKey', substring-before(substring-after(concat(name,'/'),'/'),'/')))]">
<li>
<a href="#">
<xsl:variable name="subcat">
<xsl:call-template name="string-replace-all">
<xsl:with-param name="text" select="string(substring-before(substring-after(concat(name,'/'),'/'),'/'))" />
<xsl:with-param name="replace" select="'_'" />
<xsl:with-param name="by" select="' '" />
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="$subcat"/>
</a>
</li>
</xsl:for-each>
</ul>
</xsl:if>
</li>
</xsl:if>
</xsl:for-each>
</ul>
</div>
</xsl:template>
<xsl:template name="string-replace-all">
<xsl:param name="text" />
<xsl:param name="replace" />
<xsl:param name="by" />
<xsl:choose>
<xsl:when test="contains($text, $replace)">
<xsl:value-of select="substring-before($text,$replace)" />
<xsl:value-of select="$by" />
<xsl:call-template name="string-replace-all">
<xsl:with-param name="text"
select="substring-after($text,$replace)" />
<xsl:with-param name="replace" select="$replace" />
<xsl:with-param name="by" select="$by" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
的问题是,这个代码是给我像
- 地址
-
-
输出 -
- 地址1
- 地址2
-
- 员工姓名
-
-
-
- 名
- 名
- 姓
-
- 账户
- 帐户名称
- 帐户名称
请帮我解决这个问题。
在此先感谢。
您可以提供可能的工作示例XML! – Treemonkey 2012-01-06 14:49:32