Forum Moderators: open

Message Too Old, No Replies

XSL transformation for newspage

how to walk through every node?

         

Darkelve

12:33 pm on Dec 6, 2004 (gmt 0)

10+ Year Member



I've got an XML question. Or rather, an XSL(T) question.

This is the XML:


<nieuwslijn>

<artikel id="1">
<titel>de nieuwslijn</titel>
<datum>25 november 2004</datum>
<inhoud>Dienstregelingen en wedstrijd oudejaarsnacht beschikbaar vanaf 6 december</inhoud>
<internetadres>http://www.example.com/nieuws/.#oudejaarsnacht</internetadres>

<omschrijving>
<titel>Dienstregelingen en wedstrijd oudejaarsnacht beschikbaar vanaf 6 december</titel>
<datum>25 november 2004</datum>
<subtitel></subtitel>
<paragraaf id="1">Vanaf 6 december 2004 kunt u op deze website terecht voor de dienstregelingen en de wedstrijd voor oudejaarsnacht.</paragraaf>
</omschrijving>

</artikel>

<artikel id="2">
<titel>de nieuwslijn</titel>
<datum>26 november 2004</datum>
<inhoud>Tentoonstelling '100 jaar elektrische tram' nog tot 9 januari</inhoud>
<internetadres>http://www.example.com/oostvlaanderen/evenementen_elektrischetram.html</internetadres>
</artikel>

<artikel id="3">
<titel>de nieuwslijn</titel>
<datum>3 december 2004</datum>
<inhoud>4 december: betaalstaking in Oost-Vlaanderen</inhoud>
<internetadres>http://www.example.com/nieuws/#staking_ovl</internetadres>
</artikel>

</nieuwslijn>

This is the XSL(T) stylesheet:


<?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="/">
<div id="newscontainer">
<h1><xsl:value-of select="nieuwslijn/artikel[@id='1']/titel"/></h1>
<div id="news">
<p id="newsitem">
<a href="{nieuwslijn/artikel[@id='1']/internetadres}"><xsl:value-of select="nieuwslijn/artikel[@id='1']/inhoud"/></a>
</p>
<p id="endnewsitem">
<a href="{nieuwslijn/artikel[@id='2']/internetadres}"><xsl:value-of select="nieuwslijn/artikel[@id='2']/inhoud"/></a></p>
</div>
<div id="btimage"> </div>
</div>

</xsl:template>
</xsl:stylesheet>

You can see that in the XSLT stylesheet, I have to specify each article number (artikel[id='x']) seperately. But I would like to write the code so that the page will 'walk through' these values.

These are the steps I want to add:

1. Walk through array "artikel[id='x']" until end of document is reached.
2. Last item:
a) Code of the last item has to differ slightly: <p id="endnewsitem"> instead of just <p>
cool.gif + contain the extra code " </div> <div id="btimage"> </div> "

This is necessary to signal the end of the items in the list and draw the blue line underneath the last item.

3. Stop processing if end of document has been reached

A block of ASP code makes use of this XML and XSLT files to generate the corresponding block of code for the yellow news container at the top right of every page.


<%'Load XML
set xml = Server.CreateObject("Microsoft.XMLDOM")
xml.async = false
xml.load(Server.MapPath("/pathto/nieuwslijn.xml"))

'Load XSL
set xsl = Server.CreateObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load(Server.MapPath("/pathto/nieuwsoverzicht.xsl"))

'Transform file
Response.Write(xml.transformNode(xsl))
%>

Darkelve

<Sorry, no personal URLs. See TOS [webmasterworld.com]>

[edited by: tedster at 6:58 pm (utc) on Jan. 2, 2005]

Alternative Future

12:44 pm on Dec 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello

Have you looked into using xsl:for-each

<xsl:for-each select="artikel">
//repeat all required values in here
</xsl:for-each>
//Finish with your
<div id="btimage"> </div>

Or am i off in the wrong track?

-George

Darkelve

1:15 pm on Dec 6, 2004 (gmt 0)

10+ Year Member



Well, I'm working on it and succeeded with something similar (I stripped the id='x' from the XML files, although I'm not sure now if that was necessary):


<div id="newscontainer">
<h1>de nieuwslijn</h1>
<div id="news">
<xsl:for-each select="nieuwslijn/artikel">
<p id="newsitem">
<a href="{internetadres}">
<xsl:value-of select="inhoud"/></a>
</p>
</xsl:for-each>
[b]
<p id="endnewsitem">
&#160;
</p>
[/b]
</div>
<div id="btimage">&#160;</div>
</div>

But the very LAST element of the node is different from the rest.

The paragraph (<p>) of the last element has to get the a html id of id='endnewsitem', because this is what 'closes' the box in which the news appears.

So your/my code applies, except that for the very LAST element of the node 'nieuwlijn/artikel' a different piece of code has to be created. ( <p id="endnewsitem" instead of <p="newsitem"> )

So this should be what the last paragraph looks like in the html code (with &#160; being the value of the last node in 'nieuwslijn/artikel':


<p id="endnewsitem">
&#160;
</p>

Darkelve

Alternative Future

1:23 pm on Dec 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is it not possible to add this to your xml object that is passed to your xslt? i.e.

<artikel id="2">
<titel>de nieuwslijn</titel>
<datum>26 november 2004</datum>
<inhoud>Tentoonstelling '100 jaar elektrische tram' nog tot 9 januari</inhoud>
<internetadres>http://www.delijn.be/oostvlaanderen/evenementen_elektrischetram.html</internetadres>
<style-id>newsitem</style-id>
</artikel>

<artikel id="3">
<titel>de nieuwslijn</titel>
<datum>3 december 2004</datum>
<inhoud>4 december: betaalstaking in Oost-Vlaanderen</inhoud>
<internetadres>http://www.delijn.be/nieuws/#staking_ovl</internetadres>
<style-id>endnewsitem</style-id>
</artikel>

Obviously to get this working you would have to have total control over the building up of the data.

-George

Darkelve

1:26 pm on Dec 6, 2004 (gmt 0)

10+ Year Member



Obviously to get this working you would have to have total control over the building up of the data.

I have... will try this.

Darkelve

1:33 pm on Dec 6, 2004 (gmt 0)

10+ Year Member



Haha! It worked, thanks! :)
I like this solution

This forum is the best...

This post can be marked as solved (at least if this is how it is done here).