【问题标题】:Need help with XSLT 1.0 and grouping在 XSLT 1.0 和分组方面需要帮助
【发布时间】:2011-01-11 16:45:03
【问题描述】:

我有以下 XML 文件:

<Promotions>
  <Promotion>
    <Category>Arts &amp; Entertainment</Category>
    <Client>Client Five</Client>
    <Title>Get your Free 2</Title>
  </Promotion>
  <Promotion>
    <Category>Arts &amp; Entertainment</Category>
    <Client>Client 5</Client>
    <Title>Get your Free 4</Title>
  </Promotion>
  <Promotion>
    <Category>Arts &amp; Entertainment</Category>
    <Client>Client five</Client>
    <Title>Get your Free 5</Title>
  </Promotion>
  <Promotion>
    <Category>Community &amp; 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 &amp; 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 &amp; 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 字符串中错误地嵌套了单引号(很容易发现:只计算引发错误的行上的打开和关闭单引号)。你需要先修复它们。

标签: c# xslt grouping


【解决方案1】:

我认为错误是您的引用,但逻辑​​似乎也有缺陷。这不是一个很好的解决方案,但它应该能让你走上正轨。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" />
  <xsl:key name="categories" match="Category" use="." />
    <xsl:template match="/">
      <xsl:for-each select="/Promotions/Promotion/Category[
        generate-id(.) = generate-id(key('categories', .)[1])
      ]">
      <xsl:variable name="cname" select="." />
      <Category title="{.}">
        <xsl:for-each select="/Promotions/Promotion[Category=$cname]">
          <Title>
            <xsl:value-of select="Title" />
          </Title>
        </xsl:for-each>
      </Category>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

给你这个:

<Category title="Arts &amp; Entertainment">
  <Title>Get your Free 2</Title>
  <Title>Get your Free 4</Title>
  <Title>Get your Free 5</Title>
</Category>
<Category title="Community &amp; Neighborhood">
  <Title>Get your Free 1</Title>
</Category>
<Category title="Education">
  <Title>Get Your Free 3</Title>
</Category>

【讨论】:

  • @gum411:将代码块缩进四个空格(选择文本并按 CTRL-K,或使用“010101”编辑器按钮)。编辑您的帖子以了解我的意思。
  • +1 为答案。请注意,&lt;xsl:for-each select="/Promotions/Promotion[Category=$cname]"&gt; 等同于 &lt;xsl:for-each select="key('Category', .)"&gt;
  • @gum 如何将此 XSLT 更改为输出:Arts &娱乐 而不是
  • 从 Category 标签中移除 title="{.}" 并改用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-21
  • 2011-08-09
  • 1970-01-01
相关资源
最近更新 更多