Re: [xsl] Converting from <dt><dd> pairs to better XML

Subject: Re: [xsl] Converting from <dt><dd> pairs to better XML
From: "Imsieke, Gerrit, le-tex" <gerrit.imsieke@xxxxxxxxx>
Date: Thu, 26 Aug 2010 00:26:40 +0200
On 25.08.2010 23:56, Evan Leibovitch wrote:

There are variable numbers of each dt/dd combination, but they are
generally kept together by<dt>  value.
Ultimately I'd like to convert this into a pipe-separated-value file
(with implied headers):

111|222,333,444|555,666|....

Dear Evan,


You say you are using xsltproc which is XSLT 1. On the other hand, you teach yourself XSLT 2.0 with Michael Kay's book. Can you switch to an XSLT 2 processor, such as Saxon? Then this will serve as a solution:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  xmlns:xs="http://www.w3.org/2001/XMLSchema";
  version="2.0"  >

<xsl:output method="text" />

  <xsl:template match="dl">
    <xsl:variable name="dds" as="xs:string+">
      <xsl:for-each-group select="dt" group-by=".">
        <xsl:sequence
          select="string-join(
                    for $dt in current-group()
                      return $dt/following-sibling::*[1]/self::dd,
                    ','
                  )">
        </xsl:sequence>
      </xsl:for-each-group>
    </xsl:variable>
    <xsl:sequence select="string-join($dds, '|')" />
  </xsl:template>

</xsl:stylesheet>

This is based on the assumption that each dt is followed by exactly one dd. If there may be multiple dds after each dt, you should group adjacent dds in a first pass.

-Gerrit

--
Gerrit Imsieke
GeschC$ftsfC<hrer / Managing Director
le-tex publishing services GmbH
Weissenfelser Str. 84, 04229 Leipzig, Germany
Phone +49 341 355356 110, Fax +49 341 355356 510
gerrit.imsieke@xxxxxxxxx, http://www.le-tex.de

Registergericht / Commercial Register: Amtsgericht Leipzig
Registernummer / Registration Number: HRB 24930

GeschC$ftsfC<hrer: Gerrit Imsieke, Svea Jelonek,
Thomas Schmidt, Dr. Reinhard VC6ckler

Current Thread