xml:aid table to HTML

Discussion in 'XML' started by [Jongware], Jul 9, 2007.

  1. [Jongware]

    [Jongware] Guest

    I'm a bit stumped about this request on an Adobe forum. Apparently, exporting a
    table from their flagship InDesign to XML produces the following output for a
    table (only first 2 rows listed; after that it's more of the same):

    <?xml version="1.0" encoding="UTF-8"?>
    < Table xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/" aid:table="table"
    aid:trows="7" aid:tcols="3">
    <Cell aid:table="cell" aid:crows="1" aid:ccols="2">
    <CellHeading>Situacao do Beneficiario</CellHeading>
    </Cell>
    <Cell aid:table="cell" aid:crows="1" aid:ccols="1"
    aid:ccolwidth="98.93438079914081">
    <CellHeading>Taxas de Bonificacao</CellHeading>
    </Cell>

    Cell aid:table="cell" aid:crows="1" aid:ccols="1"
    aid:ccolwidth="78.09186111410146">
    <CellBodyCenter>
    <CharBold>Idade</CharBold>
    </CellBodyCenter>
    </Cell>
    <Cell aid:table="cell" aid:crows="1" aid:ccols="1"
    aid:ccolwidth="78.09186111410146">
    <CellBodyCenter>
    <CharBold>Carreira Contributiva (em anos)</CharBold>
    </CellBodyCenter>
    </Cell>
    <Cell aid:table="cell" aid:crows="1" aid:ccols="1"
    aid:ccolwidth="98.93438079914081">
    <CellBodyCenter>
    <CharBold>Mensal</CharBold>
    </CellBodyCenter>
    </Cell>

    -- and the request of the OP is, how to convert this to HTML. Sounds easy? Not 4
    me. The first row features a colspan (aid:ccols="2"); without this, I could --
    and would -- cheat, but this colspan messes up the <Cell> count. You see the
    problem. Thou Shalt Not Loop or Count in XSLT As There Are No Variables! Anyone
    else see a solution?

    [Jongware]
    [Jongware], Jul 9, 2007
    #1
    1. Advertising

  2. [Jongware] wrote:
    > Thou Shalt Not Loop or Count in XSLT As There Are No Variables!


    General observation: you can often use position(), or last() applied to
    an expression that counts the preceeding instances, as a replacement for
    a loop counter. Try those first -- preferably position(), since it will
    be more efficient.

    See examples in the XSLT FAQ. Research left as an exercise for the student.


    (If all else fails, it is possible to use recursion in place of looping
    -- but that really should be a last resort.)


    --
    () ASCII Ribbon Campaign | Joe Kesselman
    /\ Stamp out HTML e-mail! | System architexture and kinetic poetry
    Joe Kesselman, Jul 10, 2007
    #2
    1. Advertising

  3. [Jongware]

    Pavel Lepin Guest

    [Jongware] <> wrote in
    <4Pwki.3912$%q4.1386@amstwist00>:
    > I'm a bit stumped about this request on an Adobe forum.


    Perhaps you should've sent the OP here instead of serving as
    a middleman.

    > Apparently, exporting a table from their flagship InDesign
    > to XML produces the following output for a table (only
    > first 2 rows listed; after that it's more of the same):
    >
    > <?xml version="1.0" encoding="UTF-8"?>
    > < Table xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/"


    Not well-formed.

    > aid:table="table" aid:trows="7" aid:tcols="3">


    [...]

    > Cell aid:table="cell" aid:crows="1" aid:ccols="1"


    Not well-formed.

    > aid:ccolwidth="78.09186111410146">


    [...]

    > -- and the request of the OP is, how to convert this to
    > HTML. Sounds easy? Not 4 me. The first row features a
    > colspan (aid:ccols="2"); without this, I could -- and
    > would -- cheat, but this colspan messes up the <Cell>
    > count. You see the problem.


    With just colspans--not really. With rowspans--sort of.

    > Thou Shalt Not Loop or Count in XSLT As There Are No
    > Variables!


    There are variables in XSLT. Looping and counting does not
    present any insurmountable problems either.

    > Anyone else see a solution?


    Engaging my gray matter yielded the following in about ten
    minutes:

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/">
    <xsl:eek:utput method="html"/>
    <xsl:key name="row" match="Cell"
    use=
    "
    concat
    (
    generate-id(..),'|',
    floor
    (
    sum(preceding-sibling::Cell/@aid:ccols) div
    ../@aid:tcols
    )
    )
    "/>
    <xsl:template match="/">
    <xsl:element name="html">
    <xsl:element name="head">
    <xsl:element name="title"/>
    </xsl:element>
    <xsl:element name="body">
    <xsl:apply-templates/>
    </xsl:element>
    </xsl:element>
    </xsl:template>
    <xsl:template match="Table">
    <xsl:element name="table">
    <xsl:call-template name="rows">
    <xsl:with-param name="start" select="0"/>
    <xsl:with-param name="end" select="@aid:trows"/>
    </xsl:call-template>
    </xsl:element>
    </xsl:template>
    <xsl:template name="rows">
    <xsl:param name="start"/>
    <xsl:param name="end"/>
    <xsl:choose>
    <xsl:when test="$start = $end">
    <xsl:element name="tr">
    <xsl:apply-templates
    select=
    "
    key('row',concat(generate-id(),'|',$start))
    "/>
    </xsl:element>
    </xsl:when>
    <xsl:eek:therwise>
    <xsl:variable name="mid"
    select="$start + floor(($end - $start) div 2)"/>
    <xsl:call-template name="rows">
    <xsl:with-param name="start" select="$start"/>
    <xsl:with-param name="end" select="$mid"/>
    </xsl:call-template>
    <xsl:call-template name="rows">
    <xsl:with-param name="start" select="$mid + 1"/>
    <xsl:with-param name="end" select="$end"/>
    </xsl:call-template>
    </xsl:eek:therwise>
    </xsl:choose>
    </xsl:template>
    <xsl:template match="Cell[CellHeading]">
    <xsl:element name="th">
    <xsl:apply-templates select="@aid:ccols"/>
    <xsl:apply-templates select="CellHeading/text()"/>
    </xsl:element>
    </xsl:template>
    <xsl:template match="Cell">
    <xsl:element name="td">
    <xsl:apply-templates select="@aid:ccols"/>
    <xsl:apply-templates
    select=".//text()[normalize-space() != '']"/>
    </xsl:element>
    </xsl:template>
    <xsl:template match="@aid:ccols[. = 1]"/>
    <xsl:template match="@aid:ccols">
    <xsl:attribute name="colspan">
    <xsl:value-of select="."/>
    </xsl:attribute>
    </xsl:template>
    </xsl:stylesheet>

    Dealing with rowspans is left as an exercise for the reader.
    Sending polite hate-mail to Adobe asking them to remove
    their neck-mounted brain casings from their own rear-end
    orifices is advised as well.

    --
    ....the pleasure of obedience is pretty thin compared with
    the pleasure of hearing a rotten tomato hit someone in the
    rear end. -- Garrison Keillor
    Pavel Lepin, Jul 10, 2007
    #3
  4. [Jongware]

    [Jongware] Guest

    "Joe Kesselman" <> wrote in message
    news:...
    > General observation: you can often use position(), or last() applied to an
    > expression that counts the preceeding instances, as a replacement for a
    > loop counter. Try those first -- preferably position(), since it will be
    > more efficient.


    That would have been my cheat if all to-be rows had the same number of
    cells. A short synopsis of the (quite unreadable) XML:
    (header)
    <table col='3' row='5'>
    (first row)
    <cell span='2'><cell span='1'>
    (the rest)
    <cell span='1'><cell span='1'> <cell span='1'>

    -- which has to be output in HTML form, grouping cells per row. The first
    row only has 2 cells, as their 'spans' added equals the number of columns,
    while the other rows have 3 cells each.

    > (If all else fails, it is possible to use recursion in place of looping --
    > but that really should be a last resort.)


    The last resort is (always) cheating, and write a custom XSL for this case.
    And another one for different numbers of rows and columns. And yet another
    if the spans are in other places...
    I tried recursion (sort of), propagating a 'current column' semi-variable
    through the Cell template, but that was a very bad idea (as in I couldn't
    insert row starts and ends).

    [Jw]
    [Jongware], Jul 10, 2007
    #4
  5. [Jongware]

    [Jongware] Guest

    "Pavel Lepin" <> wrote in message
    news:f6vmc0$fk4$...
    >
    > [Jongware] <> wrote in
    > <4Pwki.3912$%q4.1386@amstwist00>:
    >> I'm a bit stumped about this request on an Adobe forum.

    >
    > Perhaps you should've sent the OP here instead of serving as
    > a middleman.


    I'll mention this ng ;-)

    > Engaging my gray matter yielded the following in about ten
    > minutes:


    [brilliant, working solution snipped]

    Oof. Just 10 mins?? It'll take me a bit longer than that to understand it.

    > Dealing with rowspans is left as an exercise for the reader.
    > Sending polite hate-mail to Adobe asking them to remove
    > their neck-mounted brain casings from their own rear-end
    > orifices is advised as well.


    Fully agree with that.

    [Jw]
    [Jongware], Jul 10, 2007
    #5
  6. [Jongware]

    Guest

    Thank you Jongware, and
    Pavel Lepin!

    This is a great solution but now I get an XML with some tables and the
    tables has rowspan and colspan too. Can you help me for the transform?
    Or the method like as the previous method?

    <Table xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/"
    aid:table="table" aid:trows="7" aid:tcols="3">
    <Cell aid:table="cell" aid:crows="1" aid:ccols="3">
    <CellHeading>Situação do Beneficiário/Taxas de Bonificação</
    CellHeading>
    </Cell>
    <Cell aid:table="cell" aid:crows="1" aid:ccols="1"
    aid:ccolwidth="78.09186111410146">
    <CellBodyCenter>
    <CharBold>Idade</CharBold>
    </CellBodyCenter>
    </Cell>
    <Cell aid:table="cell" aid:crows="1" aid:ccols="1"
    aid:ccolwidth="103.98750365826466">
    <CellBodyCenter>
    <CharBold>Carreira Contributiva (em anos)</CharBold>
    </CellBodyCenter>
    </Cell>
    <Cell aid:table="cell" aid:crows="1" aid:ccols="1"
    aid:ccolwidth="72.78873825497762">
    <CellBodyCenter>
    <CharBold>Mensal</CharBold>
    </CellBodyCenter>
    </Cell>
    <Cell aid:table="cell" aid:crows="1" aid:ccols="1"
    aid:ccolwidth="78.09186111410146">
    <CellBodyCenter>&lt; 65</CellBodyCenter>
    </Cell>
    <Cell aid:table="cell" aid:crows="1" aid:ccols="1"
    aid:ccolwidth="103.98750365826466">
    <CellBodyCenter>Condições de Acesso à Pensão sem
    Penalização</CellBodyCenter>
    </Cell>
    <Cell aid:table="cell" aid:crows="1" aid:ccols="1"
    aid:ccolwidth="72.78873825497762">
    <CellBodyCenter>0,65%</CellBodyCenter>
    </Cell>
    <Cell aid:table="cell" aid:crows="1" aid:ccols="1"
    aid:ccolwidth="78.09186111410146"></Cell>
    <Cell aid:table="cell" aid:crows="3" aid:ccols="1"
    aid:ccolwidth="103.98750365826466">
    <CellBodyCenter>15 a 24 </CellBodyCenter>
    <CellBodyCenter>25 a 34 </CellBodyCenter>
    <CellBodyCenter>35 a 39</CellBodyCenter>
    </Cell>
    <Cell aid:table="cell" aid:crows="1" aid:ccols="1"
    aid:ccolwidth="72.78873825497762">
    <CellBodyCenter>0.33%</CellBodyCenter>
    </Cell>
    <Cell aid:table="cell" aid:crows="1" aid:ccols="1"
    aid:ccolwidth="78.09186111410146">
    <CellBodyCenter>&gt; 65</CellBodyCenter>
    </Cell>
    <Cell aid:table="cell" aid:crows="1" aid:ccols="1"
    aid:ccolwidth="72.78873825497762">
    <CellBodyCenter>0.65%</CellBodyCenter>
    </Cell>
    <Cell aid:table="cell" aid:crows="1" aid:ccols="1"
    aid:ccolwidth="78.09186111410146"></Cell>
    <Cell aid:table="cell" aid:crows="1" aid:ccols="1"
    aid:ccolwidth="72.78873825497762">
    <CellBodyCenter>1.0%</CellBodyCenter>
    </Cell>
    <Cell aid:table="cell" aid:crows="1" aid:ccols="1"
    aid:ccolwidth="78.09186111410146">
    <CellBodyCenter>&gt; 65</CellBodyCenter>
    </Cell>
    <Cell aid:table="cell" aid:crows="1" aid:ccols="1"
    aid:ccolwidth="103.98750365826466">
    <CellBodyCenter>&gt; 40</CellBodyCenter>
    </Cell>
    <Cell aid:table="cell" aid:crows="1" aid:ccols="1"
    aid:ccolwidth="72.78873825497762">
    <CellBodyCenter>0.5%</CellBodyCenter>
    </Cell>
    </Table>

    Thank you!
    , Jul 17, 2007
    #6
  7. [Jongware]

    Pavel Lepin Guest

    < > wrote in
    <>:

    [colspan/rowspan with no row delimition horrors]

    > This is a great solution but now I get an XML with some
    > tables and the tables has rowspan and colspan too. Can you
    > help me for the transform? Or the method like as the
    > previous method?


    I'm afraid I fail to see an easy, general XSLT1 solution.
    XSLT2 would make things much easier, but it would still be
    a good bit of work even then. The problem is that with
    rowspans, unlike with colspans, the elements should be
    processed out of order to determine the resulting
    positions. Your best option would be either using a
    general-purpose language w/ DOM or SAX parser to transform
    the document into something more manageable, or just
    hacking it in XSLT until something gives (be warned: in my
    estimation this is going to be *quite* exhausting... and
    the end result is going to be ugly). Hiring an expert might
    be an option as well if you can afford that.

    For that matter, there's a number of XSLT experts on the
    group, so one of them might come up with a simple, general
    solution I failed to notice.

    --
    ....the pleasure of obedience is pretty thin compared with
    the pleasure of hearing a rotten tomato hit someone in the
    rear end. -- Garrison Keillor
    Pavel Lepin, Jul 17, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael Attenborough
    Replies:
    22
    Views:
    2,272
    Mike Treseler
    Mar 13, 2006
  2. Alex Hunsley
    Replies:
    2
    Views:
    363
    Steve Holden
    Jun 4, 2004
  3. William Gill
    Replies:
    10
    Views:
    560
    Florian Diesch
    Sep 1, 2005
  4. Replies:
    29
    Views:
    678
    Paddy
    Mar 11, 2007
  5. Logan Lee

    It was just a memory aid when reading C code

    Logan Lee, Dec 24, 2007, in forum: C Programming
    Replies:
    2
    Views:
    272
    Thad Smith
    Dec 25, 2007
Loading...

Share This Page