Re: [xsl] Converting a Batch File to XML

Subject: Re: [xsl] Converting a Batch File to XML
From: "Garvin Franco" <garvin_franco@xxxxxxxxxxx>
Date: Wed, 28 Jul 2004 07:42:10 -0400
Hi Jeni,

This is an excellent base to begin my quest.

On it, right away.

I will let you know how it goes.


Thanks again for the head start....



Regards Garvin




From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Reply-To: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
To: "Garvin Franco" <garvin_franco@xxxxxxxxxxx>
CC: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] Converting a Batch File to XML
Date: Mon, 26 Jul 2004 14:33:24 +0100

Hi Garvin,

> I have a flat file that I need to convert to an XML structure. Now I
> can do this if the records in the file had the same structure, this
> is fairly simple. But my source fixed length file has mutliple
> records that need to be associated to a single node on the target
> xml. For example, my file is structured as follows...

As you're doing this with XSLT 1.0, then you may as well parse the
string directly rather than going through an intermediate XML form
(since doing the grouping on the XML will need the same kind of code
as parsing the string, and the string will be a lot less
memory-intensive than the XML).

Assuming that you have some way of getting your string into the
stylesheet (through a parameter), you need recursive templates that
work through the string using substring-before() and substring-after()
functions. Something like:

<xsl:template name="parse-data">
  <xsl:param name="string" />
  <xsl:if test="$string">
    <someRoot>
      <header>
        <headerTag>
          <xsl:value-of select="substring-after(
                                  substring-before($string,
                                                   '
'),
                                  'H-')" />
        </headerTag>
      </header>
      <xsl:call-template name="parse-orders">
        <xsl:with-param name="string"
          select="substring-after($string, '
')" />
      </xsl:call-template>
    </someRoot>
  </xsl:if>
</xsl:template>

<xsl:template name="parse-orders">
  <xsl:param name="string" />
  <xsl:if test="$string">
    <record>
      <order>
        <xsl:call-template name="parse-items">
          <xsl:with-param name="string"
            select="substring-before($string, 'S-')" />
        </xsl:call-template>
        <summary>
          <summaryTag>
            <xsl:variable name="summary"
              select="substring-after($string, 'S-')" />
            <xsl:choose>
              <xsl:when test="contains($summary, '
')">
<xsl:value-of select="substring-before($summary, '
')" />
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="$summary" />
              </xsl:otherwise>
            </xsl:choose>
          </summaryTag>
        </summary>
      </order>
    </record>
    <xsl:call-template name="parse-orders">
      <xsl:with-param name="string"
        select="substring-after(substring-after($string, '
S-'),
'
')" />
    </xsl:call-template>
  </xsl:if>
</xsl:template>

<xsl:template name="parse-items">
  <xsl:param name="string" />
  <xsl:if test="$string">
    <item>
      <itemTag>
        <xsl:variable name="item"
          select="substring-after($string, 'I-')" />
        <xsl:choose>
          <xsl:when test="contains($item, '
')">
<xsl:value-of select="substring-before($item, '
')" />
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="$item" />
          </xsl:otherwise>
        </xsl:choose>
      </itemTag>
    </item>
    <xsl:call-template name="parse-items">
      <xsl:with-param name="string"
        select="substring-after($string, '
')" />
    </xsl:call-template>
  </xsl:if>
</xsl:template>

Call the parse-data template as in:

  <xsl:call-template name="parse-data">
    <xsl:with-param name="string" select="..." />
  </xsl:call-template>

I hope that gets you part way there...

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


_________________________________________________________________
Take advantage of powerful junk e-mail filters built on patented Microsoft. SmartScreen Technology. http://join.msn.com/?pgmarket=en-ca&page=byoa/prem&xAPID=1994&DI=1034&SU=http://hotmail.com/enca&HL=Market_MSNIS_Taglines Start enjoying all the benefits of MSN. Premium right now and get the first two months FREE*.


Current Thread