问题
A Little update in my previous question already @Dimetre answered that
Link
Input XML
<tutorial>
<lessons>
<lesson>
chapter Bat 20
</lesson>
<lesson>
chapter Pen Ball 10~
</lesson>
<lesson>
chapter Book
</lesson>
<lesson>
note lesson
</lesson>
<lessons1>
<lesson>
chapter Pencil 10
</lesson>
<lesson>
description page
</lesson>
<lesson>
chapter Car Tank 25
</lesson>
</lessons1>
</lessons>
The Output will be
<Geography>
<historical>
<social>
<toc1>
<toc>
<chapter>chapter</chapter>
<unit>Bat</unit>
<pages>20</pages>
</toc>
<toc>
<chapter>chapter</chapter>
<unit>Pen Ball</unit>
<pages>10</pages>
</toc>
<toc>
<chapter>chapter</chapter>
<unit>Book</unit>
<pages>10</pages>
</toc>
<sample>
<original>note lesson</original>
</sample>
</toc1>
<toc2>
<toc>
<chapter>chapter</chapter>
<unit>Pencil</unit>
<pages>10</pages>
</toc>
<sample>
<original>description page</original>
</sample>
<toc>
<chapter>chapter</chapter>
<unit>Car Tank</unit>
<pages>25</pages>
</toc>
</toc2>
</social>
Here in input XML I have for sample i have written two Lessons (lesson, Lesson1) actually but actually it my have n number of lessons. I think I am asking more but i am learning simultaneously.
Please help me & Guide me
Thanks in advance
Regards Karthic
回答1:
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[starts-with(normalize-space(), 'chapter')]">
<xsl:variable name="vNorm" select=
"translate(normalize-space(), '~', '')"/>
<xsl:variable name="vAtUnit" select=
"substring-after($vNorm, 'chapter')"/>
<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>chapter</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 (corrected not to have lessons1
nested in lessons
):
<tutorial>
<lessons>
<lesson>
chapter Bat 20
</lesson>
<lesson>
chapter Pen Ball 10~
</lesson>
<lesson>
chapter Book
</lesson>
<lesson>
note lesson
</lesson>
</lessons>
<lessons1>
<lesson>
chapter Pencil 10
</lesson>
<lesson>
description page
</lesson>
<lesson>
chapter Car Tank 25
</lesson>
</lessons1>
</tutorial>
produces the wanted, correct result:
<Geography>
<historical>
<social>
<toc1>
<toc>
<chapter>chapter</chapter>
<unit>Bat</unit>
<pages>20</pages>
</toc>
<toc>
<chapter>chapter</chapter>
<unit>Pen Ball</unit>
<pages>10</pages>
</toc>
<toc>
<chapter>chapter</chapter>
<unit>Book</unit>
<pages>10</pages>
</toc>
<toc>
<sample>
<original>note lesson</original>
</sample>
</toc>
</toc1>
<toc2>
<toc>
<chapter>chapter</chapter>
<unit>Pencil</unit>
<pages>10</pages>
</toc>
<toc>
<sample>
<original>description page</original>
</sample>
</toc>
<toc>
<chapter>chapter</chapter>
<unit>Car Tank</unit>
<pages>25</pages>
</toc>
</toc2>
</social>
</historical>
</Geography>
回答2:
Is this your own format of XML? It seems you are limiting your own data (or removing the structure) by putting the lesson number in the lesson element. If you are using XSL to process it, every time you add a new lesson you have to update the xsl to cope with the new types, or make a complicated stylesheet to do it. Instead of that, would it not be better to use attributes, so instead of lesson and lesson1 for chapters you had
<lesson id='1'>
....
</lesson>
<lesson id='2'>
....
</lesson>
then you wouldn't have to change the code if you have 1 lesson or 50000, and you could use a modified version of what dimitre wrote for you
来源:https://stackoverflow.com/questions/11523708/condition-checking-in-xslt