• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 800
  • 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 BormansCommented:
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 BormansCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Geert BormansCommented:
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 BormansCommented:
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 BormansCommented:
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 BormansCommented:
What was the problem?
0
 
Geert BormansCommented:
Funny, I just see this thread appearing on the biglist... Ken making similar suggestions :)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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