开发者

using XSLT stylesheet on XML and deformatting html

i would like to create a data feed that has the basic elements of RSS 2.0 and looks like so:

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

i am using an XSLT stylesheet on an XML document to populate this template, a problem a have run into is i want to use a tag called <Caption> in the XML document for the tag <description> in the finished project. This 'Caption' field is formatted in html, i would like to pull the Caption data into 'description' but just display it as text and drop all the html tags. What would it look like in the stylesheet to do this?

EDIT:

the incoming XML file looks like so(including only one 'item' element):

<?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;Sales@cloth333333333and.com&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>  

you can see the 'Caption' tag i mentioned at the bottom.

i have never used xslt before, so i currently only have a skeleton that i can only get to just basically repeat all the data, even if i use 'match' templates, all of the text is included in the final product regardless. i know i am just wording the template incorrectly, but i can find this concept explained anywhere. the wording i am using for match is:

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

with the intention of turning the 'Description' tag into a 'title' field in a final xml rss2.0 document

EDIT:

okay, i am using XSLTPalette and the screen has 3 output modes: 'raw','pretty','rendered' results. when i use the stylesheet i have now to strip elements from the 'Caption' nodes, they appear like i want in the 'raw' results, it is just the text without all the escaped chars. also appears fine in the rendered result, but my data is still unstructured in the rendered result. it doesnt appear at all in the 'pretty' output box, but 'raw' is getting closer to what i need, i guess. here is an incomplete stylesheet for an example:

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

two things: this effectively strips the correct section of nodes, but how would i also encapsulate the output of this section into a tag called 'description' for the final document, and what am i not doing to have only the things i want show up in the product, like to include nothing except what i specify i want.


Without seeing exactly what you get as input, I can only suggest that you pass the nodes you want to strip HTML from, through this XSLT template.

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

Just include it in you existing XSLT, applying it to the relevant <Caption> node.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜