XSLT 分组和子分组

发布于 2024-08-20 15:51:18 字数 4895 浏览 3 评论 0原文

我有以下代码:

    <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 &amp; Entertainment</Category>
    <Client>Client 1</Client>
    <Title>Get your Free 2</Title>
  </Promotion>
  <Promotion>
    <Category>Arts &amp; Entertainment</Category>
    <Client>Client 1</Client>
    <Title>Get your Free 4</Title>
  </Promotion>
  <Promotion>
    <Category>Arts &amp; Entertainment</Category>
    <Client>Client 1</Client>
    <Title>Get your Free 5</Title>
  </Promotion>
  <Promotion>
    <Category>Community &amp; 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 &amp; 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 &amp; 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 &amp; 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 &amp; 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>

基本上在按类别分组之后,我只想查看该类别下的一位客户以及该类别中该客户的每次促销活动。

I have the following code:

    <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>

My 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>

It outputs the following:

<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>

I would like the output to be grouped by category first then by client for each category, any insight to this would be great:

<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>

Basically after grouped by category, I only want to see one client under the category with each promotion for that client in that category.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拔了角的鹿 2024-08-27 15:51:18

如果没有看到 XML 输入,就很难建议对样式表进行更改,因此目前我所能做的就是查看 http://www.biglist.com/lists/xsl-list/archives/200101/msg00070.html 作为使用 XSLT 1.0 进行多级分组的示例。

[编辑]:以下是应用两级慕尼黑分组的方法:

<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>

Without seeing the XML input it is hard to suggest changes to your stylesheet so currently all I can do is to look at http://www.biglist.com/lists/xsl-list/archives/200101/msg00070.html as an example of multi level grouping with XSLT 1.0.

[edit]: Here is how you could apply two level Muenchian grouping:

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文