homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / XML Development
Forum Library, Charter, Moderators: httpwebwitch

XML Development Forum

XML & XSL question from newbie

10+ Year Member

Msg#: 4259958 posted 6:12 pm on Jan 29, 2011 (gmt 0)

I'm trying to broaden the content on my website somewhat and have been approached by a third party to publish content available via their XML feeds which they make available to selected parties. I understand I need to create an XSL stylesheet.

I have very little experience in this area and have been struggling for the last couple of days. I've worked through a few online tutorials and examples and managed to get them to work no problem. The issue is I don't seem to be able to apply the knowledge I've acquired to the XML feed I need to process.

Below is an extract from the XML feed and the simple XSL sheet I've created. It returns no content what so ever. If I change the template match to xyz it returns all the data contained the the XML file. Any advice as to where I'm going wrong would be much appreciated.

Website domain removed.

- <Loans xmlns="http://api.example.com">
- <Product xmlns="">
<CategoryName>Standard Loans</CategoryName>
<LoanName>Sainsbury's Personal Loan</LoanName>
<LoanComments>Nectar card holders receive double Nectar points for 2yrs on 7.5k-14,999. <b>Typical APR 7.2%</b></LoanComments>
<FeeAmount p2:nil="true" xmlns:p2="http://www.w3.org/2001/XMLSchema-instance" />

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:template match="/">


<xsl:for-each select="Product">

<div class="deal">

<xsl:value-of select="LoanName"/>







WebmasterWorld Administrator httpwebwitch us a WebmasterWorld Top Contributor of All Time 10+ Year Member

Msg#: 4259958 posted 9:16 pm on Jan 29, 2011 (gmt 0)

What happens if you do:

<xsl:template match="/Product">

then get rid of the for-each



10+ Year Member

Msg#: 4259958 posted 7:55 pm on Jan 30, 2011 (gmt 0)

If I implement what you suggest I receive an XML error.

However if I make it <xsl:template match="Product"> rather than <xsl:template match="/Product"> I can start to make things work.

However I want to display data in a table and have either some divs or an actual <table> displayed before listing the product details. Won't I need a foreach for that?

For instance

<xsl:template match="Product">

<tr><th>Loan</th><th>Min Amount</th><th>Max Amount</th><th>Representative APR (variable)</th></tr>

Then I'm confused what to put here to stop it repeating the above header for each <Product>?

<xsl:value-of select="LoanName"/>

&#163;<xsl:value-of select="MinAdvance"/>

&#163;<xsl:value-of select="MaxAdvance"/>

<xsl:value-of select="Apr"/>%


Also how could I split the table with a new row for each CategoryName. I don't want to duplicate the CategoryName for each <Product> so would want to add a new row to group products underneath if that makes sense?

<th colspan="4"><xsl:value-of select="CategoryName"/></th>


WebmasterWorld Administrator httpwebwitch us a WebmasterWorld Top Contributor of All Time 10+ Year Member

Msg#: 4259958 posted 2:23 pm on Jan 31, 2011 (gmt 0)

You're on the right track.

In many cases when you're using a for-each, a template match would do the same job. It's just a matter of taste. Personally I like using templates unless the situation demands a for-each.

your <th> headers have to appear outside the loop, so they're output once, then the loop begins throwing down a <tr> for each product.

So... begin with the template that matches "/", and write out the <table> beginning, and the <tr> row with the <th>'s in it.

Then do a <xsl:apply-templates select="Product" />

That will call a template which matches "Product", where you write out the data row.

Here's an example using an <h2> followed by a series of <p>'s. You'll do the same thing, but with a <table> followed by multiple <tr>'s.


Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / XML Development
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved