XSLT XML-HTML Transformation using grouping

Discussion in 'XML' started by Kevin Brown, Aug 26, 2004.

  1. Kevin Brown

    Kevin Brown Guest

    Is there anyway to generate this type of resulting HTML table from
    this XML using XSLT? Basically I need to be able to consult 2 trees of
    data to generate the HTML, but I have not been able to figure out how
    to do so. There is supposed to be a way of using templates and the key
    function to do grouping, but can it be done with 2 different trees? If
    so, are there any examples of such things

    Thanks for any help,

    Kevin

    ***

    HTML Table

    chef 1 chef 2 chef 3
    must: make.pizza
    pour.milk
    maybe: make.orange.juice make.ice.cream make.martini
    never: make.apple.pie


    ***

    XML File
    <?xml version="1.0" encoding="LATIN1" standalone="yes" ?>
    <!--
    <!DOCTYPE housework.definition PUBLIC "house work xml"
    "housework.dtd">

    -->
    <housework.definition housework.name="kitchen work"
    docclass.name="house work">
    <element.for.housework.def.list>
    <element.for.housework.def element.name="make.pizza" presence="must"
    />
    <element.for.housework.def element.name="pour.milk" presence="must"
    />
    <element.for.housework.def element.name="make.orange.juice"
    presence="maybe" />
    <element.for.housework.def element.name="make.apple.pie"
    presence="never" />
    <element.for.housework.def element.name="make.ice.cream"
    presence="maybe" />
    <element.for.housework.def element.name="make.martini"
    presence="maybe" />
    </element.for.housework.def.list>
    <sequence.def.list>
    <sequence.def sequence="stage 1">
    <sequence.def.role role="chef 1">
    <element.for.sequence.def.role element="make.pizza" />
    <element.for.sequence.def.role element="pour.milk" />
    <element.for.sequence.def.role element="make.orange.juice" />
    </sequence.def.role>
    <sequence.def.role role="chef 2">
    <element.for.sequence.def.role element="make.apple.pie" />
    <element.for.sequence.def.role element="make.ice.cream" />
    </sequence.def.role>
    <sequence.def.role role="chef 3">
    <element.for.sequence.def.role element="make.martini" />
    </sequence.def.role>
    </sequence.def>
    </sequence.def.list>
    </housework.definition>
    Kevin Brown, Aug 26, 2004
    #1
    1. Advertising

  2. Kevin Brown

    Marrow Guest

    Hi Kevin,

    The only distinct/grouping I can see if the values of @presence?

    Try something like...

    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:eek:utput method="html" indent="yes"/>
    <xsl:key name="kDistinctPresence" match="element.for.housework.def"
    use="@presence"/>

    <xsl:template match="/">
    <html>
    <body>
    <xsl:variable name="chefs"
    select="/housework.definition/sequence.def.list/sequence.def/sequence.def.ro
    le"/>
    <table border="1">
    <!-- do the chef header row -->
    <tr>
    <th/>
    <xsl:for-each select="$chefs">
    <th>
    <xsl:value-of select="@role"/>
    </th>
    </xsl:for-each>
    </tr>
    <!-- now do the rows for each distinct @presence -->
    <xsl:apply-templates
    select="/housework.definition/element.for.housework.def.list/element.for.hou
    sework.def[generate-id() =
    generate-id(key('kDistinctPresence',@presence))]">
    <xsl:with-param name="chefs" select="$chefs"/>
    </xsl:apply-templates>
    </table>
    </body>
    </html>
    </xsl:template>

    <xsl:template match="element.for.housework.def">
    <xsl:param name="chefs"/>
    <xsl:variable name="this-presence"
    select="key('kDistinctPresence',@presence)/@element.name"/>
    <tr>
    <th align="right">
    <xsl:value-of select="@presence"/>
    <xsl:text>:</xsl:text>
    </th>
    <!-- now do columns for each chef with this @presence value -->
    <xsl:for-each select="$chefs">
    <td>
    <!-- look for matches withing this chef of the current @presence -->
    <xsl:for-each select="element.for.sequence.def.role/@element[. =
    $this-presence]">
    <xsl:value-of select="."/>
    <xsl:if test="position() != last()">
    <br/>
    </xsl:if>
    </xsl:for-each>
    </td>
    </xsl:for-each>
    </tr>
    </xsl:template>
    </xsl:stylesheet>


    HTH
    Marrow
    http://www.marrowsoft.com - home of Xselerator (XSLT IDE and debugger)
    http://www.topxml.com/Xselerator



    "Kevin Brown" <> wrote in message
    news:...
    > Is there anyway to generate this type of resulting HTML table from
    > this XML using XSLT? Basically I need to be able to consult 2 trees of
    > data to generate the HTML, but I have not been able to figure out how
    > to do so. There is supposed to be a way of using templates and the key
    > function to do grouping, but can it be done with 2 different trees? If
    > so, are there any examples of such things
    >
    > Thanks for any help,
    >
    > Kevin
    >
    > ***
    >
    > HTML Table
    >
    > chef 1 chef 2 chef 3
    > must: make.pizza
    > pour.milk
    > maybe: make.orange.juice make.ice.cream make.martini
    > never: make.apple.pie
    >
    >
    > ***
    >
    > XML File
    > <?xml version="1.0" encoding="LATIN1" standalone="yes" ?>
    > <!--
    > <!DOCTYPE housework.definition PUBLIC "house work xml"
    > "housework.dtd">
    >
    > -->
    > <housework.definition housework.name="kitchen work"
    > docclass.name="house work">
    > <element.for.housework.def.list>
    > <element.for.housework.def element.name="make.pizza" presence="must"
    > />
    > <element.for.housework.def element.name="pour.milk" presence="must"
    > />
    > <element.for.housework.def element.name="make.orange.juice"
    > presence="maybe" />
    > <element.for.housework.def element.name="make.apple.pie"
    > presence="never" />
    > <element.for.housework.def element.name="make.ice.cream"
    > presence="maybe" />
    > <element.for.housework.def element.name="make.martini"
    > presence="maybe" />
    > </element.for.housework.def.list>
    > <sequence.def.list>
    > <sequence.def sequence="stage 1">
    > <sequence.def.role role="chef 1">
    > <element.for.sequence.def.role element="make.pizza" />
    > <element.for.sequence.def.role element="pour.milk" />
    > <element.for.sequence.def.role element="make.orange.juice" />
    > </sequence.def.role>
    > <sequence.def.role role="chef 2">
    > <element.for.sequence.def.role element="make.apple.pie" />
    > <element.for.sequence.def.role element="make.ice.cream" />
    > </sequence.def.role>
    > <sequence.def.role role="chef 3">
    > <element.for.sequence.def.role element="make.martini" />
    > </sequence.def.role>
    > </sequence.def>
    > </sequence.def.list>
    > </housework.definition>
    Marrow, Aug 26, 2004
    #2
    1. Advertising

  3. Kevin Brown

    William Park Guest

    Kevin Brown <> wrote:
    > Is there anyway to generate this type of resulting HTML table from
    > this XML using XSLT? Basically I need to be able to consult 2 trees of
    > data to generate the HTML, but I have not been able to figure out how
    > to do so. There is supposed to be a way of using templates and the key
    > function to do grouping, but can it be done with 2 different trees? If
    > so, are there any examples of such things
    >
    > Thanks for any help,
    >
    > Kevin
    >
    > ***
    >
    > HTML Table
    >
    > chef 1 chef 2 chef 3
    > must: make.pizza
    > pour.milk
    > maybe: make.orange.juice make.ice.cream make.martini
    > never: make.apple.pie


    Why don't you convert your XML into arrays, ie.
    must=( make.pizza pour.milk )
    maybe=( make.orange.juice make.ice.cream make.martini )
    never=( make.apple.pie )
    and
    chef1=( make.pizza pour.milk make.orange.juice )
    chef2=( make.ice.cream make.apple.pie )
    chef3=( make.martini )

    Then, you can cross-check as you print them out.

    --
    William Park <>
    Open Geometry Consulting, Toronto, Canada
    William Park, Aug 26, 2004
    #3
  4. Kevin Brown

    Kevin Brown Guest

    Thanks guys, you've been a big help!

    William Park <> wrote in message news:<>...
    > Kevin Brown <> wrote:
    > > Is there anyway to generate this type of resulting HTML table from
    > > this XML using XSLT? Basically I need to be able to consult 2 trees of
    > > data to generate the HTML, but I have not been able to figure out how
    > > to do so. There is supposed to be a way of using templates and the key
    > > function to do grouping, but can it be done with 2 different trees? If
    > > so, are there any examples of such things
    > >
    > > Thanks for any help,
    > >
    > > Kevin
    > >
    > > ***
    > >
    > > HTML Table
    > >
    > > chef 1 chef 2 chef 3
    > > must: make.pizza
    > > pour.milk
    > > maybe: make.orange.juice make.ice.cream make.martini
    > > never: make.apple.pie

    >
    > Why don't you convert your XML into arrays, ie.
    > must=( make.pizza pour.milk )
    > maybe=( make.orange.juice make.ice.cream make.martini )
    > never=( make.apple.pie )
    > and
    > chef1=( make.pizza pour.milk make.orange.juice )
    > chef2=( make.ice.cream make.apple.pie )
    > chef3=( make.martini )
    >
    > Then, you can cross-check as you print them out.
    Kevin Brown, Aug 28, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. clogwog
    Replies:
    0
    Views:
    415
    clogwog
    Oct 20, 2004
  2. Jon Martin Solaas
    Replies:
    2
    Views:
    448
    Jon Martin Solaas
    Jun 15, 2004
  3. SG
    Replies:
    2
    Views:
    834
    Mike Schilling
    Aug 15, 2007
  4. bosesuparna
    Replies:
    0
    Views:
    946
    bosesuparna
    Nov 25, 2011
  5. Binaryx
    Replies:
    3
    Views:
    5,056
    Peter Flynn
    Apr 14, 2012
Loading...

Share This Page