首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对XML使用XSLT样式表并对html进行反格式化

对XML使用XSLT样式表并对html进行反格式化
EN

Stack Overflow用户
提问于 2011-02-12 12:36:02
回答 1查看 1.3K关注 0票数 0

我想创建一个具有RSS 2.0基本元素的数据馈送,如下所示:

代码语言:javascript
复制
<?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>的标记。这个‘标题’字段是html格式的,我想把标题数据拉到‘描述’,但只是显示为文本,并删除所有的html标签。在样式表中这样做会是什么样子呢?

编辑:

传入的XML文件如下所示(只包含一个'item‘元素):

代码语言:javascript
复制
<?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>  

你可以在底部看到我提到的'Caption‘标签。

我以前从来没有使用过xslt,所以我目前只有一个框架,我只能基本上重复所有的数据,即使我使用‘匹配’模板,所有的文本都包含在最终的产品中。我知道我只是不正确地使用了模板,但我可以在任何地方找到这个概念的解释。我对match使用的措辞是:

代码语言:javascript
复制
<xsl:template match="Description"
<title>
<apply-template/>
</title>
</xsl:template>  

目的是在最终的xml rss2.0文档中将'Description‘标记转换为'title’字段

编辑:

好的,我使用的是XSLTPalette,屏幕有3种输出模式:“原始”,“漂亮”,“渲染”结果。当我使用样式表从“标题”节点中剥离元素时,它们在“原始”结果中看起来就像我想要的那样,它只是没有转义字符的文本。在渲染结果中看起来也很好,但我的数据在渲染结果中仍然是非结构化的。它根本不会出现在“漂亮”的输出框中,但我猜“原始”越来越接近我的需求了。下面是一个示例的不完整样式表:

代码语言:javascript
复制
<?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>  

两件事:这有效地剥离了节点的正确部分,但是我如何将这一部分的输出封装到最终文档的一个名为'description‘的标记中,以及我没有做什么事情来只在产品中显示我想要的东西,比如除了我指定的我想要的东西之外什么都不包含。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-02-12 13:26:00

如果看不到作为输入的确切内容,我只能建议您通过这个XSLT模板传递要从中剥离HTML的节点。

代码语言:javascript
复制
<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>节点即可。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4976355

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档