Reputation: 954
I have an XML tekst like:
<div class="col2">
<p>1. wine</p>
<p>2. beer</p>
<p>3. food</p>
</div>
I want to transform that into:
<div class="col2">
<ol>
<li>wine</li>
<li>beer</li>
<li>food</li>
</ol>
</div>
How can I do that in xslt? Specifically: how to strip out the numbers (with a regexpr) ? I now have:
<xsl:template match="div[@class='col2']">
<div>
<xsl:copy-of select="@*"/>
<ol>
<xsl:for-each select="p"> <===== what do I do here?
<li>
<xsl:apply-templates/>
</li>
</xsl:for-each>
</ol>
</div>
</xsl:template>
Upvotes: 2
Views: 85
Reputation: 23637
If you replace <xsl:apply-templates />
for this:
<xsl:value-of select="normalize-space(substring-after(., '.'))"/>
you will remove everything before '.', trim the extra spaces and get the result you want.
For the cases you presented (words without spaces, digits or periods), you could also achieve the same result with:
<xsl:value-of select="translate(., '01234567890. ', '')"/>
But this will fail if your data is different, since it also eliminates any spaces you have in your text (ex: '5. fast food' will become 'fastfood'). It will also eliminate numbers or periods anywhere in the string.
These both work in XSLT 1.0 or XSLT 2.0, but they aren't really using Regexp. Regexp isn't supported in XSLT 1.0.
If you want to do it with regexp (requires XSLT 2.0), then replace <xsl:apply-templates />
for this:
<xsl:value-of select="replace(., '\d+\.\s+', '')"/>
Which means "find one or more digits, followed by a period, followed by one or more spaces, and replace that with nothing".
Upvotes: 3