Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with XSLT

Status
Not open for further replies.

sonya9879

Programmer
Jun 18, 2004
147
CA
hi,

I am trying to render this xml so it looks like the way I want it but I can't get this grouping to work, I am not very familiar with XSLT so I was wondering if someone knows how to do this? thanks

XML:

<records>
<customer>
<id>1</id>
<name>tom</name>
<city>broon</city>
<amnt>10</amnt>
<state>sc</state>
<section>cc</section>
<recipt>ok</recipt>
</customer>

<customer>
<id>2</id>
<name>john</name>
<city>philly</city>
<amnt>50</amnt>
<state>pa</state>
<section>dd</section>
<recipt>ok</recipt>
</customer>

<customer>
<id>3</id>
<name>phil</name>
<city>dallas</city>
<amnt>100</amnt>
<state>tx</state>
<section>cc</section>
<recipt>ok</recipt>
</customer>

<customer>
<id>4</id>
<name>julian</name>
<city>menphis</city>
<amnt>133</amnt>
<state>sc</state>
<section>cc</section>
<recipt>ok</recipt>
</customer>

<customer>
<id>5</id>
<name>gloria</name>
<city>menphis</city>
<amnt>33</amnt>
<state>sc</state>
<section>dd</section>
<recipt>ok</recipt>
</customer>
</records>

and this is what I want the result to look like:

STATE: SC

SECTION: CC
ID Name Amount City Rec.PT.
1 tom 10 broon ok
4 julian 133 menphis ok

SECTION: DD
ID Name Amount City Rec.PT.
5 gloria 33 menphis ok

STATE: PA

SECTION: DD
ID Name Amount City Rec.PT.
2 John 50 philly ok

STATE: TX

SECTION: DD
ID Name Amount City Rec.PT.
3 phil 100 dallas ok



 
You want this as HTML tables?

Jon

"I don't regret this, but I both rue and lament it.
 
thanks JontyMC for your reply, yes i would prefer this in html tables. do you know how to do this with xsl? I am driving nuts with this since a couple of days ago :(
 
Here you go:
Code:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
  <xsl:output method="xml" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" doctype-system="[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"/>[/URL]
  <xsl:template match="/">
    <html>
      <head>
        <title>Test</title>
        <style type="text/css">
dt
{
  margin-top: 1em;
  font-weight: bold;
  font-size: x-large;
  text-transform: uppercase;
}
dd
{
  margin-left: 1em;
}
caption
{
  text-align: left;
  font-weight: bold;
  font-size: large;
  text-transform: uppercase;
}
        </style>
      </head>
      <body>
        <dl>
          <xsl:apply-templates select="records/customer[not(state = preceding-sibling::customer/state)]" mode="state"/>
        </dl>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="customer" mode="state">
    <dt>
      <xsl:value-of select="concat('STATE: ', state)"/>
    </dt>
    <xsl:apply-templates select="../customer[state = current()/state and not(section = preceding-sibling::customer[state = current()/state]/section)]" mode="section"/>
  </xsl:template>
  <xsl:template match="customer" mode="section">
    <dd>
      <table border="1">
        <caption>
          <xsl:value-of select="concat('SECTION: ', section)"/>
        </caption>
        <thead>
          <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Amount</th>
            <th>City</th>
            <th>Rec.PT.</th>
          </tr>
        </thead>
        <tbody>
          <xsl:for-each select="../customer[state = current()/state and section = current()/section]">
            <tr>
              <td>
                <xsl:value-of select="id"/>
              </td>
              <td>
                <xsl:value-of select="name"/>
              </td>
              <td>
                <xsl:value-of select="amnt"/>
              </td>
              <td>
                <xsl:value-of select="city"/>
              </td>
              <td>
                <xsl:value-of select="recipt"/>
              </td>
            </tr>
          </xsl:for-each>
        </tbody>
      </table>
    </dd>
  </xsl:template>
</xsl:stylesheet>
If you're XML is v large, you may want to use meunchian grouping (more efficient).

Jon

"I don't regret this, but I both rue and lament it.
 
First I need to say: GOD BLESS YOU! thanks so much for helping me with this, I am still in the early parts of XSLT and learning by the minute. one question i have about this, in the sections, I get the whole section duplicated the same amount of entries I have when i have several entries under a section. is like is doing the loop again and again for the amount of times records are in the section. do u know what i mean?

for example

SECTION: CC
ID Name Amount City Rec.PT.
1 tom 10 broon ok
4 julian 133 menphis ok

SECTION: CC
ID Name Amount City Rec.PT.
1 tom 10 broon ok
4 julian 133 menphis ok

SECTION: CC
ID Name Amount City Rec.PT.
1 tom 10 broon ok
4 julian 133 menphis ok
 
Hmmm. You copied the exact code? I don't get that problem.

Jon

"I don't regret this, but I both rue and lament it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top