English 中文(简体)
XSLT,只获取标记中字符串的一部分
原标题:XSLT, grab just a portion of a string within a tag
  • 时间:2011-02-13 03:11:14
  •  标签:
  • xml
  • xslt

好吧,我有一个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,  &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编辑器的原始输出中获取文本,因为它可以满足我的需要。它把正确的标签放在正确的地方。然而,现在strip标签似乎不起作用了,我尝试制作另一个版本的strip标签,它将剥离src=之后和>;之前的所有内容;但很明显,脱衣舞标签与我想做的相反。有什么东西与脱衣舞标签相反吗?然后我可以把strip-tag这个词替换为strip-all except或其他名称

EDIT:
here is the input xml file:

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

我想要的输出:

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

有些标签不需要从来源填充,但总是相同的,例如接受付款、条件和产品类型

问题回答

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

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

https://bug98168.bugzilla.mozilla.org/attachment.cgi?id=434081

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





相关问题
how to represent it in dtd?

I have two element action and guid. guid is a required field when action is add. but when action is del it will not appear in file. How to represent this in dtd ?

.Net application configuration add xml-data

I need to add xml-content to my application configuration file. Is there a way to add it directly to the appSettings section or do I need to implement a configSection? Is it possible to add the xml ...

XStream serializing collections

I have a class structure that I would like to serialize with Xstream. The root class contains a collection of other objects (of varying types). I would like to only serialize part of the objects that ...

MS Word splits words in its XML format

I have a Word 2003 document saved as a XML in WordProcessingML format. It contains few placeholders which will be dynamically replaced by an appropriate content. But, the problem is that Word ...

Merging an XML file with a list of changes

I have two XML files that are generated by another application I have no control over. The first is a settings file, and the second is a list of changes that should be applied to the first. Main ...

How do I check if a node has no siblings?

I have a org.w3c.dom.Node object. I would like to see if it has any other siblings. Here s what I have tried: Node sibling = node.getNextSibling(); if(sibling == null) return true; else ...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

热门标签