Reputation: 175
With the previous post.
here is the link
Again Small Update in input xml the other validation are all same. Here only the chapter (element) is changing instead of chapter i will have numbers
<tutorial>
<lessons>
<lesson>
12000 Bat 20
</lesson>
<lesson>
15000 Pen Ball 10~
</lesson>
<lesson>
14000 Book
</lesson>
<lesson>
note lesson
</lesson>
</lessons>
<lessons1>
<lesson>
24000 Pencil 10
</lesson>
<lesson>
description page
</lesson>
<lesson>
8000 Car Tank 25
</lesson>
</lessons1>
In the previous question we have Chapter was the first node (chapter Bat 20) but here I have 12000 bat 20
Desire output for the above input is
<Geography>
<historical>
<social>
<toc1>
<toc>
<chapter>12000</chapter>
<unit>Bat</unit>
<pages>20</pages>
</toc>
<toc>
<chapter>15000</chapter>
<unit>Pen Ball</unit>
<pages>10</pages>
</toc>
<toc>
<chapter>14000</chapter>
<unit>Book</unit>
<pages>10</pages>
</toc>
<toc>
<sample>
<original>note lesson</original>
</sample>
</toc>
</toc1>
<toc2>
<toc>
<chapter>24000</chapter>
<unit>Pencil</unit>
<pages>10</pages>
</toc>
<toc>
<sample>
<original>description page</original>
</sample>
</toc>
<toc>
<chapter>8000</chapter>
<unit>Car Tank</unit>
<pages>25</pages>
</toc>
</toc2>
</social>
@Dimitre & @Tomalak From next time i will write fully prepared question and definitely i will post it with the solution what i have, now i am started learning little faster(XSLT) with this below output and previous output.
Please guide me here
Thanks in advance Karthic
Upvotes: 1
Views: 108
Reputation: 243449
This transformation:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="tutorial">
<Geography>
<historical>
<social>
<xsl:apply-templates select=
"*[starts-with(name(),'lessons')]"/>
</social>
</historical>
</Geography>
</xsl:template>
<xsl:template match="*[starts-with(name(), 'lessons')]">
<xsl:variable name="vPos" select="position()"/>
<xsl:element name="toc{$vPos}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match=
"lesson[substring-before(normalize-space(), ' ')
castable as xs:integer
]">
<xsl:variable name="vNorm" select=
"translate(normalize-space(), '~', '')"/>
<xsl:variable name="vAtUnit" select=
"substring-after($vNorm, ' ')"/>
<xsl:variable name="vUnit" select=
"replace($vAtUnit, '([^0123456789]+)(\d*)', '$1')"/>
<xsl:variable name="vLastPart" as="xs:string" select=
"substring-after($vAtUnit, $vUnit)"/>
<xsl:variable name="vNum"
select="concat($vLastPart, '10'[not($vLastPart)])"/>
<toc>
<chapter>
<xsl:value-of select="substring-before($vNorm, ' ')"/>
</chapter>
<unit><xsl:value-of select="normalize-space($vUnit)"/></unit>
<pages><xsl:value-of select="$vNum"/></pages>
</toc>
</xsl:template>
<xsl:template match="lesson">
<toc>
<sample>
<original><xsl:value-of select="normalize-space()"/></original>
</sample>
</toc>
</xsl:template>
</xsl:stylesheet>
when applied on the provided XML document:
<tutorial>
<lessons>
<lesson>
12000 Bat 20
</lesson>
<lesson>
15000 Pen Ball 10~
</lesson>
<lesson>
14000 Book
</lesson>
<lesson>
note lesson
</lesson>
</lessons>
<lessons1>
<lesson>
24000 Pencil 10
</lesson>
<lesson>
description page
</lesson>
<lesson>
8000 Car Tank 25
</lesson>
</lessons1>
</tutorial>
produces the wanted, correct result:
<Geography>
<historical>
<social>
<toc1>
<toc>
<chapter>12000</chapter>
<unit>Bat</unit>
<pages>20</pages>
</toc>
<toc>
<chapter>15000</chapter>
<unit>Pen Ball</unit>
<pages>10</pages>
</toc>
<toc>
<chapter>14000</chapter>
<unit>Book</unit>
<pages>10</pages>
</toc>
<toc>
<sample>
<original>note lesson</original>
</sample>
</toc>
</toc1>
<toc2>
<toc>
<chapter>24000</chapter>
<unit>Pencil</unit>
<pages>10</pages>
</toc>
<toc>
<sample>
<original>description page</original>
</sample>
</toc>
<toc>
<chapter>8000</chapter>
<unit>Car Tank</unit>
<pages>25</pages>
</toc>
</toc2>
</social>
</historical>
</Geography>
Upvotes: 1