xsl-list
[Top] [All Lists]

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

2010-08-25 17:27:03


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
Geschäftsfü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(_dot_)imsieke(_at_)le-tex(_dot_)de, http://www.le-tex.de

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

Geschäftsführer: Gerrit Imsieke, Svea Jelonek,
Thomas Schmidt, Dr. Reinhard Vöckler

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>