sprut
sprut

Reputation: 21

xslt create ordered list from string

Have a string in xml

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

Is it possible to create an ordered list like this:

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

Upvotes: 0

Views: 1061

Answers (2)

Marcin
Marcin

Reputation: 190

It is possible but it would have to be a string operation, meaning it might not be possible in some cases if the text is more complex than this.

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

Upvotes: 1

Martin Honnen
Martin Honnen

Reputation: 167516

If you use e.g.

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

you should get what you want. Another option would be to use analyze-string.

Upvotes: 1

Related Questions