Below is the input XML (Little Big) sorry for the bigger input XML and as well as output xml
chapter unit
If you just want to remove the "~", use translate(xxx, '~', '')
Beyond that, I'm afraid you're not making your requirements clear. For example, I don't understand this clause:
if there is no value in by default it has to displayed unit 10
This transformation:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="vNames" select="'chapter', 'unit', 'pages'"/>
<xsl:template match="lessons">
<Geography>
<historical>
<social>
<toc1>
<xsl:apply-templates select="lesson"/>
</toc1>
<xsl:apply-templates select="lessons1"/>
</social>
</historical>
</Geography>
</xsl:template>
<xsl:template match="lesson[starts-with(normalize-space(), 'chapter')]">
<xsl:variable name="vNorm" select=
"translate(normalize-space(), '~', '')"/>
<xsl:variable name="vAtNumber" select=
"substring-after($vNorm, 'chapter unit')"/>
<xsl:variable name="vNum" select=
"if(matches($vAtNumber, '^\s*\d+'))
then replace($vAtNumber, '(^\s*(\d+)).*$', '$2')
else '10'
"/>
<xsl:analyze-string select="."
regex="(chapter\s+)(unit\s*)(((\d*~?)\s+)?page)">
<xsl:matching-substring>
<toc>
<chapter>chapter</chapter>
<unit>unit <xsl:value-of select="$vNum"/></unit>
<pages>page</pages>
</toc>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:template>
<xsl:template match="lesson">
<sample>
<original><xsl:value-of select="normalize-space()"/></original>
</sample>
</xsl:template>
<xsl:template match="lessons1">
<toc2>
<xsl:apply-templates/>
</toc2>
</xsl:template>
</xsl:stylesheet>
when applied on the provided XML document:
<tutorial>
<lessons>
<lesson>
chapter unit 1 page
</lesson>
<lesson>
chapter unit 10~ page
</lesson>
<lesson>
chapter unit page
</lesson>
<lesson>
note lesson
</lesson>
<lessons1>
<lesson>
chapter unit 1 page
</lesson>
<lesson>
description page
</lesson>
<lesson>
chapter unit page
</lesson>
</lessons1>
</lessons>
</tutorial>
produces the wanted, correct result:
<Geography>
<historical>
<social>
<toc1>
<toc>
<chapter>chapter</chapter>
<unit>unit 1</unit>
<pages>page</pages>
</toc>
<toc>
<chapter>chapter</chapter>
<unit>unit 10</unit>
<pages>page</pages>
</toc>
<toc>
<chapter>chapter</chapter>
<unit>unit 10</unit>
<pages>page</pages>
</toc>
<sample>
<original>note lesson</original>
</sample>
</toc1>
<toc2>
<toc>
<chapter>chapter</chapter>
<unit>unit 1</unit>
<pages>page</pages>
</toc>
<sample>
<original>description page</original>
</sample>
<toc>
<chapter>chapter</chapter>
<unit>unit 10</unit>
<pages>page</pages>
</toc>
</toc2>
</social>
</historical>
</Geography>