Reputation: 1147
I have an html ordered list with differnent types e.g
<ol type=a>
<li>This is list item a</li>
<li>this is list item b</li>
</ol>
<ol type=i>
<li>This is list item 1</li>
<li>this is list item 2</li>
</ol>
I want to know if we can convert them into xml markup mentioned below.
<xml>
<orderlist>
<li>
<num>a</num>
<text>This is list item a</text>
</li>
<li>
<num>b</num>
<text>This is list item b</text>
</li>
</orderlist>
</xml>
<xml>
<orderlist>
<li>
<num>i</num>
<text>This is list item 1</text>
</li>
<li>
<num>ii</num>
<text>This is list item 2</text>
</li>
</orderlist>
</xml>
I can put match template for LI and easily get the text but how to get the numbering based on type attribute... ie a,b,c .... i, ii, iii, iv and so on... Any help will be appreciated.
Upvotes: 2
Views: 4192
Reputation: 52858
If your HTML is well-formed XML, yes:
XML Input
<foo>
<ol type="a">
<li>This is list item a</li>
<li>this is list item b</li>
</ol>
<ol type="i">
<li>This is list item 1</li>
<li>this is list item 2</li>
</ol>
</foo>
XSLT 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="ol">
<orderedlist>
<xsl:apply-templates select="node()|@*"/>
</orderedlist>
</xsl:template>
<xsl:template match="li">
<li>
<num><xsl:number format="{../@type}"/></num>
<text><xsl:apply-templates/></text>
</li>
</xsl:template>
</xsl:stylesheet>
XML Output
<foo>
<orderedlist>
<li>
<num>a</num>
<text>This is list item a</text>
</li>
<li>
<num>b</num>
<text>this is list item b</text>
</li>
</orderedlist>
<orderedlist>
<li>
<num>i</num>
<text>This is list item 1</text>
</li>
<li>
<num>ii</num>
<text>this is list item 2</text>
</li>
</orderedlist>
</foo>
Upvotes: 2