New xml dude needs help with xslt

G

Guttyguppy

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.
 
N

nicolas

Le 14 Sep 2005 17:52:42 -0700
Guttyguppy said:
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>
 
P

Peter Flynn

Guttyguppy said:
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
 
G

Guttyguppy

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 said:
Le 14 Sep 2005 17:52:42 -0700
Guttyguppy said:
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>
 
J

Joris Gillis

Tempore 21:50:43 said:
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,
 
G

Guttyguppy

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

Joris said:
Tempore 21:50:43 said:
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,
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top