我想弄清楚一下,看看我是否正確理解這個問題。如何使用XSLT從XML文件創建RSS 2.0訂閱源?
我有一個網站的XML文件,列出了該網站的所有數據。我正在嘗試創建RSS 2.0供稿,以便我可以將數據供稿上傳到Google產品。 XML文件有太多信息;我只想從中獲取產品,並將它們放入一個更小的XML或RSS文件中,其中包含特定於Google所需的渠道和標籤。
我被告知使用XSLT樣式表。它會看起來像最終項目的空模板嗎?
E.g.
<channel>
<product>
<othertags>
我需要一個小腳本來運行XML文件來填充最終產品嗎?
什麼是腳本的最簡單的方法?我可以在bash中做到嗎?
編輯:
我看到能夠在Dreamweaver中附加XSLT樣式表;有人知道嗎?
NEWEDIT: 我做了更多的研究,並讓XSLTPalette在OSX上工作。我用一個隨機的XSLT表格測試了一小段XML文件,並得到了很好的結果。我試圖創建XSLTsheet使用這些參數:
<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中,所以我想我應該堅持這一點。
編輯: 這裏是一個單一產品的XML文件中的摘錄:
<Product Id="21082">
<Code>21082</Code>
<Description>Cute Spaghetti Strap Top</Description>
<Url>http://www.clot333333nd.com/21082.html</Url>
<Thumb><img border=0 width=53 height=70 src=http://ep.yimg33333333333333333-27706119506618_2144_317650845></Thumb>
<Picture><img border=0 width=1125 height=1500 src=http://ep.yim333333333333333306119506618_2144_207331152></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></b></font></p></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>
我想使它看起來像這樣:
<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>
這裏是我想要的以匹配:
<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>
這是我到目前爲止建立的,但我得到一些錯誤時我嘗試使用它:
<?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
給每個元素保持不變的標籤,如條件,付款接受,即使他們沒有在原文件中的相應值?
XSLT是一種聲明性語言,針對XML轉換進行了優化。如果你需要爲你的任務需要一個樣式表,那麼你應該更準確地描述它:你希望保持什麼樣的元素不被輸入?如果您在詢問如何調用某些XSLT處理器,那麼您將需要提供更多的環境上下文:至少包含XSLT處理器,至少包含語言等。 – 2011-02-09 18:24:57
當您說'語言'是指XML還是RSS 2.0?如果我使用GUI來處理XSLT,是否需要涉及腳本語言?這會使過程更有效率嗎? – fightermagethief 2011-02-10 09:07:31