【问题标题】:xslt create ordered list from stringxslt 从字符串创建有序列表
【发布时间】:2015-02-27 11:41:53
【问题描述】:

在xml中有一个字符串

<anons>
      1. first list item. 2. second list item. 3. third list item.
</anons>

是否可以像这样创建有序列表:

<ol>
  <li>first list item.</li>
  <li>second list item.</li>
  <li>third list item.</li>
</ol>

【问题讨论】:

  • 请不要同时使用xslt-1.0xslt-2.0 标记问​​题 - 它们相互排斥。

标签: xml xslt xslt-1.0 xslt-2.0


【解决方案1】:

这是可能的,但它必须是一个字符串操作,这意味着如果文本比这更复杂,在某些情况下它可能是不可能的。

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
exclude-result-prefixes="xs xd"
version="2.0">

<xsl:template match="/">
    <xsl:apply-templates/>
</xsl:template>

<xsl:template match="anons">
    <ol>
        <xsl:for-each select="tokenize (./string(), '\d+\.')[normalize-space()]">
                <li><xsl:value-of select="normalize-space(.)"/></li>
        </xsl:for-each>
    </ol>
</xsl:template>

</xsl:stylesheet>

【讨论】:

    【解决方案2】:

    如果你使用例如

    <xsl:template match="anons">
      <ol>
        <xsl:for-each select="tokenize(., '[0-9]+\.')[normalize-space()]">
          <li>
            <xsl:value-of select="normalize-space()"/>
          </li>
        </xsl:for-each>
      </ol>
    </xsl:template>
    

    你应该得到你想要的。另一种选择是使用analyze-string

    【讨论】:

      猜你喜欢
      • 2011-04-20
      • 2016-02-13
      • 2016-12-02
      • 1970-01-01
      • 1970-01-01
      • 2014-02-10
      • 2023-04-06
      • 2020-04-21
      • 2013-08-11
      相关资源
      最近更新 更多