【问题标题】:Separate text of an element in xslt (from xml)xslt 中元素的分隔文本(来自 xml)
【发布时间】:2018-01-19 12:36:33
【问题描述】:

xml

<product>
  <name>Smartphone Samsung S6</name>
</product>
<product>
  <name>Smartphone Samsung S8</name>
</product>

html

<h1>Smartphone<small>Samsung S6</small></h1>
<h1>Smartphone<small>Samsung S8</small></h1>

如何在 xslt 中做到这一点?输出:html

<xsl:for-each select="product">
<h1><xsl:value-of select="name"/></h1>
</xsl:for-each>

【问题讨论】:

  • 好吧,在small 元素中标记该文本的标准是什么,是单词Samsung 后跟另一个单词还是Smartphone 的任何内容?您使用哪个版本的 XSLT?
  • 三星后面是 S6 或 S8。在智能手机之前什么都没有。 xsl 2.0
  • 您的产品名称是否总是采用相同的“类别制造型号”格式?

标签: html xml xslt dtd


【解决方案1】:

试试这个:

<xsl:for-each select="product">
    <h1>
        <xsl:analyze-string select="name" regex="^(Smartphone) (Samsung S[0-9])">
            <xsl:matching-substring>
                <xsl:value-of select="regex-group(1)"/>
                <small>
                    <xsl:value-of select="regex-group(2)"/>
                </small>
            </xsl:matching-substring>
            <xsl:non-matching-substring>
                <xsl:value-of select="."/>
            </xsl:non-matching-substring>
        </xsl:analyze-string>
    </h1>
</xsl:for-each>

请参阅http://xsltransform.net/jxN8NpA 上的转换

【讨论】:

  • 如果我有另一款名为 HP 647j 笔记本电脑的产品?我做不到
  • ^(Smartphone) (Samsung S[0-9])更改为^(Smartphone|Laptop) (Samsung S[0-9]|HP [0-9]+[a-z]?)
猜你喜欢
  • 2014-03-11
  • 2014-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-11
相关资源
最近更新 更多