views:

210

answers:

3

I have the following code:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  <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 &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>

It outputs the following:

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

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

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

+3  A: 

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>
Martin Honnen
Accept answer as this is exactly what i was looking for. Will post code and xml shortly.
James Campbell
I have edited my question, although you pointed me in the right direction, I cannot get it to work right, please advise
James Campbell
A: 

Thank you SO much! This was EXACTLY what I needed!

Angie
A: 

My XML like this.... Air Cargo Freight International LLC parco 11745 04 3430060 3430075 cargo.jpeg Air Cargo Freight International LLC tazese 11745 09 3431234 3434321 cargo.jpeg Air Cargo Freight International LLC raseus 56899 08 347890 345678 cargo.jpeg

now my output like this.... Air Cargo cargo.jpeg Customer>Freight International

box 11745, parco, 04 3430060

fax 04 3430075 cargo.jpeg Customer>Freight International

box 11745, tazese, 04 3431234

fax 04 3434321 cargo.jpeg Customer>Freight International

box 56899, raseus, 04 347890

fax 04 345678

i would like result like this... Air Cargo cargo.jpeg Customer>Freight International

box 11745, parco, 04 3430060

fax 04 3430075

box 11745, tazese, 04 3431234

fax 04 3434321

box 56899, raseus, 04 347890

fax 04 345678 one customer having more branches so all branch list should come under one customer name for this case how to create xslt please give some idea.

advance thanks

ahmed