XSL formatting

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

XSL formatting

Post by raghavan20 »

let us say i have a following xml block

Code: Select all

<channel>
    <title>Feeds from BBC</title>
    <link>http://www.bbc.co.uk</link>
    <description>Business feeds from bbc.co.uk</description>
    <language>English</language>
    <publishedDate>22-02-2007 03:34:02</publishedDate>
  </channel>

i want to apply xsl to this. i want to put the title in hyperlink that goes to http://www.bbc.co.uk

Code: Select all

<table width = '600'>
<tr>
	<td>
		<a href = "<xsl:value-of select='channel/link' />"> <------------------------------THIS IS WRONG; HOW DO I DO THIS ????
			<xsl:value-of select='channel/title' />
		</a>
	</td>
	<td>
		Date: <xsl:value-of select='channel/publishedDate' />
	</td>
</tr>
<tr>
	<td colspan = '2'><xsl:value-of select='channel/description' /></td>
</tr>
</table>
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

just a quick doubt. is it possible to use something like css classes and ids.
i am not really interested in writing like this

Code: Select all

<xsl:for-each select="itemList/item">
      <tr>
        <th style = 'background-color:gray'><xsl:value-of select="title"/></th> 
      </tr>
      <tr>
        <td style = 'background-color:lightblue;'><xsl:value-of select="description"/></td>
      </tr>
            
      </xsl:for-each>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

class="xyz" is a valid xml attribute. And that's all the processor cares about.
<a href = "<xsl:value-of select='channel/link' />">
You're looking for xsl:attribute

Code: Select all

<?php
$rss = '<rss>
 <channel>
  <title>Feeds from BBC</title>
  <link>http://www.bbc.co.uk</link>
  <description>Business feeds from bbc.co.uk</description>
  <language>English</language>
  <publishedDate>22-02-2007 03:34:02</publishedDate>
 </channel> 
</rss>';

$xsl = '<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="html" encoding="iso-8859-1" indent="no"/>
 <xsl:template match="channel">
  <a>
   <xsl:attribute name="href"><xsl:value-of select="link"/></xsl:attribute>
   <xsl:value-of select="title"/>
  </a>
 </xsl:template>
</xsl:stylesheet>';

$dom = DOMDocument::loadxml($xsl);
$proc = new XSLTProcessor;
$proc->importStyleSheet($dom);

$dom = DOMDocument::loadxml($rss);
echo $proc->transformToXML($dom);
?>
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

volka wrote:class="xyz" is a valid xml attribute. And that's all the processor cares about.
<a href = "<xsl:value-of select='channel/link' />">
thanks volka, i understand how to use hyperlink. but can you show me some example of how to CSS classes and ids in CSS document?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

raghavan20 wrote:<a href = "<xsl:value-of select='channel/link' />">
That doesn't work.
raghavan20 wrote:but can you show me some example of how to CSS classes and ids in CSS document?
I don't understand the question. Please explain what you're trying to achieve.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

If i understand well, it's the same problem as described in: http://www.timvw.be/value-of-in-an-attribute/.

For you the answer would be:

<a href ="{channel/link}">
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

or the mentioned
<xsl:attribute name="href"><xsl:value-of select="link"/></xsl:attribute>
;)
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

timvw wrote:If i understand well, it's the same problem as described in: http://www.timvw.be/value-of-in-an-attribute/.

For you the answer would be:

<a href ="{channel/link}">
that was kewl timvw
volka wrote: Quote:
<xsl:attribute name="href"><xsl:value-of select="link"/></xsl:attribute>
where do you get this list form such as href, etc ?
raghavan20 wrote:
but can you show me some example of how to CSS classes and ids in CSS document?
I don't understand the question. Please explain what you're trying to achieve.
I was just asking if I were to use CSS for XML styling, how would i use classes and ids that are supported by CSS ?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

I don't think you can use classes with xml - and I think you can only use "id"s if you define an attribute as an id in your DTD.

I know one way for you to find out! (and post what you discover here, would you ;-) )
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

raghavan20 wrote:
volka wrote: Quote:
<xsl:attribute name="href"><xsl:value-of select="link"/></xsl:attribute>
where do you get this list form such as href, etc ?
I do not understand the question.

raghavan20 wrote:I was just asking if I were to use CSS for XML styling, how would i use classes and ids that are supported by CSS ?
Isn't the output (x)html? Then do and use whatever you can do and use in (x)html.
Post Reply