Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Structuring flat data

From: ariana_paris@-----.--.--
To: 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". :)



transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent