首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XSLT,只获取标记中字符串的一部分

XSLT,只获取标记中字符串的一部分
EN

Stack Overflow用户
提问于 2011-02-13 11:11:15
回答 1查看 617关注 0票数 0

好了,我有一个xslt样式表,它可以完成我现在需要的大部分功能,看起来是这样的:

代码语言: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/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, '&lt;')">
        <xsl:value-of select="substring-before($text, '&lt;')"/>
        <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, '&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:apply-templates/>
  </xsl:template>
</xsl:stylesheet>

这可能是一个巨大的难题,因为我只是从我的xml编辑器的“原始”输出中抓取文本,因为它做了我需要的事情。它是把正确的标签放在正确的地方。然而,现在“条带标签”似乎不起作用了,我试图制作另一个版本的“条带标签”,它将剥离“src=”之后和“>”之前的所有内容,但很明显,“条带标签”将与我想要做的相反。有没有什么东西可以和“脱衣标签”相反呢?然后我就可以把“strip tag”这个词替换成“strip all-except”或者其他任何名称

编辑:

以下是输入xml文件:

代码语言: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>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>&lt;img border=0 width=50 height=70 src=http://ep.y3333333333327706119506618_2144_317652924&gt;</Thumb>
  <Picture>&lt;img border=0 width=600 height=845 src=http://ep.yim3333333st-27706119506618_2144_317019111&gt;</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>&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</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>  

我想要的输出:

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

EN

回答 1

Stack Overflow用户

发布于 2011-02-13 23:14:50

XML不应该使用词汇表,也不应该使用期望将可解析数据作为未解析文本节点的XML使用者

如果这样做,那么您必须面对后果并进行适当的解析,而不是一些容易出错的RegExp或字符串处理。

https://bug98168.bugzilla.mozilla.org/attachment.cgi?id=434081上提供了一个非常basic的XHTML解析器,用于对格式正确的进行编码

因此,您可以解析未解析的数据,然后使用node-set()扩展函数应用第二阶段转换。

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

https://stackoverflow.com/questions/4982208

复制
相关文章

相似问题

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