好了,我有一个xslt样式表,它可以完成我现在需要的大部分功能,看起来是这样的:
<?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/Description">
<title>
<xsl:apply-templates/>
</title>
</xsl:template>
<xsl:template match="//Product/Picture">
<link>
<xsl:apply-templates/>
</link>
</xsl:template>
<xsl:template match="//Product/Caption">
<description>
<xsl:apply-templates/>
</description>
</xsl:template>
<xsl:template match="Picture">
<xsl:param name="text"/>
<xsl:choose>
<xsl:when test="contains($text, '<')">
<xsl:value-of select="substring-before($text, '<')"/>
<xsl:call-template name="strip-tags">
<xsl:with-param name="text" select="substring-after($text, 'src=')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="Caption">
<xsl:param name="text"/>
<xsl:choose>
<xsl:when test="contains($text, '<')">
<xsl:value-of select="substring-before($text, '<')"/>
<xsl:call-template name="strip-tags">
<xsl:with-param name="text" select="substring-after($text,'>')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>这可能是一个巨大的难题,因为我只是从我的xml编辑器的“原始”输出中抓取文本,因为它做了我需要的事情。它是把正确的标签放在正确的地方。然而,现在“条带标签”似乎不起作用了,我试图制作另一个版本的“条带标签”,它将剥离“src=”之后和“>”之前的所有内容,但很明显,“条带标签”将与我想要做的相反。有没有什么东西可以和“脱衣标签”相反呢?然后我就可以把“strip tag”这个词替换成“strip all-except”或者其他任何名称
编辑:
以下是输入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>Cl33333</StoreName>
<Currency>USD</Currency>
<ShipMethods>
<ShipMethod></ShipMethod>
</ShipMethods>
<PayMethods>
</PayMethods>
</Settings>
<Products>
<Product Id="agfasu">
<Code>3616a</Code>
<Description>Ageless Fashion Suit</Description>
<Url>http://www.cl333333333d.com/agfasu.html</Url>
<Thumb><img border=0 width=50 height=70 src=http://ep.y3333333333327706119506618_2144_317652924></Thumb>
<Picture><img border=0 width=600 height=845 src=http://ep.yim3333333st-27706119506618_2144_317019111></Picture>
<Orderable>YES</Orderable>
<Taxable>YES</Taxable>
<Pricing>
<BasePrice>178.00</BasePrice>
</Pricing>
<Path>333333333333333om/wochsu.html">Womens Church Suits</ProductRef>
<ProductRef Id="2454" Url="http://www.cl33333333454.html">Aussie Austine Spring/Summer 2011</ProductRef>
</Path>
<Availability>Usually ships the next business day.</Availability>
<Caption><head> <meta content="en-us" http-equiv="Content-Language"> <style type="text/css"> .style3 { font-family: arial, helvetica; font-size: medium; font-weight: bold; } .style4 { font-size: small; } </style> </head> <p><strong>Wholesale Women&#39;s</Caption>
<OptionLists>
<OptionList name="Size">
<OptionValue>8</OptionValue>
</OptionList>
<OptionList name="Colors">
<OptionValue>Red</OptionValue>
</OptionList>
<OptionList name="Accessories">
<OptionValue>Suit</OptionValue>
</OptionList>
</OptionLists>
</Product> 我想要的输出:
<item>
<title>
<![CDATA['DescriptionTag]]>
</title>
<description>
<![CDATA[CaptionTagStrippedofEscapedCharacters]]>
</description>
<link>'UrlTag'</link>
<g:condition>new</g:condition>
<g:price>'BasePriceTag'</g:price>
<g:product_type>Clothing, Accessories</g:product_type>
<g:image_link>'PictureTagFrom 'src=' to '>' </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> 有些标签不需要从来源填充,但始终是相同的,例如'payment accepted','condition‘和'product type’
发布于 2011-02-13 23:14:50
XML不应该使用词汇表,也不应该使用期望将可解析数据作为未解析文本节点的XML使用者
如果这样做,那么您必须面对后果并进行适当的解析,而不是一些容易出错的RegExp或字符串处理。
https://bug98168.bugzilla.mozilla.org/attachment.cgi?id=434081上提供了一个非常basic的XHTML解析器,用于对格式正确的进行编码
因此,您可以解析未解析的数据,然后使用node-set()扩展函数应用第二阶段转换。
https://stackoverflow.com/questions/4982208
复制相似问题