xslt从字符串创建有序列表

时间:2015-02-27 11:41:54

标签: xml xslt xslt-1.0 xslt-2.0

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

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>

答案 1 :(得分:1)

如果您使用例如

<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