yetti78
asked on
XSL Transform of RSS feed that includes multiple namespaces
I am using Java (EE 5) using standard XSL transformer and would like to perform a relatively simple activity: take a valid RSS 2.0 feed that has some additional elements (with a custom namespace) and process it using XSL to create an HTML page.
RSS
---------------------------------------
<rss version="2.0" xmlns:customns="http://www.helpyoulist.com/customns">
<channel>
<title>Daily listings for Acme Inc</title>
<link>http://www.acmeinc.com/</link>
<description>Daily listings for Acme Inc</description>
<language>en-us</language>
<copyright>Copyright 2009</copyright>
<customns:companyName>Acme Incorporated</customns:companyName>
<customns:address1>123 Main Street</customns:address1>
<customns:city>Anytown</customns:city>
<customns:state>NJ</customns:state>
<customns:zip>12345</customns:zip>
<customns:phone>(123) 555-1212</customns:phone>
<customns:fax>(123) 555-1213</customns:fax>
<customns:email>info@acmeinc.com</customns:email>
<item>
<title>2008 Acme Floor Sander</title>
<guid>http://www.acmeinc.com/ACL1.html</guid>
<description>What a great floor sander!</description>
<customns:listingID>ACL1</customns:listingID>
<customns:manufacturer>Acme</customns:manufacturer>
<customns:model>FS1</customns:model>
<customns:year>2008</customns:year>
<customns:serial>5207</customns:serial>
<customns:condition>New</customns:condition>
<customns:askingPrice>519.00</customns:askingPrice>
<customns:contact customns:sortOrder="1">
<customns:firstName>Raul</customns:firstName>
<customns:lastName>Jones</customns:lastName>
<customns:email>rjones@acmeinc.com</customns:email>
<customns:phone>(123) 555-1213</customns:phone>
</customns:contact>
<customns:contact customns:sortOrder="2">
<customns:firstName>Brandon</customns:firstName>
<customns:lastName>Boyd</customns:lastName>
<customns:email>bboyd@acmeinc.com</customns:email>
<customns:phone>(123) 555-1214</customns:phone>
</customns:contact>
</item>
<item>
<title>2007 Acme Belt Sander</title>
<guid>http://www.acmeinc.com/ACL2556.html</guid>
<description>This belt sander rules!</description>
<customns:listingID>ACL2556</customns:listingID>
<customns:manufacturer>Acme</customns:manufacturer>
<customns:model>BS1</customns:model>
<customns:year>2004</customns:year>
<customns:serial>4006</customns:serial>
<customns:condition>Used</customns:condition>
<customns:askingPrice>299.00</customns:askingPrice>
<customns:contact customns:sortOrder="1">
<customns:firstName>Brandon</customns:firstName>
<customns:lastName>Boyd</customns:lastName>
<customns:email>bboyd@acmeinc.com</customns:email>
<customns:phone>(123) 555-1214</customns:phone>
</customns:contact>
<customns:contact customns:sortOrder="2">
<customns:firstName>Raul</customns:firstName>
<customns:lastName>Jones</customns:lastName>
<customns:email>rjones@acmeinc.com</customns:email>
<customns:phone>(123) 555-1213</customns:phone>
</customns:contact>
</item>
</channel>
</rss>
---------------------------------------
XSL
---------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:customns="http://www.helpyoulist.com/customns">
<xsl:output method="html" version="4.01" />
<xsl:output doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" />
<xsl:output doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" />
<xsl:template match="rss/channel">
<xsl:apply-templates select="item"/>
</xsl:template>
<xsl:template match="item">
<xsl:if test="customns:guid='http://www.acmeinc.com/ACL1.html'"> <!-- WILL REPLACE WITH VAR LATER -->
<xsl:value-of select="title" /><br /> <!-- WORKS FINE! -->
<xsl:value-of select="customns:model" />|<br /> <!-- IT'A NO LIKA! -->
/xsl:if>
</xsl:template>
</xsl:stylesheet>
Furthermore you have to tell us what is not working,
changing the test element namespace, makes both title and customns:model show
so I don't see the problem now
changing the test element namespace, makes both title and customns:model show
so I don't see the problem now
ASKER
Right, I was qualifying (temporarily) in the standard namespace. Here, inside the if is the thing it won't display:
<xsl:value-of select="customns:model" />|<br /> <!-- IT'A NO LIKA! -->
<xsl:value-of select="customns:model" />|<br /> <!-- IT'A NO LIKA! -->
It displays nicely on my machine,
I am now guessing what was in the RSS that didn't work, that is bad guessing of course,
can you please post the real RSS causing you problems?
I am now guessing what was in the RSS that didn't work, that is bad guessing of course,
can you please post the real RSS causing you problems?
ASKER
The problem is that when the transform is run, the transformer doesn't output the element that is prefixed with the customns namespace (in this case customns:model), so on that line in the outputted HTML page is '|<br />', instead of 'FS1|<br />'
Hope that makes it more clear, thanks.
Hope that makes it more clear, thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Gertone,
Add this declaration inside the <xsl:stylesheet at top of XSL
extension-element-prefixes ="customns "
So that it looks like below:
What XSL processor are you using?
Add this declaration inside the <xsl:stylesheet at top of XSL
extension-element-prefixes
So that it looks like below:
What XSL processor are you using?
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:customns="http://www.helpyoulist.com/customns"
extension-element-prefixes="customns">
You don't have to declare an extension-element prefix, to get rid of the namespace nodes
this is what you should do
exclude-result-prefixes="c ustomns"
I am testing with Xalan, Saxon6.5 and Saxon 9.1
All give the same correct result
Which one are you using?
this is what you should do
exclude-result-prefixes="c
I am testing with Xalan, Saxon6.5 and Saxon 9.1
All give the same correct result
Which one are you using?
ASKER
Got it!
What was the problem?
Funny, I just see this thread appearing on the biglist... Ken making similar suggestions :)
<xsl:if test="guid='http://www.acmeinc.com/ACL1.html'"> <!-- WILL REPLACE WITH VAR LATER -->
instead of
<xsl:if test="customns:guid='http://www.acmeinc.com/ACL1.html'"> <!-- WILL REPLACE WITH VAR LATER -->