【发布时间】:2011-01-11 16:45:03
【问题描述】:
我有以下 XML 文件:
<Promotions>
<Promotion>
<Category>Arts & Entertainment</Category>
<Client>Client Five</Client>
<Title>Get your Free 2</Title>
</Promotion>
<Promotion>
<Category>Arts & Entertainment</Category>
<Client>Client 5</Client>
<Title>Get your Free 4</Title>
</Promotion>
<Promotion>
<Category>Arts & Entertainment</Category>
<Client>Client five</Client>
<Title>Get your Free 5</Title>
</Promotion>
<Promotion>
<Category>Community & Neighborhood</Category>
<Client>Client 1</Client>
<Title>Get your Free 1</Title>
</Promotion>
<Promotion>
<Category>Education</Category>
<Client>Client 3</Client>
<Title>Get Your Free 3</Title>
</Promotion>
我想按类别分组。我尝试了以下方法并不断收到错误:
string xslmarkup = @"
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:output method='html' />
<xsl:key name='Categories' match='Promotions/Promotion' use='Category'/>
<xsl:template match='/'>
<xsl:apply-templates select='
/Promotions/Promotion[
generate-id()
=
generate-id(key ('Categories',Category)[1])
]
'/>
</xsl:template>
<xsl:template match='Promotion'>
<xsl:value-of select='Title'/>
</xsl:template>
</xsl:stylesheet>
"
我想要这样的输出:
<h1>Arts & Entertainment</h1>
<ul>Client Five</ul>
<ul>Get your Free 2</ul>
<ul>Client 5</ul>
<ul>Get your Free 4</ul>
<ul>Client five</ul>
<ul>Get your Free 5</ul>
<h1>Community & Neighborhood</h1>
<ul>Client 1</ul>
<ul>Get your Free 1</ul>
<h1>Education</h1>
<ul>Client 3</ul>
<ul>Get Your Free 3</ul>
【问题讨论】:
-
我很确定你*不*想要像你展示的样本那样的输出。您的输出样本缺少任何类型的结构,剩下的就是节点顺序。这是对 XML 的一种非常糟糕的使用,您应该远离它。另外:您收到的错误是什么?
-
这不是我想要的,我只需要将它们分组到按类别分组的无序列表中。
-
错误:InnerException = {"'Categories' 是一个意外的标记。需要空格。第 9 行,位置 85。"}
-
这是导致错误的行:
-
@Vecdid:我只是想确保这是您收到的错误(因为我已经这么认为了)。您在 XSLT 字符串中错误地嵌套了单引号(很容易发现:只计算引发错误的行上的打开和关闭单引号)。你需要先修复它们。