homepage Welcome to WebmasterWorld Guest from 54.204.64.152
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Website
Home / Forums Index / Code, Content, and Presentation / XML Development
Forum Library, Charter, Moderators: httpwebwitch

XML Development Forum

    
XML & XSL question from newbie
dawlish




msg:4259960
 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>
<OrderId>2</OrderId>
<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>
<LoanAmount>10000.00</LoanAmount>
<LoanDuration>4</LoanDuration>
<Apr>7.2</Apr>
<InterestRate>7.20</InterestRate>
<FeeAmount p2:nil="true" xmlns:p2="http://www.w3.org/2001/XMLSchema-instance" />
<MonthlyPayment>239.34</MonthlyPayment>
<TotalChargeForCredit>1488.00</TotalChargeForCredit>
<TotalAmountPayable>11488.00</TotalAmountPayable>
<MinAdvance>7500.00</MinAdvance>
<MaxAdvance>14999.00</MaxAdvance>
<DetailsUrl>http://www.example.com</DetailsUrl>
<ApplyUrl>http://www.example.com</ApplyUrl>
<ProviderSiteUrl>http://www.example.com/loans/LoansExternalDb_new.asp?BestBuy=1&Source=bbt&LoansID=1562</ProviderSiteUrl>
<LogoUrl>http://www.example.com/images/Sainsburys100.gif</LogoUrl>
<IsSecured>false</IsSecured>
<IsFixedRate>true</IsFixedRate>
<HeadlineApr>7.20</HeadlineApr>
</Product>






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


<xsl:template match="/">

<html>
<body>

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


<div class="deal">

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




</div>




</xsl:for-each>


</body>
</html>

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

 

httpwebwitch




msg:4260005
 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

?

dawlish




msg:4260233
 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">

<table>
<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>?

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

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

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

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

</td>
</tr>


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>

httpwebwitch




msg:4260558
 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.

[w3schools.com...]

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.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved