xsl-list
[Top] [All Lists]

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

2010-09-03 01:58:48
Dear Evan,

By means of which rule will the stylesheet be able to infer that the missing grouping key is 'DDD'?

Maybe the actual problem suggests an obvious rule for generating these column titles?

In any case, whether you generated a sequence of titles in advance or whether you supply it verbatim: Since it's not there in the data, you cannot derive a grouping key out of the data. Instead I suggest the following solution that doesn't use grouping at all (because you reassured us that after each dt, there's always a single dd):

  <xsl:template match="dl">
    <xsl:variable name="context" select="." as="element(dl)" />
    <xsl:variable name="dds" as="xs:string+">
      <!-- The following may be a sequence calculated in advance: -->
      <xsl:for-each select="('AAA', 'BBB', 'CCC', 'DDD', 'EEE')">
        <xsl:sequence
          select="string-join(
                    (: could also use following-sibling::dd[1] here :)
$context/dt[. eq current()]/following-sibling::*[1]/self::dd,
                    ','
                  )" />
      </xsl:for-each>
    </xsl:variable>
    <xsl:sequence select="string-join($dds, '|')" />
  </xsl:template>

Regarding 'following-sibling':

Is it the following-sibling::*[1]/self::dd that you find hard to understand? This is to say: "use the immediately following sibling element but only if it's a dd." We should probably raise an error if it turns out not to be self::dd. In this case, as I wrote in the comment above, you could also select following-sibling::dd[1] which means "use the first of the following dd siblings".

In the original example, we had
        <xsl:sequence
          select="string-join(
                    for $dt in current-group()
                      return $dt/following-sibling::*[1]/self::dd,
                    ','
                  )" />
which just means: construct a sequence in such a way that for each member of the current group [#] use the immediately following dd element and add it to the sequence.

[#] The current group consists of all <dt>AAA</dt> elements in the first group, all <dt>BBB</dt> elements in the second group, and so on.

-Gerrit


On 03.09.2010 02:10, Evan Leibovitch wrote:
Thanks to everyone who helped in my original question

With your assistance, I was able to convert
<dl>
  <dt>AAA</dt>
  <dd>111</dd>
  <dt>BBB</dt>
  <dd>222</dd>
  <dt>BBB</dt>
  <dd>333</dd>
  <dt>BBB</dt>
  <dd>444</dd>
  <dt>CCC</dt>
  <dd>555</dd>
  <dt>CCC</dt>
  <dd>666</dd>
  <dt>EEE</dt>
  <dd>777</dd>
</dt>

into

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

Unfortunately, it was only after that when I found that the supplier
of my data decided to make a variable and arbitrary number of distinct
<dd><dt>  pairs from one record to the other, so I need to massage the
result further before it's suitable to go into the pipe-separated file
that I need.

What I really need is actually
111|222,333,444|555,666||777

(... for the above record, because it had no data under "DDD")

So I think I need to do a two-pass solution,

1) On the first pass, to convert the above example data to
<AAA>111</AAA>
<BBB>222,333,444</BBB>
<CCC>555,666</CCC>

So I *do* need to group by DT but I am able to assume only one<dd>
value following each<dt>

2) On the second pass I extract only those fields that I need (and
give null entries for those that have no supplied data).

The second pass I think I know how to do, but the first pass still
eludes me because of the "following-sibling" magic whose syntax and
context still eludes me at this moment. I'm trying to deconstruct the
examples you've sent me, but it hasn't been easy.

Any help (especially with the first pass) will be greatly appreciated.

- Evan

--~------------------------------------------------------------------
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>
--~--


--
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>