如何使用 XSLT 从 XML 文件创建 RSS 2.0 提要? [英] How can I use XSLT to create an RSS 2.0 feed from an XML file?

查看:73
本文介绍了如何使用 XSLT 从 XML 文件创建 RSS 2.0 提要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想澄清一下,看看我是否正确理解了这个问题.

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

我有一个来自网站的 XML 文件,其中列出了该网站的所有数据.我正在尝试创建一个 RSS 2.0 提要,以便我可以将数据提要上传到 Google 产品.XML 文件包含太多信息;我只想从中取出产品并将它们放入一个较小的 XML 或 RSS 文件中,其中包含 Google 想要的特定频道和标签.

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.

有人告诉我使用 XSLT 样式表.它看起来像最终项目的空模板吗?

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

例如

< channel >  
< product >  
< othertags >

我是否需要在 XML 文件上运行一个小脚本来填充最终产品?

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

编写此脚本的最简单方法是什么?我可以用 bash 做吗?

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


我看到了在 Dreamweaver 中附加 XSLT 样式表的能力;有人知道这个吗?


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

新我做了更多的研究,并让 XSLTPalette 在 OSX 上工作.我使用随机 XSLTsheet 测试了一小部分 XML 文件,结果很好.我尝试创建的 XSLTsheet 使用以下参数:

NEW 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>

所有这些标签在 XML 文件中不一定是相同的名称,所以在查看我将哪些值放在哪里后,我该如何对应这两者?XML 文件大约 6MB,我怀疑能否用我的 2GB 处理它.我应该先尝试修剪文件吗?我从之前上传的网站中获取了字段,但很多链接不正确,因此重做,它是在 RSS 2.0 中,所以我想我应该坚持下去.

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.

以下是 XML 文件中单个产品的摘录:

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>

另外,我是否可以使用 foreach 为每个元素赋予保持不变的标签,例如条件、接受付款,即使它们在原始文件中没有相应的值?

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?

推荐答案

它看起来像最终项目的空模板吗?

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

对不起,你能澄清一下吗?如果您在谈论您的示例,那么结果 xml 可能如下所示.这取决于您的 XSLT 样式表.

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.

我是否需要在 XML 文件上运行一个小脚本来填充最终产品?

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

您需要对源 XML 文档应用 XSLT 转换才能获得结果.

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

编写此脚本的最简单方法是什么?我可以用 bash 做吗?

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

您可以安装任何可用的 XSLT 处理器,创建一个 bash 脚本,将您的 XML 提供给您的 XSLT 样式表并生成结果并使用 cron 或手动运行您的脚本等.

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.

我看到了在 Dreamweaver 中附加 XSLT 样式表的能力;有没有人知道这个?

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

这是为了简化 XSLT 样式表的开发.

This is to simplify development of the XSLT stylesheets.

所有这些标签在 XML 文件中不一定是相同的名称,所以在查看我将哪些值放在哪里后,我将如何将两者对应起来?

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?

您必须编写将输入节点转换为输出的 xslt 模板.F.i.

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

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

互联网上有关于 XSLT 的很好的教程.

There are good tutorials on XSLT available on the internet.

XML 文件大约 6MB,我怀疑能否用我的 2GB 处理它,我应该先尝试修剪文件吗?

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 处理器可以处理很多比这更大的文件.

XSLT processor can process files a lot larget than that.

这篇关于如何使用 XSLT 从 XML 文件创建 RSS 2.0 提要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆