• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 805
  • Last Modified:

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>

Open in new window

0
yetti78
Asked:
yetti78
  • 7
  • 4
1 Solution
 
Geert BormansInformation ArchitectCommented:
In your example the element guid is not in the customns namespace

       <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 -->
0
 
Geert BormansInformation ArchitectCommented:
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
0
 
yetti78Author Commented:
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! -->
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
Geert BormansInformation ArchitectCommented:
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?
0
 
yetti78Author Commented:
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.
0
 
Geert BormansInformation ArchitectCommented:
given your original XSLT (after fixing the test)
and using your original RSS posted
I get this
2008 Acme Floor Sander<br xmlns:customns="http://www.helpyoulist.com/customns">FS1|<br xmlns:customns="http://www.helpyoulist.com/customns">

I think this is what you are expecting,
so I can't reproduce your problem, given the data I have now

(you need to set the br namespace right by the way)
0
 
yetti78Author Commented:
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?
<?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">

Open in new window

0
 
Geert BormansInformation ArchitectCommented:
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="customns"

I am testing with Xalan, Saxon6.5 and Saxon 9.1
All give the same correct result
Which one are you using?
0
 
yetti78Author Commented:
Got it!
0
 
Geert BormansInformation ArchitectCommented:
What was the problem?
0
 
Geert BormansInformation ArchitectCommented:
Funny, I just see this thread appearing on the biglist... Ken making similar suggestions :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

  • 7
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now