xsl-list
[Top] [All Lists]

RE: [xsl] Assistance with for-each

2007-07-04 14:00:16
The poor design of the input is the first thing that jumps out at me.

If <perscat> and <trade> are truly properties of <person>, then this structure 
is much easier to understand and work with:

<person man="1">
  <perscat category="Cook"/>
  <trade>McDonalds Chef</trade>
</person>

But I'll assume that you have no control over the input and work from there.

I'll start by saying that you don't need (an ought not use) for-each here. You 
need only create a template for <person> and apply-templates:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
  <xsl:output method="html" indent="yes" />
  <xsl:strip-space elements="*" />

  <xsl:template match="/">
    <xsl:apply-templates />
  </xsl:template>
  
  <xsl:template match="reqpers">
    <table>
      <xsl:apply-templates select="person" />
    </table>
  </xsl:template>
    
  <xsl:template match="person">
    <tr>
      <td><xsl:value-of select="@man"/></td>
      <td><xsl:value-of select="following-sibling::perscat[1]/@category"/></td>
      <td><xsl:value-of select="following-sibling::trade[1]"/></td>
    </tr>
  </xsl:template>

</xsl:stylesheet>
-- 
Charles Knell
cknell(_at_)onebox(_dot_)com - email



-----Original Message-----
From:     Maxine Pensyl-Johnson <Maxine(_dot_)Pensyl-Johnson(_at_)uav(_dot_)com>
Sent:     Wed, 4 Jul 2007 12:52:29 -0700
To:       <xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com>
Subject:  [xsl] Assistance with for-each

I'm trying to get my sample data to all appear on one table row, but I'm
unsuccessful. Any help you can provide is appreciated.


XSLT 1.0   SAXON 6.5.5

Sample Data:
                <reqpers>

                    <person man="1"/>

                    <perscat category="Cook"/>

                    <trade>McDonalds Chef</trade>

                    <person man="1"/>

                    <perscat category="Staff"/>

                    <trade>Front Counter</trade>

                    <person man="4"/>

                    <perscat category="Janitor"/>

                    <trade>Clean up the mess left by others</trade>

                </reqpers>

 

Desired Results

1        Cook    McDonalds Chef
1        Staff   Front Counter
4        Janitor Clean up the mess left by others
  

Code:

        <xsl:for-each
select="reqpers/person|reqpers/perscat|reqpers/trade">

            <tr>

                <td valign="top"><xsl:value-of
select="person/@man"/></td>

                <td valign="top"><xsl:value-of
select="perscat/@category"/></td>

               <td valign="top"><xsl:value-of
select="trade/text()"/></td>

            </tr>

        </xsl:for-each>

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--




--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>