我有以下代码:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" />
<xsl:key name="categories" match="Category" use="." />
<xsl:key name="clients" match="Category" use ="Category/Client" />
<xsl:template match="/">
<ul id="red" class="treeview-red">
<xsl:for-each select="/Promotions/Promotion/Category[
generate-id(.) = generate-id(key('categories', .)[1])
]">
<xsl:variable name="cname" select="." />
<li>
<span>
<xsl:value-of select="$cname" />
</span>
<xsl:for-each select="/Promotions/Promotion[Category=$cname]">
<ul>
<li>
<span>
<xsl:value-of select="Client" />
</span>
</li>
<ul>
<li>
<span>
<xsl:value-of select="Title" />
</span>
</li>
</ul>
</ul>
</xsl:for-each>
</li>
</xsl:for-each>
</ul>
</xsl:template>
</xsl:stylesheet>
我的XML:
<Promotions>
<Promotion>
<Category>Arts & Entertainment</Category>
<Client>Client 1</Client>
<Title>Get your Free 2</Title>
</Promotion>
<Promotion>
<Category>Arts & Entertainment</Category>
<Client>Client 1</Client>
<Title>Get your Free 4</Title>
</Promotion>
<Promotion>
<Category>Arts & Entertainment</Category>
<Client>Client 1</Client>
<Title>Get your Free 5</Title>
</Promotion>
<Promotion>
<Category>Community & Neighborhood</Category>
<Client>Client 2</Client>
<Title>Get your Free 1</Title>
</Promotion>
<Promotion>
<Category>Education</Category>
<Client>Client 3</Client>
<Title>Get Your Free 3</Title>
</Promotion>
</Promotions>
输出以下内容:
<ul id="red" class="treeview-red">
<li><span>Arts & Entertainment</span><ul>
<li><span>Client 1</span></li>
<ul>
<li><span>Get your Free 2</span></li>
</ul>
</ul>
<ul>
<li><span>Client 1</span></li>
<ul>
<li><span>Get your Free 4</span></li>
</ul>
</ul>
<ul>
<li><span>Client 1</span></li>
<ul>
<li><span>Get your Free 5</span></li>
</ul>
</ul>
</li>
<li><span>Community & Neighborhood</span><ul>
<li><span>Client 2</span></li>
<ul>
<li><span>Get your Free 1</span></li>
</ul>
</ul>
</li>
<li><span>Education</span><ul>
<li><span>Client 3</span></li>
<ul>
<li><span>Get Your Free 3</span></li>
</ul>
</ul>
</li>
</ul>
我希望输出按类别先分类,然后按客户对每个类别进行分组,对此的任何见解都会很棒:
<ul id="red" class="treeview-red">
<li><span>Arts & Entertainment</span><ul>
<li><span>Client 1</span></li>
<ul>
<li><span>Get your Free 2</span></li>
</ul>
<ul>
<li><span>Get your Free 4</span></li>
</ul>
<ul>
<li><span>Get your Free 5</span></li>
</ul>
</ul>
</li>
<li><span>Community & Neighborhood</span><ul>
<li><span>Client 2</span></li>
<ul>
<li><span>Get your Free 1</span></li>
</ul>
</ul>
</li>
<li><span>Education</span><ul>
<li><span>client 3</span></li>
<ul>
<li><span>Get Your Free 3</span></li>
</ul>
</ul>
</li>
</ul>
基本上按类别分组后,我只希望在该类别下看到该客户在该类别下的一个客户。
答案 0 :(得分:9)
如果没有看到XML输入,很难建议对样式表进行更改,因此目前我只能将http://www.biglist.com/lists/xsl-list/archives/200101/msg00070.html作为使用XSLT 1.0进行多级分组的示例。
[edit]:以下是如何应用两级Muenchian分组:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html" indent="yes"/>
<xsl:key name="k1" match="Promotion" use="Category"/>
<xsl:key name="k2" match="Promotion" use="concat(Category, '|', Client)"/>
<xsl:template match="Promotions">
<ul id="red" class="treeview-red">
<xsl:for-each select="Promotion[generate-id() = generate-id(key('k1', Category)[1])]">
<li>
<span>
<xsl:value-of select="Category"/>
</span>
<xsl:for-each select="key('k1', Category)[generate-id() = generate-id(key('k2', concat(Category, '|', Client))[1])]">
<ul>
<li>
<span>
<xsl:value-of select="Client"/>
</span>
<xsl:for-each select="key('k2', concat(Category, '|', Client))">
<ul>
<li>
<span>
<xsl:value-of select="Title"/>
</span>
</li>
</ul>
</xsl:for-each>
</li>
</ul>
</xsl:for-each>
</li>
</xsl:for-each>
</ul>
</xsl:template>
</xsl:stylesheet>