New xml dude needs help with xslt

Discussion in 'XML' started by Guttyguppy, Sep 15, 2005.

  1. Guttyguppy

    Guttyguppy Guest

    I have

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
    <html>
    <body>
    <xsl:for-each select="category/entry">
    <xsl:value-of select="job"/><br/>
    <xsl:value-of select="date"/><br/>
    </xsl:for-each>
    </body>
    </html>
    </xsl:template>
    </xsl:stylesheet>

    and my .xml file has

    <category>Jobs</category>
    <entry>
    <job>Plumber</job>
    <date>12/5/04</date>
    </entry>
    <category>Teams</category>
    <entry>
    <job>Jets</job>
    <date>12/3/04</date>
    </entry>

    I want to have unlimited entries, but I also want to run a for-each on
    the categories. How would I structure something like that? I'm
    absolutely new to this, so please bear with me! Thanks.
    Guttyguppy, Sep 15, 2005
    #1
    1. Advertising

  2. Guttyguppy

    nicolas Guest

    Le 14 Sep 2005 17:52:42 -0700
    "Guttyguppy" <> a écrit:

    > I have
    >
    > [xsl snip]
    > and my .xml file has
    > [snip]
    >
    > I want to have unlimited entries, but I also want to run a for-each on
    > the categories. How would I structure something like that? I'm
    > absolutely new to this, so please bear with me! Thanks.


    I suggest the following data structure

    <?xml version="1.0"?>
    <categories>
    <category>
    <name>Jobs</name>
    <entries>
    <entry>
    <job>Plumber</job>
    <date>15/504</date>
    </entry>
    <entry>
    [...]
    </entry>
    </entries>
    </category>
    <category>
    <name>Teams</name>
    <entries>
    <entry>
    <name>...</name>
    <date>...</date>
    </entry>
    </entries>

    </category>
    </categories>

    with the sheet (using apply-templates instead of for-each)

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:eek:utput method="xml" omit-xml-declaration="no"/>
    <xsl:strip-space elements="*"></xsl:strip-space>
    <xsl:template match="/">
    <html>
    <body>
    <xsl:apply-templates></xsl:apply-templates>
    </body>
    </html>
    </xsl:template>
    <xsl:template match="category/name"></xsl:template>
    <xsl:template match="job|date">
    <xsl:apply-templates></xsl:apply-templates><br/>
    </xsl:template>
    </xsl:stylesheet>



    --
    nicolas //
    nicolas, Sep 15, 2005
    #2
    1. Advertising

  3. Guttyguppy

    Peter Flynn Guest

    Guttyguppy wrote:

    > I have
    >
    > <?xml version="1.0" encoding="ISO-8859-1"?>
    > <xsl:stylesheet version="1.0"
    > xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    > <xsl:template match="/">
    > <html>
    > <body>
    > <xsl:for-each select="category/entry">
    > <xsl:value-of select="job"/><br/>
    > <xsl:value-of select="date"/><br/>
    > </xsl:for-each>
    > </body>
    > </html>
    > </xsl:template>
    > </xsl:stylesheet>
    >
    > and my .xml file has
    >
    > <category>Jobs</category>
    > <entry>
    > <job>Plumber</job>
    > <date>12/5/04</date>
    > </entry>
    > <category>Teams</category>
    > <entry>
    > <job>Jets</job>
    > <date>12/3/04</date>
    > </entry>
    >
    > I want to have unlimited entries, but I also want to run a for-each on
    > the categories. How would I structure something like that? I'm
    > absolutely new to this, so please bear with me! Thanks.


    Nicolas has already suggested a different markup design, which is more
    explicit and properly nested. The trick is to enclose things in the
    elements which name what they are -- personally I prefer compactness,
    especially where the data is categorical:

    <data>
    <entry cat="Jobs" job="Plumber" date="2004-05-12"/>
    <entry cat="Teams" job="Jets" date="2004-03-12
    </data>

    I STRONGLY recommend using ISO 8601 dates in the format given: they are
    much easier to manipulate.

    Then you don't need for-each at all:

    <xsl:apply-templates select="/data/entry">
    <xsl:sort select="@cat"/>
    <xsl:sort select="@date"/>
    </xsl:apply-templates>

    ///Peter
    Peter Flynn, Sep 16, 2005
    #3
  4. Guttyguppy

    Guttyguppy Guest

    Thanks Nicolas,
    I'm a bit confused by the line,
    <xsl:template match="job|date">
    because I may have other things besides those two. How can I allow for
    an unlimited number of data-types under "entry"?
    nicolas wrote:
    > Le 14 Sep 2005 17:52:42 -0700
    > "Guttyguppy" <> a écrit:
    >
    > > I have
    > >
    > > [xsl snip]
    > > and my .xml file has
    > > [snip]
    > >
    > > I want to have unlimited entries, but I also want to run a for-each on
    > > the categories. How would I structure something like that? I'm
    > > absolutely new to this, so please bear with me! Thanks.

    >
    > I suggest the following data structure
    >
    > <?xml version="1.0"?>
    > <categories>
    > <category>
    > <name>Jobs</name>
    > <entries>
    > <entry>
    > <job>Plumber</job>
    > <date>15/504</date>
    > </entry>
    > <entry>
    > [...]
    > </entry>
    > </entries>
    > </category>
    > <category>
    > <name>Teams</name>
    > <entries>
    > <entry>
    > <name>...</name>
    > <date>...</date>
    > </entry>
    > </entries>
    >
    > </category>
    > </categories>
    >
    > with the sheet (using apply-templates instead of for-each)
    >
    > <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    > <xsl:eek:utput method="xml" omit-xml-declaration="no"/>
    > <xsl:strip-space elements="*"></xsl:strip-space>
    > <xsl:template match="/">
    > <html>
    > <body>
    > <xsl:apply-templates></xsl:apply-templates>
    > </body>
    > </html>
    > </xsl:template>
    > <xsl:template match="category/name"></xsl:template>
    > <xsl:template match="job|date">
    > <xsl:apply-templates></xsl:apply-templates><br/>
    > </xsl:template>
    > </xsl:stylesheet>
    >
    >
    >
    > --
    > nicolas //
    Guttyguppy, Sep 19, 2005
    #4
  5. Guttyguppy

    Joris Gillis Guest

    Tempore 21:50:43, die Monday 19 September 2005 AD, hinc in foro {comp.text.xml} scripsit Guttyguppy <>:

    > I'm a bit confused by the line,
    > <xsl:template match="job|date">
    > because I may have other things besides those two. How can I allow for
    > an unlimited number of data-types under "entry"?


    Use <xsl:template match="entry/*">

    If there should be a child element 'foo' of 'entry' that should not be matched, you can exclude it thus:
    <xsl:template match="entry/*[not(self::foo)]">

    regards,
    --
    Joris Gillis (http://users.telenet.be/root-jg/me.html)
    Gaudiam omnibus traderat W3C, nec vana fides
    Joris Gillis, Sep 19, 2005
    #5
  6. Guttyguppy

    Guttyguppy Guest

    Thanks everyone, I'll try it and report back!

    Joris Gillis wrote:
    > Tempore 21:50:43, die Monday 19 September 2005 AD, hinc in foro {comp.text.xml} scripsit Guttyguppy <>:
    >
    > > I'm a bit confused by the line,
    > > <xsl:template match="job|date">
    > > because I may have other things besides those two. How can I allow for
    > > an unlimited number of data-types under "entry"?

    >
    > Use <xsl:template match="entry/*">
    >
    > If there should be a child element 'foo' of 'entry' that should not be matched, you can exclude it thus:
    > <xsl:template match="entry/*[not(self::foo)]">
    >
    > regards,
    > --
    > Joris Gillis (http://users.telenet.be/root-jg/me.html)
    > Gaudiam omnibus traderat W3C, nec vana fides
    Guttyguppy, Sep 20, 2005
    #6
  7. Guttyguppy

    Guest

    Worked like a charm. Thanks again.
    , Sep 22, 2005
    #7
    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. Richard
    Replies:
    8
    Views:
    585
    Richard
    Feb 7, 2005
  2. Merrill & Michele

    Dude and Duden

    Merrill & Michele, Dec 10, 2004, in forum: C Programming
    Replies:
    6
    Views:
    383
    Merrill & Michele
    Dec 13, 2004
  3. Pistorius

    Hyperlink dude!!

    Pistorius, Dec 23, 2009, in forum: .NET
    Replies:
    1
    Views:
    326
    Pistorius
    Dec 23, 2009
  4. wong chian

    dude with web custom controls

    wong chian, Jan 22, 2004, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    115
    Victor Garcia Aprea [MVP]
    Jan 22, 2004
  5. Josema

    Required Field Validator dude

    Josema, Feb 27, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    95
    Josema
    Feb 27, 2004
Loading...

Share This Page