Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > New xml dude needs help with xslt

Reply
Thread Tools

New xml dude needs help with xslt

 
 
Guttyguppy
Guest
Posts: n/a
 
      09-15-2005
I have

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlnssl="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.

 
Reply With Quote
 
 
 
 
nicolas
Guest
Posts: n/a
 
      09-15-2005
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" xmlnssl="http://www.w3.org/1999/XSL/Transform">
<xslutput 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 //
 
Reply With Quote
 
 
 
 
Peter Flynn
Guest
Posts: n/a
 
      09-16-2005
Guttyguppy wrote:

> I have
>
> <?xml version="1.0" encoding="ISO-8859-1"?>
> <xsl:stylesheet version="1.0"
> xmlnssl="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
 
Reply With Quote
 
Guttyguppy
Guest
Posts: n/a
 
      09-19-2005
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" xmlnssl="http://www.w3.org/1999/XSL/Transform">
> <xslutput 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 //


 
Reply With Quote
 
Joris Gillis
Guest
Posts: n/a
 
      09-19-2005
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
 
Reply With Quote
 
Guttyguppy
Guest
Posts: n/a
 
      09-20-2005
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


 
Reply With Quote
 
pppepppe@yahoo.com
Guest
Posts: n/a
 
      09-22-2005
Worked like a charm. Thanks again.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
OT:You have to check this dude out!! Grep MCSE 16 10-06-2005 05:17 PM
CDR Dude, Best way to shut up a flamer is to not respond to them, they don't talk to walls forever! Edmonton_Guy@usenet.com Computer Support 3 09-14-2005 10:56 AM
Dude, where's my mail?! Bug in renaming folders in TB 1.0.2 Z Firefox 5 06-25-2005 08:55 PM
Hey Nigel dude could it get any simpler? Richard HTML 8 02-07-2005 05:42 PM
ANN: New low-cost XML Editor, XSLT Editor, XSLT Debugger, DTD/Schema Editor Stylus Studio Java 0 08-03-2004 03:53 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57