473,386 Members | 1,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

reading an XML and outputing an RSS feed.

Hello,

I've been working on this for a days and getting frustrated. Basically i want read a master xml file that references many other xml files and use this information to output an RSS Feed.

File to reference. this file is usally quite large and has many categories so it needs to be able to read each category and output a channel then item contents for each article id in the catecory.

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <vol_issue vol_issue_id="idname">
  3.  <description>text here</description>
  4.  <language>en</language>
  5.  <type>print</type>
  6.  <xmlfilename>filename.xml</xmlfilename>
  7.  <cdfilename>filename.xml</cdfilename>
  8.  <category category_id="2">
  9.   <sort_order>2</sort_order>
  10.   <article article_id="filename"/>
  11.   <article article_id="filename"/>
  12.   <article article_id="filename"/>
  13.   <article article_id="filename"/>
  14.   <article article_id="filename"/>
  15.   <article article_id="filename"/>
  16.   <article article_id="filename"/>
  17.   <article article_id="filename"/>
  18.   <article article_id="filename"/>
  19.  </category>
  20. </volissue>
this is the XSL i am building to handle the operation, at this point all it does is insert the top 6 lines of the above .xml which i do not even need.

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  3. <xsl:output method="xml" encoding="windows-1252" omit-xml-declaration="yes"/>
  4. <xsl:param name="type">print</xsl:param>
  5.     <xsl:param name="vol">200304</xsl:param>
  6.     <xsl:param name="cat">2</xsl:param>
  7.     <xsl:param name="articleid">12345</xsl:param>
  8. <xsl:variable name="OpenCDATASection"><![CDATA[<![]]><![CDATA[CDATA[]]></xsl:variable>
  9. <xsl:variable name="CloseCDATASection"><![CDATA[]]]]><![CDATA[>]]></xsl:variable>
  10. <xsl:variable name="cat_description" select="document('../articles/categories.xml')/main/category[@category_id = $cat]/description"/>
  11. <xsl:template match="feeds/feed">
  12. <rss version="2.0">
  13. <xsl:for-each select="@category_id">
  14.     <channel>
  15.         <title><xsl:variable name="catid" select="@category_id"/></title>
  16.         <description><xsl:value-of select="description"/></description>
  17.         <link></link>
  18.         <lastBuildDate><xsl:value-of select="@feeddate"/></lastBuildDate>
  19.  
  20.     <xsl:variable name="feed_articles" select="document('../articles/') "/>
  21.  
  22.     <xsl:for-each select="article">
  23.  
  24.         <xsl:variable name="articleid" select="."/>
  25.         <xsl:variable name="article" select="document(concat('../articles/', $articleid, '.xml'))"/>
  26.  
  27.  
  28.  
  29.             <item>
  30.                 <title><xsl:value-of select="$article/article/headline"/></title>
  31.                 <description><xsl:value-of select="$article/article/blurb"/></description>
  32.                 <link>http://linkhere?articleID=<xsl:value-of select="$articleid"/></link>
  33.  
  34.             </item>
  35.             </xsl:for-each>
  36.  
  37. </channel>
  38. </xsl:for-each>
  39. </rss>
  40. </xsl:template>
  41. </xsl:stylesheet>
Hope you can help.
Nov 8 '07 #1
11 1731
jkmyoung
2,057 Expert 2GB
I can see several problems with your xslt and xpath. To start with:
1. For-each context problem
Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="feeds/feed">
  2. ...
  3. <xsl:for-each select="@category_id">
  4.  
This changes the context to the category_id attribute node.
The title doesn't make sense, nor does the description.
I would remove the for-each altogether.

2. <xsl:variable name="feed_articles" select="document('../articles/') "/>
This is a directory, not an xml file, and would cause problems.

3. <xsl:variable name="articleid" select="."/>
Don't you want <xsl:variable name="articleid" select="@articleid"/> ?

Possible other problem:
-<xsl:template match="feeds/feed">
Do you only have one feed element? Where is your feed element? Perhaps this should be <xsl:template match="/vol_issue"> instead?

Where are your articles xml files located?
Nov 9 '07 #2
I can see several problems with your xslt and xpath. To start with:
1. For-each context problem
Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="feeds/feed">
  2. ...
  3. <xsl:for-each select="@category_id">
  4.  
This changes the context to the category_id attribute node.
The title doesn't make sense, nor does the description.
I would remove the for-each altogether.

2. <xsl:variable name="feed_articles" select="document('../articles/') "/>
This is a directory, not an xml file, and would cause problems.

3. <xsl:variable name="articleid" select="."/>
Don't you want <xsl:variable name="articleid" select="@articleid"/> ?

Possible other problem:
-<xsl:template match="feeds/feed">
Do you only have one feed element? Where is your feed element? Perhaps this should be <xsl:template match="/vol_issue"> instead?

Where are your articles xml files located?
Thanks for the reply i will work through the problem areas that you have pointed out.

But i am still not 100% sure on how to make it read through the master list and treat each category as a seperate RSS channel.

Vol_issue if just another master file that holds 4 or 5 other master files and inside those files is what is contained with above .xml in mt first post.
Nov 11 '07 #3
Ok, mayeb this will give you a better understanding of what i need,

I have to read all the below information and export a single RSS file.

So it would be

<rss>

<channel> (treat each category id as a channel)

contents
<item>
contents
</item>
</channel>

<channel>
Contents
<item>
contents
</item>
</channel>
ect ect ect.
</rss>

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <vol_issue vol_issue_id="200712">
  3.  <description>December</description>
  4.  <language>en</language>
  5.  <type>print</type>
  6.  <xmlfilename>en_print_200712.xml</xmlfilename>
  7.  <cdfilename>cd_en_print_200712.xml</cdfilename>
  8.  <category category_id="2">
  9.   <sort_order>2</sort_order>
  10.   <article article_id="47106"/>
  11.   <article article_id="47032"/>
  12.   <article article_id="47015"/>
  13.   <article article_id="46852"/>
  14.   <article article_id="46828"/>
  15.   <article article_id="46827"/>
  16.   <article article_id="46826"/>
  17.   <article article_id="46825"/>
  18.   <article article_id="46434"/>
  19.  </category>
  20.  <category category_id="72">
  21.   <sort_order>7</sort_order>
  22.   <article article_id="47179"/>
  23.   <article article_id="47178"/>
  24.   <article article_id="47177"/>
  25.   <article article_id="47176"/>
  26.   <article article_id="47175"/>
  27.   <article article_id="47033"/>
  28.   <article article_id="47032"/>
  29.   <article article_id="47031"/>
  30.   <article article_id="46945"/>
  31.   <article article_id="46944"/>
  32.   <article article_id="46943"/>
  33.   <article article_id="46929"/>
  34.   <article article_id="46928"/>
  35.   <article article_id="46927"/>
  36.   <article article_id="46781"/>
  37.   <article article_id="46780"/>
  38.   <article article_id="46779"/>
  39.   <article article_id="46767"/>
  40.   <article article_id="46766"/>
  41.   <article article_id="46765"/>
  42.   <article article_id="46764"/>
  43.   <article article_id="46763"/>
  44.   <article article_id="46621"/>
  45.   <article article_id="46617"/>
  46.   <article article_id="46601"/>
  47.   <article article_id="46592"/>
  48.   <article article_id="46591"/>
  49.   <article article_id="46590"/>
  50.   <article article_id="46589"/>
  51.   <article article_id="46588"/>
  52.   <article article_id="46587"/>
  53.   <article article_id="46586"/>
  54.   <article article_id="46585"/>
  55.   <article article_id="46457"/>
  56.   <article article_id="46456"/>
  57.   <article article_id="46455"/>
  58.   <article article_id="46433"/>
  59.   <article article_id="46419"/>
  60.   <article article_id="46418"/>
  61.   <article article_id="46417"/>
  62.   <article article_id="46416"/>
  63.  </category>
  64.  <category category_id="5">
  65.   <sort_order>10</sort_order>
  66.   <article article_id="47206"/>
  67.   <article article_id="46822"/>
  68.   <article article_id="46821"/>
  69.   <article article_id="46490"/>
  70.  </category>
  71.  <category category_id="6">
  72.   <sort_order>11</sort_order>
  73.   <article article_id="46976"/>
  74.   <article article_id="46975"/>
  75.   <article article_id="46945"/>
  76.   <article article_id="46944"/>
  77.   <article article_id="46943"/>
  78.   <article article_id="46639"/>
  79.   <article article_id="46638"/>
  80.   <article article_id="46637"/>
  81.   <article article_id="46489"/>
  82.  </category>
  83.  <category category_id="7">
  84.   <sort_order>12</sort_order>
  85.   <article article_id="46453"/>
  86.   <article article_id="46452"/>
  87.  </category>
  88.  <category category_id="8">
  89.   <sort_order>13</sort_order>
  90.   <article article_id="47208"/>
  91.   <article article_id="47206"/>
  92.   <article article_id="46685"/>
  93.   <article article_id="46638"/>
  94.   <article article_id="46630"/>
  95.   <article article_id="46491"/>
  96.   <article article_id="46490"/>
  97.   <article article_id="46489"/>
  98.   <article article_id="46475"/>
  99.   <article article_id="46432"/>
  100.  </category>
  101.  <category category_id="9">
  102.   <sort_order>14</sort_order>
  103.   <article article_id="47142"/>
  104.   <article article_id="47141"/>
  105.   <article article_id="46939"/>
  106.   <article article_id="46687"/>
  107.   <article article_id="46518"/>
  108.   <article article_id="46517"/>
  109.   <article article_id="46445"/>
  110.   <article article_id="46444"/>
  111.  </category>
  112. </vol_issue>
  113.  
Nov 12 '07 #4
jkmyoung
2,057 Expert 2GB
Let's go about this using the template method.
You have one main node, vol_issue. Use this to create the main node in your result, and use apply-templates to create the rest of what you need. I also notice you have sort order in your categories, so we'll use that as well.
Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="vol_issue"><!-- main template -->
  2.   <rss version="2.0">
  3.     <xsl:apply-templates select="category">
  4.        <xsl:sort select="sort_order"/>
  5.     </xsl:apply-templates>
  6.   </rss>
  7. </xsl:template>
You said:
(treat each category id as a channel)
So, assuming each category has a different id, we create a template for it.
Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="category">
  2.   <channel>
  3.       <title><xsl:variable name="catid" select="@category_id"/></title>
  4.       <link></link>
Unfortunately, the description is in the root node. So, we use .. to go up one level. Is feed date vol_issue_id?
Expand|Select|Wrap|Line Numbers
  1.     <description><xsl:value-of select="..\description"/></description>
  2.     <feed_date><xsl:value-of select="..\@vol_issue_id"/></feed_date>
  3.     <xsl:apply-templates select="article"> 
  4. </xsl:template>
We've also included an apply-templates on the article, so now let's finish up with the article template.
Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="article">
  2.   <xsl:variable name="article" select="document(concat('../articles/', @article_id, '.xml'))"/>
  3.   <item>
  4.     <title><xsl:value-of select="$article/article/headline"/></title>
  5.     <description><xsl:value-of select="$article/article/blurb"/></description>
  6.     <link>http://linkhere?articleID=<xsl:value-of select="@article_id"/></link>
  7.   </item>
  8. </xsl:template>
I hope breaking it up into templates makes it easier to understand.
Nov 13 '07 #5
I hope breaking it up into templates makes it easier to understand.
Thanks again for the reply, yes it does make more sense that way,

Unfortunately, the description is in the root node. So, we use .. to go up one level. Is feed date vol_issue_id?
the vol_issue_id is the file that will be referenced to build the RSS, so on my asp page i select which issue to build the RSS from. The feed date can be any date.


Thanks again.
Nov 13 '07 #6
Ok all is well now with the exception of 1 item, i need to remove duplicates filenames in the output

so if <article article_id="47208"/> appears more than once in the vol_issue i don't want to write it more than once in the outputed XML.
Nov 20 '07 #7
jkmyoung
2,057 Expert 2GB
Within the same channel, or not?

Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="article">
  2.   <xsl:if test="not(preceding-sibling::article[@article_id = current()/@article_id])">
  3.  
If the parent node doesn't matter, then use preceding:: instead of preceding-sibling::
Nov 20 '07 #8
Within the same channel, or not?

Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="article">
  2.   <xsl:if test="not(preceding-sibling::article[@article_id = current()/@article_id])">
  3.  
If the parent node doesn't matter, then use preceding:: instead of preceding-sibling::
Thanks for all the help things are working they i need it to now.
Nov 20 '07 #9
ok i have made a slight error, i need to refernce the channel description from yet another .xml we will call it categories.xml

Expand|Select|Wrap|Line Numbers
  1. <main>
  2.  <category category_id="00">
  3.   <description>text here</description>  <--channel description to pull.
  4. <cd_folder></cd_folder>
  5.   <incl_in_reg></incl_in_reg>
  6.   <sort_order></sort_order>
  7.   <language></language>
  8.   <image_filename></image_filename>
  9.  </category>
  10.  
Any thoughts?
Nov 22 '07 #10
jkmyoung
2,057 Expert 2GB
Ok, let's say you have a category in variable $category
To access the other document, we use the document() function.

eg.
<xsl:value-of select="document('category.xml')//category[@category_id=$category]/description"/>
Nov 23 '07 #11
Ah thank you, //category is the step i was missing.

Thanks again you are the best.
Nov 26 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: bissatch | last post by:
Hi, I have the following XML content in a file (main.xml): <?xml version="1.0" encoding="iso-8859-1"?> <menu> <option title="Home" href="home.php"> <option title="News" href="news.php">...
3
by: Simon | last post by:
Using qbasic, the following accepts input from com1, and prints its output : OPEN "com1:9600,n,8,1,CD0,CS0,DS0,RS" FOR INPUT AS #1 WHILE INKEY$ <> " " INPUT #1, a$ PRINT a$ WEND What...
2
by: Antonio Ooi | last post by:
Hi, Probably due to my 'Language for non-Unicode Setting' in Regional Settings, my ASP page keeps outputing the date format as Chinese Simplified characters when issuing say, <%=Now()%>. In...
0
by: Julian | last post by:
I have written a CGI program which takes an RSS feed and turns it into HTML. I'm using it to display the latest headlines from the BBC News website. The question is, what order should I display...
4
by: Michael Carr | last post by:
I have a function that populates a class with values from a database. I'd like to pass into the function either a SqlDataReader or a DataRow, depending on which mechanism I'm using to retrieve data...
5
by: WebBuilder451 | last post by:
is there ant place where i can learn the basics of reading an rss feed and enumerating through the values? just want to start at the bottom, no frills. thanks -- thanks (as always) some day...
1
by: Allerdyce.John | last post by:
I need some help in reading error message: which line has problem? line 233? or line 37? Thank you. $ ./read2.py log.xml Traceback (most recent call last): File "./read2.py", line 233, in ?...
4
by: Gaijinco | last post by:
I had a file named nap.in which looks like this: 4 10:00 12:00 Lectures 12:00 13:00 Lunch, like always. 13:00 15:00 Boring lectures... 15:30 17:45 Reading 4 10:00 12:00 Lectures 12:00 13:00...
1
by: une | last post by:
hey guys, I have this program to do, but I kind of started forst just te read things from the file and then I would go and make the calculation but it is showing some wierd funnky resualts, I dont...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.