我有以下形式的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