3

I am trying to get multiple html documents for every User in an XML document using XSLT based on the answer in: how to apply group by on xslt elements however without succes.

XML:

<root>
  <ArrayOfResult>
    <Result>
      <Men>
        <BowlerResult>
          <Person>
            <Name>name 1</Name>
          </Person>
          <Data>
            <Score1>1</Score1>
            <Score2>1</Score2>
            <Score3>1</Score3>
          </Data>
        </BowlerResult>
        <BowlerResult>
          <Person>
            <Name>name 2</Name>
          </Person>
          <Data>
            <Score1>2</Score1>
            <Score2>2</Score2>
            <Score3>2</Score3>
          </Data>
        </BowlerResult>
      </Men>
      <Women>
        <BowlerResult>
          <Person>
            <Name>name 3</Name>
          </Person>
          <Data>
            <Score1>3</Score1>
            <Score2>3</Score2>
            <Score3>3</Score3>
          </Data>
        </BowlerResult>
      </Women>
    </Result>
    <Result>...</Result>
  </ArrayOfResult>
  <ArrayOfResult>...</ArrayOfResult>

Person can be in more Leagues

What I want to achieve: for every Person (distinct on Person/Name) i want to accumulate data from all 'BowlerResult' elements

The XSLT I have right now:

<xsl:key name="keyPerson" match="BowlerResult" use="Person/Name" />

<xsl:template match="text()" />

<xsl:template match="/root">
  <root>
    <xsl:apply-templates />
  </root>
</xsl:template>

<xsl:template
 match="BowlerResult[generate-id(.)=generate-id(key('keyPerson',Person/Name)[1])]">
  <Person value="{Person/Name}">
    <xsl:for-each select="key('keyPerson', Person/Name)">
      <Result>
        <id>
          <xsl:value-of select="Person/Name" />
        </id>
      </Result>
    </xsl:for-each>
  </Person>
</xsl:template>

But this only gives me the an iteration of the first Person.

Community
  • 1
  • 1
amaters
  • 2,266
  • 2
  • 24
  • 44

1 Answers1

4

Your XSLT has a bunch of issues but the main problem here was that the first template has a path that doesn't match anything (TeamResult is not directly under root and there is no Result element. Also, the key is named "keyPerson", not "keyBowler".

This XSLT successfully groups the players once per player. Could you give it a try? (n.b. the actual input XML uses element names BowlerResult and Bowler)

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:key name="keyPerson" match="BowlerResult" use="Bowler/Name" />

  <xsl:template match="text()" />

  <xsl:template match="/root">
    <root>
      <xsl:apply-templates />
    </root>
  </xsl:template>

  <xsl:template
   match="BowlerResult[generate-id(.)=generate-id(key('keyPerson',Bowler/Name)[1])]">
    <xsl:variable name="name" select="Bowler/Name" />
    <Person value="{$name}">
      <xsl:for-each select="key('keyPerson', $name)">
        <Result>
          <id>
            <xsl:value-of select="$name" />
          </id>
        </Result>
      </xsl:for-each>
    </Person>
  </xsl:template>


</xsl:stylesheet>
JLRishe
  • 99,490
  • 19
  • 131
  • 169
  • thanks for pointing out the typos. I slightly altered the XML for clarity and forgot some. I will take a look at your xslt and get back on it asap. – amaters Jan 18 '13 at 11:50
  • I tried to addapt it to what you gave me. Now the result is empty. I posted the files online if you want to check them: http://www.hoedinie.nl/stack/Xml/Complete.xml http://www.hoedinie.nl/stack/Xslt/Players.xslt – amaters Jan 18 '13 at 12:06
  • Does it output data before you modify it? If so, can you show us your modified version? – JLRishe Jan 18 '13 at 12:08
  • yes, i just edited my post when you were answering. the files are online now – amaters Jan 18 '13 at 12:17
  • I did just that. Twice to be exact. Can't find anything thats wrong. sorry. – amaters Jan 18 '13 at 12:31
  • Sorry, I misunderstood what you said. I thought you edited your answer and didn't realize the modified XSLT was somewhere else. Could you provide the actual input XML somewhere? – JLRishe Jan 18 '13 at 12:33
  • the input is in the same comment (2 links after each other): http://www.hoedinie.nl/stack/Xml/Complete.xml the XSL: http://www.hoedinie.nl/stack/Xslt/Players.xslt – amaters Jan 18 '13 at 12:44
  • The input XML looked empty when I accessed it via IE, but it seems ok in FireFox. Trying it now... – JLRishe Jan 18 '13 at 12:47
  • 1
    Ok, the issue was that the elements in the actual XML are called "Bowler", not "Person". There were five instances of "Person" that needed to be changed to "Bowler". Updated above. – JLRishe Jan 18 '13 at 12:57
  • Thanks for your patience! It works! One step further in the complete XSLT puzzle. :) – amaters Jan 18 '13 at 14:31