473,387 Members | 3,750 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

New xml dude needs help with xslt

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.

Sep 15 '05 #1
6 1338
Le 14 Sep 2005 17:52:42 -0700
"Guttyguppy" <gu********@gmail.com> 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:output 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 //
Sep 15 '05 #2
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
Sep 16 '05 #3
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" <gu********@gmail.com> 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:output 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 //


Sep 19 '05 #4
Tempore 21:50:43, die Monday 19 September 2005 AD, hinc in foro {comp.text.xml} scripsit Guttyguppy <gu********@gmail.com>:
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
Sep 19 '05 #5
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 <gu********@gmail.com>:
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


Sep 20 '05 #6
Worked like a charm. Thanks again.

Sep 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: K. N. | last post by:
Is there any good and fast Python module for XSLT processing ? I'm going to use XML and XSLT to generate web pages, so I need XSLT processor that will be able to transform for example a DOM object...
2
by: Tom Corcoran | last post by:
I am working to ease updating of a html page by transforming 2 xml files. I was going to use xslt for this and had bought 2 unopened books, wrox xslt and o'reilly's xslt cookbook. But am now...
1
by: Mohit | last post by:
Hi Friends I have to call 1 of the 2 child XSLT files from the Main XSLT file based on some criteria. I want one child XSLT file will be executed by version 1 of XSLT processor and the other by...
4
by: Cathie | last post by:
Hi All, I am trying to get my style sheet to work. It works fine in IE but I can't get it to work in .net. Below is the function I use for transforming, where advancedOptionsFile is the path...
4
by: schneider | last post by:
Anyone know if there is a way to dynamicly create a Xslt template/s and use them as an xml transform with-out use files for the Xslt? All the methods I see use files. I want to create a Xslt...
27
by: Chris, Master of All Things Insignificant | last post by:
I have come to greatly respect both Herfried & Cor's reponses and since the two conflicted, I wanted to get some clarification. My orginal post: Herfried, maybe your example here can get you to...
7
by: GS | last post by:
Hi! I am getting an "Invalid Xml" exception when I try to load an xsl file using XslTransform.Load(string url). The Xsl file is valid and works fine if I just rename the file. There are some...
3
by: thomas.porschberg | last post by:
Hi, I want to read records from a database and export it in an arbitrary format. My idea was to feed a class with a String array fetched from the database and let this class fire SAX events as...
2
jkmyoung
by: jkmyoung | last post by:
Here's a short list of useful xslt general tricks that aren't taught at w3schools. Attribute Value Template Official W3C explanation and example This is when you want to put dynamic values...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.