Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
129 views
in Technique[技术] by (71.8m points)

How can I use XSLT to create an RSS 2.0 feed from an XML file?

I am trying to get a little clarification to see if I am understanding this problem correctly.

I have an XML file from a website that lists all data from that site. I am trying to create a RSS 2.0 feed so that I can upload a data feed to Google products. The XML file has too much info; I just want to take the products from it and put them into a smaller XML or RSS file with channels and tags specific to what Google wants.

I was told to use an XSLT stylesheet. Will it just look like an empty template of the final project?

E.g.

< channel >  
< product >  
< othertags >

Will I need a small script to run on the XML file to populate the final product?

What would be the easiest way to script this? Could I do it in bash?

EDIT:
I am seeing the ability to attach XSLT stylesheets in Dreamweaver; does anyone know about this?

NEWEDIT: I did more research and got XSLTPalette to work on OSX. I tested a small piece of the XML file with a random XSLTsheet and I got good results. The XSLTsheet I am trying to create uses these parameters:

<item>
  <title>
    <![CDATA[titlegoeshere]]>
  </title>
  <description>
    <![CDATA[general description with boilerplate]]>
  </description>
  <link>link to item page</link>
  <g:condition>new</g:condition>
  <g:price>19.99</g:price>
  <g:product_type>Clothing, Accessories</g:product_type>
  <g:image_link>linktoimage.jpg</g:image_link>
  <g:payment_accepted>Visa</g:payment_accepted>
  <g:payment_accepted>Mastercard</g:payment_accepted>
  <g:payment_accepted>Discover</g:payment_accepted>
</item>

All of these tags are not necessarily the same name in the XML file, so after looking at what values I would put where, how would I correspond the two? The XML file is about 6MB and I have doubts about being able to process it with my 2GB. Should I try and trim the file first? I got the fields from a previous upload for the site, but a lot of the links were incorrect, hence the redo, it was in RSS 2.0, so I imagine I should stick with that.

EDIT: Here is an excerpt of a single product in the XML file:

<Product Id="21082">      
  <Code>21082</Code>
  <Description>Cute Spaghetti Strap Top</Description>       
  <Url>http://www.clot333333nd.com/21082.html</Url>      
  <Thumb>&lt;img border=0 width=53 height=70 src=http://ep.yimg33333333333333333-27706119506618_2144_317650845&gt;</Thumb>
  <Picture>&lt;img border=0 width=1125 height=1500 src=http://ep.yim333333333333333306119506618_2144_207331152&gt;</Picture>
  <Weight>1</Weight>
  <Orderable>YES</Orderable>
  <Taxable>YES</Taxable>
  <Pricing>
    <BasePrice>9.99</BasePrice>
    <LocalizedBasePrice>9.99</LocalizedBasePrice>
    <OrigPrice>24.99</OrigPrice>
    <LocalizedOrigPrice>24.99</LocalizedOrigPrice>
    <SalePrice>9.99</SalePrice>
    <LocalizedSalePrice>9.99</LocalizedSalePrice>
  </Pricing>
  <Path>
    <ProductRef Id="contactus" Url="http://www.clo3333333333333ctus.html">Contact Us</ProductRef>
    <ProductRef Id="tops" Url="http://www.clot333333333333ps.html">Wholesale Clothing Tops</ProductRef>
  </Path>
  <Availability>Usually ships the next business day.</Availability>
  <Caption>th bgc&lnt&gt;&lt;/b&gt;&lt;/font&gt;&lt;/p&gt;</Caption>
  <OptionLists>
    <OptionList name="Size">
      <OptionValue>S</OptionValue>
      <OptionValue>M</OptionValue>
      <OptionValue>L</OptionValue>
      <OptionValue>Mixed-Sizes</OptionValue>
    </OptionList>
    <OptionList name="Color">
      <OptionValue>Blue</OptionValue>
    </OptionList>
    <OptionList name="Quantities">
      <OptionValue>1 Piece</OptionValue>
      <OptionValue>Add(+$30.89) For 12 Pieces</OptionValue>
      <OptionValue>Add(+54.77) For 24 Pieces</OptionValue>
    </OptionList>
  </OptionLists>
</Product>

I would like to make it look like this:

<item>  
  <title>  
    <![CDATA[titlegoeshere]]>    
  </title>  
  <description>  
    <![CDATA[general description with boilerplate]]>  
  </description>
  <link>link to item page</link>
  <g:condition>new</g:condition>
  <g:price>19.99</g:price>
  <g:product_type>Clothing, Accessories</g:product_type>
  <g:image_link>linktoimage.jpg</g:image_link>
  <g:payment_accepted>Visa</g:payment_accepted>
  <g:payment_accepted>Mastercard</g:payment_accepted>
  <g:payment_accepted>Discover</g:payment_accepted>
</item>

Here is how I want it to match up:

<item>
  <title>
    <![CDATA[titlegoeshere]]>  ##Description
  </title>
  <description>
    <![CDATA[general description with boilerplate]]> ##Caption
  </description>
  <link>link to item page</link> ##Url
  <g:condition>new</g:condition> ##always new
  <g:price>19.99</g:price>  ## BasePrice
  <g:product_type>Clothing, Accessories</g:product_type>   ##always same
  <g:image_link>linktoimage.jpg</g:image_link>  ## Picture
  <g:payment_accepted>Visa</g:payment_accepted>  ##always same
  <g:payment_accepted>Mastercard</g:payment_accepted>  ## always same
  <g:payment_accepted>Discover</g:payment_accepted>  #always same
</item>

Here is what I've built so far, but I am getting a few errors when I try to use it:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xmlns:g="http://base.google.com/ns/1.0" version="1.0">
  <xsl:output method="RSS 2.0" />
  <xsl:template match="Description">
    <title>
    </title>
  </xsl:template>
  <xsl:template match="Caption">
    <description>
    </description>
  </xsl:template>
  <xsl:template match="Url>
    <link>
    </link>
  </xsl:template>
  <xsl:template match="Condition">
    <g:condition>
    </g:condition>
  </xsl:template>
  <xsl:template match="Picture">
    <g:image_link>
    </g:image_link>
  </xsl:template>
</xsl:stylesheet>

Also, could I use foreach to give each element the tags that stay the same, such as condition, payment accepted, even if they don't have corresponding values in the original file?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Will it just look like an empty template of the final project?

I'm sorry, can you clarify on this? If you're talking about your example then it the result xml can look like this. That depends on your XSLT stylesheet.

Will I need a small script to run on the XML file to populate the final product?

You will need to apply an XSLT transformation to your source XML document to get the result.

What would be the easiest way to script this? Could I do it in bash?

You can install any of the available XSLT processors, create a bash script feeding your XML to your XSLT stylesheet and producing the result and run your script with cron or by hand, etc.

I am seeing the ability to attach XSLT stylesheets in Dreamweaver; does anyone know about this?

This is to simplify development of the XSLT stylesheets.

All of these tags are not necessarily the same name in the XML file, so after looking at what values i would put where, how would i correspond the two?

You will have to write xslt templates transforming input nodes to output. F.i.

<xsl:template match="input">
  <output/>
</xsl:template>

There are good tutorials on XSLT available on the internet.

the XML file is about 6MB and i have doubts about being able to process it with my 2GB, should i try and trim the file first?

XSLT processor can process files a lot larget than that.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...