2011-02-12 71 views
0

我想創建一個具有RSS 2.0的基本要素的數據饋送,看起來像這樣:XSLT樣式表對XML和去格式化HTML

<?xml version="1.0" encoding="UTF-8" ?> 
<rss version="2.0" xmlns:g="http://base.google.com/ns/1.0"> 
<channel> 
<title>Clot3333333tory</title> 
<description>Clot333333333333333holesale pricing and bulk ordering to consumers and businesses.</description> 
<item> 
<title> 
<description> 
<link> 

我使用的XML文檔的XSLT樣式表填充此模板,遇到的問題是我想在XML文檔中爲已完成的項目中的標記<description>使用名爲<Caption>的標記。這個'Caption'字段格式化爲html,我想將Caption數據拖入'description'中,只是將其顯示爲文本並刪除所有的html標籤。在樣式表中它看起來像什麼?

編輯:
傳入的XML文件看起來像這樣(只包括一個「項目」元素):

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE StoreExport SYSTEM "http://store.yahoo.com/doc/dtd/StoreExport.dtd"> 
<StoreExport> 
<Settings> 
<Published timestamp="1297187196"/> 
<Locale code="C" name="English" encoding="iso-8859-1"/> 
<StoreName>Clo3333333nd.com</StoreName> 
<Currency>USD</Currency> 
<ShipMethods> 
    <ShipMethod>Ground</ShipMethod> 
    <ShipMethod>Two Day Delivery</ShipMethod> 
    <ShipMethod>One Day Delivery</ShipMethod> 
    <ShipMethod>3 Day</ShipMethod> 

</ShipMethods> 
<PayMethods> 
    <PayMethod>American Express</PayMethod> 
    <PayMethod>Discover</PayMethod> 
    <PayMethod>MasterCard</PayMethod> 
    <PayMethod>Visa</PayMethod> 
    <PayMethod>Diner's Club</PayMethod> 
</PayMethods> 
</Settings> 
<Products> 
<Product Id="agfasu"> 
    <Code>3616a</Code> 
    <Description>Ageless Fashion Suit</Description> 
    <Url>http://www.clo333333nd.com/agfasu.html</Url> 
    <Thumb>&lt;img border=0 width=50 height=70 src=http://ep.yimg.33333333333st-27703333333618_2144_317652924&gt;</Thumb> 
    <Picture>&lt;img border=0 width=600 height=845 src=http://ep.yimg.com/33333333-2770333333333317019111&gt;</Picture> 

    <Orderable>YES</Orderable> 
    <Taxable>YES</Taxable> 
    <Pricing> 
    <BasePrice>178.00</BasePrice> 
    <LocalizedBasePrice>178.00</LocalizedBasePrice> 
    <OrigPrice>299.99</OrigPrice> 

    <LocalizedOrigPrice>299.99</LocalizedOrigPrice> 
    <SalePrice>178.00</SalePrice> 
    <LocalizedSalePrice>178.00</LocalizedSalePrice> 
    </Pricing> 
    <Path> 
    <ProductRef Id="wochsu" Url="http://www.cl333333333333hsu.html">Womens Church Suits</ProductRef> 
    <ProductRef Id="2454" Url="http://www.clo333333333nd.com/2454.html">Aussie Austine Spring/Summer 2011</ProductRef> 

    </Path> 
    <Availability>Usually ships the next business day.</Availability> 
    <Caption>&lt;head&gt; &lt;meta content="en-us" http-equiv="Content-Language"&gt; &lt;style type="text/css"&gt; .style3 { font-family: arial, helvetica; font-size: medium; font-weight: bold; } .style4 { font-size: small; } &lt;/style&gt; &lt;/head&gt; &lt;p&gt;&lt;strong&gt;Wholesale Women&amp;#39;s Church Suits&amp;nbsp; - 3 Piece Suit - Jacket/Vest/Long Skirt&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Aussie Austine Spring/Summer 2011&lt;/strong&gt;&lt;/p&gt; &lt;p class="style4"&gt;&lt;strong&gt;Wholesale Clothing Distributors - Church Attire, Gospel Fashion, Mother of Bride Style at a Wholesale Price. For all your fancy, classic, elegant parties and or occasions. Our suits come in a variety of styles to accommodate your fashion taste. Suits for all women, moms, grandma&amp;#39;s, daughters and aunts. That classic look that will bring attention. From stylish skirts, pants, jackets and vests. Two piece suits, available hats and purses. Brand Name Wholesale Ladies Apparel. &lt;/strong&gt;&lt;/p&gt; &lt;p&gt;3&lt;strong&gt; Piece Suit - Jacket/Vest/Long Skirt&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Sizes&amp;nbsp; 8 10 12 14 16 18 20&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Color&amp;nbsp; Khaki Red&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;Available Accessories: Hat and Purse&amp;nbsp; &lt;/p&gt; &lt;font face="arial, helvetica" size="4"&gt; &lt;p align="left"&gt;Orders of 12 and 24 Items are Mixed Colors and Sizes. For more information Please call Us @ 323333300&lt;/p&gt; &lt;p align="left"&gt;Please feel free to call if you would like wholesale pricing for larger orders&amp;nbsp;&lt;/font&gt;&lt;/p&gt; &lt;font face="arial, helvetica" size="2"&gt; &lt;p align="left"&gt;&lt;b&gt;&lt;font size="4"&gt;Please call or E-mail &lt;a href="mailto:Sal333333sland.com"&gt;[email protected]&lt;/a&gt; If you are interested in purchasing larger quantities&lt;/font&gt;&lt;/b&gt;&lt;/p&gt; &lt;/font&gt; &lt;p align="left" class="style3"&gt;&amp;nbsp;&lt;/p&gt; &lt;p align="left"&gt;&amp;nbsp;&lt;/p&gt; &lt;p align="left"&gt;&amp;nbsp;&lt;/p&gt; &lt;p align="left"&gt;`&lt;/p&gt;</Caption> 

,你可以看到「標題」標籤,在我的底部所提及。
我從來沒有使用過xslt,所以我目前只有一個骨架,我只能基本上重複所有的數據,即使我使用'匹配'模板,所有文本都包含在最終產品中。我知道我只是錯誤地使用了模板,但我可以在任何地方找到這個概念。我使用匹配的寫法是:

<xsl:template match="Description" 
<title> 
<apply-template/> 
</title> 
</xsl:template> 

與轉向「說明」標籤爲「標題」字段中的最後XML RSS2.0文件的意圖

編輯:
好,我使用XSLTPalette,屏幕有3種輸出模式:'原始','漂亮','渲染'結果。當我使用樣式表時,我現在已經去掉了'Caption'節點中的元素,它們看起來像我想要的'原始'結果,它只是沒有所有轉義字符的文本。在呈現的結果中也顯示得很好,但是我的數據在渲染結果中仍然是非結構化的。它不會出現在'漂亮'的輸出框中,但'生'越來越接近我所需要的,我猜。這裏是一個例子一個不完整的樣式表:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
version="1.0"> 
    <xsl:template match="//Product/Caption"> 
    <xsl:param name="text"/> 
    <xsl:choose> 
     <xsl:when test="contains($text, '&lt;')"> 
      <xsl:value-of select="substring-before($text, '&lt;')"/> 
      <xsl:call-template name="strip-tags"> 
        <xsl:with-param name="text" select="substring-after($text, '&gt;')"/> 
      </xsl:call-template> 
     </xsl:when> 
     <xsl:otherwise> 
      <xsl:value-of select="$text"/> 
     </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 
</xsl:stylesheet> 

兩件事情:這有效地去掉節點的正確部分,但如何將我也概括了本節的輸出到一個名爲「說明」的最後文件標籤,以及我沒有做的只有我想要的東西出現在產品中,就像除了我指定的我想要的東西之外什麼也沒有包含。

+0

顯示傳入的XML示例和當前使用的XSLT,謝謝。 – ocodo 2011-02-12 05:23:07

+0

您能否提供真正的XML文檔?現在「」元素的上下文只是一個長字符串 - 沒有HTML標記,因爲它被轉義並平鋪爲普通文本。我想你是這樣做的,試圖獲得HTML顯示... – 2011-02-12 05:50:54

回答

0

沒有看到你輸入的內容,我只能建議你通過這個XSLT模板傳遞你想要去掉HTML的節點。

<xsl:template name="strip-tags"> 
    <xsl:param name="text"/> 
    <xsl:choose> 
     <xsl:when test="contains($text, '&lt;')"> 
      <xsl:value-of select="substring-before($text, '&lt;')"/> 
      <xsl:call-template name="strip-tags"> 
        <xsl:with-param name="text" select="substring-after($text, '&gt;')"/> 
      </xsl:call-template> 
     </xsl:when> 
     <xsl:otherwise> 
      <xsl:value-of select="$text"/> 
     </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 

就包括它在你現有的XSLT,將其應用到相關<Caption>節點。