Altova Mailing List Archives>Archive Index >microsoft.public.xsl Archive Home >Recent entries [Thread Prev] >Thread Next - Re: Structuring flat data Structuring flat dataTo: NULL Date: 3/3/2006 7:27:00 AM Cutting a long story short, I have some files in a rather flat XML
structure which I now want to upgrade to a more sophisticated schema.
The files can have the following structures:
Example 1:
<xml>
<p>blablabla</p>
</xml>
Example 2:
<xml>
<p>Aaaaaa</p>
<break/>
<p>Bbbbbb</p>
</xml>
Example 3:
<xml>
<title>Title 1</title>
<p>Aaaaaa</p>
<break/>
<p>Bbbbbb</p>
<title>Title 2</title>
<p>Cccccc</p>
</xml>
The point being that the files can have various combinations of titles
and breaks, or none at all. I'm hoping I can get a one-pass solution to
achieve the following results:
Result 1:
<xml>
<section>
<p>blablabla</p>
</section>
</xml>
Result 2:
<xml>
<section>
<p>Aaaaaa</p>
</section>
<section>
<p>Bbbbbb</p>
</section>
</xml>
Result 3:
<xml>
<chapter id="1">
<title>Title 1</title>
<section>
<p>Aaaaaa</p>
</section>
<section>
<p>Bbbbbb</p>
</section>
</chapter>
<chapter id="2">
<title>Title 2</title>
<section>
<p>Cccccc</p>
</section>
</chapter>
</xml>
I have already managed to handle the chapters using the following XSL,
adapted from a sample at http://www.dpawson.co.uk/xsl/sect2/N4486.html:
Chapters.xsl:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="xml">
<xsl:copy>
<xsl:apply-templates select="title" mode="walker"/>
</xsl:copy>
</xsl:template>
<xsl:template match="title" mode="walker">
<xsl:variable name="num"><xsl:value-of
select="position()"/></xsl:variable>
<chapter id="chap.{$num}">
<title>
<xsl:value-of select="."/>
</title>
<xsl:apply-templates select="following-sibling::*[1]"
mode="walker"/>
</chapter>
</xsl:template>
<xsl:template match="*" mode="walker">
<xsl:apply-templates select="."/>
<xsl:if test="not(following-sibling::*[1]/self::chapter)">
<xsl:apply-templates select="following-sibling::*[1]"
mode="walker"/>
</xsl:if>
</xsl:template>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
I also managed to write some XSL that placed text between breaks into
sections, but combining both seems a bit more tricky. So I thought "I
know, I'll ask some experts". :)
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
