Solved

XSLT - HTML Transform problems

Posted on 2009-07-05
10
550 Views
Last Modified: 2013-11-18
I am having problems with this xslt transformation

The XSLT should make it obivous what i am trying to achieve

I have included a cut down version of the XML, and my XSLT attempt, i keep getting the error invalid pattern in XMLSpy

Thank you for looking at this
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?xml-stylesheet type="text/xsl" href="file:///C:/Documents%20and%20Settings/Chris/My%20Documents/Altova/XMLSpy2009/Examples/ClosestSoFar.xslt"?>
<ProductSearch xmlns="urn:yahoo:prods">
	<Categories total_subcategories="18">
		<SubCategory>
			<Title>Projector Accessories</Title>
			<Value>100375223</Value>
			<NumberOfProducts>1</NumberOfProducts>
		</SubCategory>
	</Categories>
	<Products firstResultPosition="1" totalResultsAvailable="435" totalResultsReturned="20">
		<Product type="Offer">
			<Offer id="960e67b21e0ddfafdbb0b77716e98bbc">
				<Url>http://uk.shopping.yahooapis.com/ctl/go/sitesearchGo?.ts=1246725457647&amp;.sig=h4eRVQJiZWr.Ld1JJdxhNvz_ECc-&amp;offerId=960e67b21e0ddfafdbb0b77716e98bbc&amp;searchId=87248117220_1246725457645_1206933&amp;affiliationId=96929528&amp;country=uk&amp;wait=true&amp;comId=7546223&amp;catId=127101</Url>
				<CompareUrl>http://audiovisual.kelkoo.co.uk/p-plasma-tvs-127101/samsung-ps50b430-19601770?partnerId=96929528</CompareUrl>
				<ProductName>Samsung PS50B430 ** Special Deal ** Available if...</ProductName>
				<Price currency="gbp">699.00</Price>
				<DeliveryCost>0.0</DeliveryCost>
				<DeliveryTime>1 To 3 Working Days</DeliveryTime>
				<Availability>Availability: In stock</Availability>
				<Used>false</Used>
				<ListImage>
					<Url>http://img.kelkoo.com/uk/medium/624/631/87817543067979507255394251041155631624.jpg</Url>
					<Height>90</Height>
					<Width>90</Width>
				</ListImage>
				<GridImage>
					<Url>http://img.kelkoo.com/uk/medium/624/631/87817543067979507255394251041155631624.jpg</Url>
					<Height>90</Height>
					<Width>90</Width>
				</GridImage>
				<Summary>Free Mainland UK Delivery Only - Samsung PS50B430 50&amp;quot; HD Ready PLASMA Picture Playback Screen Size 50 &amp;quot;&amp;quot; Resolution max. 1365 x 768 1365 x 768 Image ...</Summary>
				<Merchant id="7546223">
					<Name>Electro Centre</Name>
				</Merchant>
				<CatalogID>127101-19601770</CatalogID>
				<Category id="127101">
					<Name>Plasma TVs</Name>
				</Category>
			</Offer>
		</Product>
	</Products>
	<Warnings>
		<Warning>The "merchantId" parameter is not given. The default value "-1" is used.</Warning>
		<Warning>The "logicalType" parameter is not given. The default value "and" is used.</Warning>
	</Warnings>
</ProductSearch>
 
 
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:user="urn:comparionsplus-com:xslt">
	<xsl:output method="html"/>
	<xsl:template match="ProductSearch/Products">
		<xsl:variable name="resultsPosition">
			<xsl:value-of select="@firstResultPosition"/>
		</xsl:variable>
		<xsl:variable name="totalResults">
			<xsl:value-of select="@totalResultsAvailable"/>
		</xsl:variable>
		<xsl:variable name="returnedResults">
			<xsl:value-of select="@totalResultsReturned"/>
		</xsl:variable>
	</xsl:template>
	<xsl:template match="Products/Product/Offer/">
		<xsl:variable name="prodName">
			<xsl:value-of select="ProductName"/>
		</xsl:variable>
		<xsl:variable name="prodDesc">
			<xsl:value-of select="Summary"/>
		</xsl:variable>
		<xsl:variable name="catID">
			<xsl:value-of select="Category"/>
		</xsl:variable>
		<xsl:variable name="catName">
			<xsl:value-of select="Category/Name"/>
		</xsl:variable>
		<xsl:variable name="merchantID">
			<xsl:value-of select="Merchant/@id"/>
		</xsl:variable>
		<xsl:variable name="merchantName">
			<xsl:value-of select="Merchant/Name"/>
		</xsl:variable>
		<xsl:variable name="clickLink">
			<xsl:value-of select="Url"/>
		</xsl:variable>
		<xsl:variable name="imageLink">
			<xsl:value-of select="ListImage/Url"/>
		</xsl:variable>
		<xsl:variable name="compareLink">
			<xsl:value-of select="CompareUrl"/>
		</xsl:variable>
		<xsl:variable name="prodPrice">
			<xsl:value-of select="Price"/>
		</xsl:variable>
		<xsl:variable name="delCost">
			<xsl:value-of select="DeliveryCost"/>
		</xsl:variable>
		<xsl:variable name="compareLink">
			<xsl:value-of select="CompareUrl"/>
		</xsl:variable>
		<xsl:variable name="prodAvail">
			<xsl:value-of select="Availability"/>
		</xsl:variable>
		<xsl:variable name="prodState">
			<xsl:value-of select="Used"/>
		</xsl:variable>
	</xsl:template>
	<xsl:apply-templates/>
					
					First Result Position: <xsl:value-of select="$resultsPosition"/>
	<br/>
					Total Results: <xsl:value-of select="$totalResults"/>
	<br/>
					Results on Page: <xsl:value-of select="$returnedResults"/>
	<br/>
					
					Category id: <xsl:value-of select="$catID"/>
	<br/>
					Product Name: <xsl:value-of select="$prodName"/>
	<br/>
					Long Desc: <xsl:value-of select="$prodDesc"/>
	<br/>
					Category Name: <xsl:value-of select="$catName"/>
	<br/>
					Merchant ID: <xsl:value-of select="$merchantID"/>
	<br/>
					Merchant Name: <xsl:value-of select="$merchantName"/>
	<br/>
					Product Link: <xsl:value-of select="$clickLink"/>
	<br/>
					Image Link: <xsl:value-of select="$imageLink"/>
	<br/>
					Comparison Link: <xsl:value-of select="$compareLink"/>
	<br/>
					Product Price: <xsl:value-of select="$prodPrice"/>
	<br/>
					Delivery Price: <xsl:value-of select="$delCost"/>
	<br/>
					New/Used: <xsl:value-of select="$prodState"/>
	<br/>
					Availabile: <xsl:value-of select="$prodAvail"/>
	<br/>
					Short Desc: <xsl:value-of select="substring(substring-before($prodDesc,'.'),1,1000)"/>
	<br/>
	<xsl:template match="Warnings"/>
	<xsl:template match="Categories/*"/>
	<xsl:template match="Products/Product/Offer/GridImage"/>
</xsl:stylesheet>

Open in new window

0
Comment
Question by:QuinnDester
  • 6
  • 4
10 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 24780121
No, the XSLT does not make it entirely obvious what you are trying to achiev.
You have one obvious error
line 59 of the XSLT, you have     <xsl:apply-templates/>
and that apply templates and all the following lines are not in a template.
These actions are only allowed inside an xsl:template
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 24780126
further more

line 15
   <xsl:template match="Products/Product/Offer/">
is a pattern that is not allowed,
it should be
   <xsl:template match="Products/Product/Offer">

line 49
        <xsl:variable name="compareLink">
is allready defined
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 24780132
I assume th eproblem is the line 15 one,
all the others are likely due to your cutting in the XSLT for posting

A good tip, make sure that when posting snippets, that you isolate the problem,
not introducing errors because of the cut
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 3

Author Comment

by:QuinnDester
ID: 24780190
Thanks Gertone

I have corrected the Line 15 error and removed the <xsl:apply-templates/>
the line 49 error was introduced through the cut and past sorry about that i will be more carful in future.

I know get the error that the parameter or veriable is already defined on line 57 this should be where the XSLT starts to outbut the results.

the xslt here is just trying to output the information into a list (done this way so i can get the XSLT functioning before applying any HTML formating to the results)

I am completely new to XSLT this is my first attempt at doing anything with it, Please bare in mind that what you see as obvious may be completely oblivious to me.

Thanks
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:user="urn:comparionsplus-com:xslt">
	<xsl:output method="html"/>
	<xsl:template match="ProductSearch/Products">
		<xsl:variable name="resultsPosition">
			<xsl:value-of select="@firstResultPosition"/>
		</xsl:variable>
		<xsl:variable name="totalResults">
			<xsl:value-of select="@totalResultsAvailable"/>
		</xsl:variable>
		<xsl:variable name="returnedResults">
			<xsl:value-of select="@totalResultsReturned"/>
		</xsl:variable>
	</xsl:template>
	<xsl:template match="Products/Product/Offer">
		<xsl:variable name="prodName">
			<xsl:value-of select="ProductName"/>
		</xsl:variable>
		<xsl:variable name="prodDesc">
			<xsl:value-of select="Summary"/>
		</xsl:variable>
		<xsl:variable name="catID">
			<xsl:value-of select="Category"/>
		</xsl:variable>
		<xsl:variable name="catName">
			<xsl:value-of select="Category/Name"/>
		</xsl:variable>
		<xsl:variable name="merchantID">
			<xsl:value-of select="Merchant/@id"/>
		</xsl:variable>
		<xsl:variable name="merchantName">
			<xsl:value-of select="Merchant/Name"/>
		</xsl:variable>
		<xsl:variable name="clickLink">
			<xsl:value-of select="Url"/>
		</xsl:variable>
		<xsl:variable name="imageLink">
			<xsl:value-of select="ListImage/Url"/>
		</xsl:variable>
		<xsl:variable name="compareLink">
			<xsl:value-of select="CompareUrl"/>
		</xsl:variable>
		<xsl:variable name="prodPrice">
			<xsl:value-of select="Price"/>
		</xsl:variable>
		<xsl:variable name="delCost">
			<xsl:value-of select="DeliveryCost"/>
		</xsl:variable>
		<xsl:variable name="prodAvail">
			<xsl:value-of select="Availability"/>
		</xsl:variable>
		<xsl:variable name="prodState">
			<xsl:value-of select="Used"/>
		</xsl:variable>
	</xsl:template>
					
					First Result Position: <xsl:value-of select="$resultsPosition"/>
	<br/>
					Total Results: <xsl:value-of select="$totalResults"/>
	<br/>
					Results on Page: <xsl:value-of select="$returnedResults"/>
	<br/>
					
					Category id: <xsl:value-of select="$catID"/>
	<br/>
					Product Name: <xsl:value-of select="$prodName"/>
	<br/>
					Long Desc: <xsl:value-of select="$prodDesc"/>
	<br/>
					Category Name: <xsl:value-of select="$catName"/>
	<br/>
					Merchant ID: <xsl:value-of select="$merchantID"/>
	<br/>
					Merchant Name: <xsl:value-of select="$merchantName"/>
	<br/>
					Product Link: <xsl:value-of select="$clickLink"/>
	<br/>
					Image Link: <xsl:value-of select="$imageLink"/>
	<br/>
					Comparison Link: <xsl:value-of select="$compareLink"/>
	<br/>
					Product Price: <xsl:value-of select="$prodPrice"/>
	<br/>
					Delivery Price: <xsl:value-of select="$delCost"/>
	<br/>
					New/Used: <xsl:value-of select="$prodState"/>
	<br/>
					Availabile: <xsl:value-of select="$prodAvail"/>
	<br/>
					Short Desc: <xsl:value-of select="substring(substring-before($prodDesc,'.'),1,1000)"/>
	<br/>
	<xsl:template match="Warnings"/>
	<xsl:template match="Categories/*"/>
	<xsl:template match="Products/Product/Offer/GridImage"/>
</xsl:stylesheet>

Open in new window

0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 24780277
CAn you please post the full XSLT, because it is filled again with cut and paste errors
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 24780388
Well, I think you don't grasp the concept of templates

Templates are the building blocks of an XSLT
A node that requires processing picks the template that matches its pattern best
but in order to tell nodes that they require processing, you need to pass them using apply-templates.
All actions happen inside templates, operations outside templates are not allowed (and don't make sense)
Beware: XSLT is not procedural, so action happen as you progress through the XML data

I needed to restructure your XSLT entirely
You can use the Merchant/Name example as a basis for all the other thigs you need

- Variables in XSLT are not variables but constants and they don't live outside the scope they are created.
A variable created inside a template does not live outside that template
No need to use variables here, they are contraproductive

- your source is in a namespace
ProductSearch xmlns="urn:yahoo:prods" puts all descendants in the same namespace
in order to be able to prcess in XSLT, you need a namespace too, and since the output namespace is the default one in XSLT, you will need a prefix... all XPaths will need to be prefixed
<xsl:value-of select="yahoo:Merchant/yahoo:Name"/>

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
    xmlns:user="urn:comparionsplus-com:xslt"
    xmlns:yahoo="urn:yahoo:prods">
    <xsl:output method="html"/>
    <xsl:template match="/">
        <html>
            <body>
                <xsl:apply-templates select="yahoo:ProductSearch/yahoo:Products"/>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="yahoo:ProductSearch/yahoo:Products">
            <xsl:for-each select="@*">
                   <xsl:value-of select="name()"/>
                   <xsl:text> : </xsl:text>
                   <xsl:value-of select="."/>
               <br/>
            </xsl:for-each>
        <xsl:apply-templates select="yahoo:Product/yahoo:Offer"/>
    </xsl:template>
    <xsl:template match="yahoo:Products/yahoo:Product/yahoo:Offer">
        <xsl:text>Merchant Name: </xsl:text>
        <xsl:value-of select="yahoo:Merchant/yahoo:Name"/>
        <br/>
    </xsl:template>
    
</xsl:stylesheet>

Open in new window

0
 
LVL 3

Author Comment

by:QuinnDester
ID: 24780458
Thanks Gertone

I will try this in a little while if you dont mind, just bathing the kids ready for school in the morning

I have read lots about needing to include the name space but until now, didnt realise what the name space actualy was and where it was defined. that makes other problems i was having make much more sense,

thank you
0
 
LVL 3

Author Comment

by:QuinnDester
ID: 24780699
Excellent, Thank you very much, and again for the explination.

seeing the part you did and the explination i can see what i need to do to move forward and have a better understanding now.
0
 
LVL 3

Author Closing Comment

by:QuinnDester
ID: 31599917
Excellent help complete with explination of where i was going wrong.
Thank you
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 24780974
welcome, good luck
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ebay seller html tags 2 25
what are list of ebay api errors 1 19
How can I implement a "Select All" with this configuration...? 6 42
Button and js nou working 3 8
This article discusses four methods for overlaying images in a container on a web page
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question