Elements within elements

Discussion in 'XML' started by Jyrki Keisala, Jun 8, 2005.

  1. Hi,

    I am trying to transform an XML file into a HTML table with XSLT. The
    structure of my XML file is roughly this:

    <profile>

    <command name="..." phrase="...">
    <key extended="..." pause="... repeat="..." duration="..."/>
    ...
    <key extended="..." pause="... repeat="..." duration="..."/>
    </command>

    <command name="..." phrase="...">
    <key extended="..." pause="... repeat="..." duration="..."/>
    ...
    <key extended="..." pause="... repeat="..." duration="..."/>
    </command>

    ...

    </profile>

    So, for every "command" element, there is one to many "key" elements. I
    want to translate this structure to a simple HTML table that looks like

    Command Key
    Name Phrase Extended Pause Repeat Duration
    -------------------------------------------------------------------
    cmd1 cmd1 key1 key1 key1 key1
    key2 key2 key2 key2
    cmd2 cmd2 key1 key1 key1 key1
    cmd3 cmd3 key1 key1 key1 key1
    key2 key2 key2 key2
    key3 key3 key3 key3
    ....
    --------------------------------------------------------------------

    (Tried to use TABs but am not sure whether the relative font will screw
    up the structure of the table in my posting.)

    Now handling command elements with only one key element inside is very
    easy, as everything is put into a single row. My problem is how to handle
    multiple key elements inside a command element, such that the data in the
    subsequent key elements begins in its own row, from column 3.

    There probably is a very obvious solution to this...
     
    Jyrki Keisala, Jun 8, 2005
    #1
    1. Advertising

  2. Sorry, the structure of the intended table really WAS screwed up. Lets test
    if this looks better:

    -------------------------------------------------------------------
    Command Key
    Name Phrase Ext Pause Rpt Duration
    -------------------------------------------------------------------
    cmd1 cmd1 key1 key1 key1 key1
    key2 key2 key2 key2
    cmd2 cmd2 key1 key1 key1 key1
    cmd3 cmd3 key1 key1 key1 key1
    key2 key2 key2 key2
    key3 key3 key3 key3
    ...
    --------------------------------------------------------------------
     
    Jyrki Keisala, Jun 8, 2005
    #2
    1. Advertising

  3. Hi,

    Jyrki Keisala wrote:
    >
    > I am trying to transform an XML file into a HTML table with XSLT. The
    > structure of my XML file is roughly this:
    >
    > <profile>
    >
    > <command name="..." phrase="...">
    > <key extended="..." pause="... repeat="..." duration="..."/>
    > ...
    > <key extended="..." pause="... repeat="..." duration="..."/>
    > </command>
    >
    > <command name="..." phrase="...">
    > <key extended="..." pause="... repeat="..." duration="..."/>
    > ...
    > <key extended="..." pause="... repeat="..." duration="..."/>
    > </command>
    >
    > ...
    >
    > </profile>
    >
    > [...]
    >
    >
    > Now handling command elements with only one key element inside is very
    > easy, as everything is put into a single row. My problem is how to handle
    > multiple key elements inside a command element, such that the data in the
    > subsequent key elements begins in its own row, from column 3.
    >


    I think you could use position() for this, e.g.

    <xsl:template match="key">
    <xsl:if test="position()==1">
    <!-- code for 1st row -->
    </xsl:if>
    <xsl:if test="position()!=1">
    <!-- code for all other rows -->
    </xsl:if>
    </xsl:template>

    HTH,
    Gerald
     
    Gerald Aichholzer, Jun 8, 2005
    #3
  4. Jyrki Keisala

    Guest

    Given that you stated already that each command element must have at
    least one key associated with it the best approach might be to
    associate a template with the "command" element that also processes the
    first "key" child line - further "key" element processing should be
    done in a seperate template invoked with the appropriate predicate
    (this avoids the expense of position checking on each invocation of the
    key template). in sum:

    <xsl:template match="command">
    <tr>
    <td>...cmd elements...</td>
    <td>...markup with ref to first key <xsl:value-of select="key[1]"
    /></td>
    </tr>
    <!-- continue processing of subsequent keys -->
    <xsl:apply-templates select="key[position() > 1]" />
    <xsl:template>

    <xsl:template match="key">
    <tr>
    <!-- insert some spacer elements for correct alignment ;) -->
    <td>&nbsp;</td>
    <!-- now process the key attributes + values -->
    <td><xsl:value-of select="..etc..." /></td>
    </tr>
    <xsl:template>

    hth
    ajm.


    Jyrki Keisala schrieb:
    > Sorry, the structure of the intended table really WAS screwed up. Lets test
    > if this looks better:
    >
    > -------------------------------------------------------------------
    > Command Key
    > Name Phrase Ext Pause Rpt Duration
    > -------------------------------------------------------------------
    > cmd1 cmd1 key1 key1 key1 key1
    > key2 key2 key2 key2
    > cmd2 cmd2 key1 key1 key1 key1
    > cmd3 cmd3 key1 key1 key1 key1
    > key2 key2 key2 key2
    > key3 key3 key3 key3
    > ...
    > --------------------------------------------------------------------
     
    , Jun 8, 2005
    #4
  5. That solution certainly looks elegant, and I'll try it out. Thanks for
    the tip!

    Jyrki


    > Given that you stated already that each command element must have at
    > least one key associated with it the best approach might be to
    > associate a template with the "command" element that also processes
    > the first "key" child line - further "key" element processing should
    > be done in a seperate template invoked with the appropriate predicate
    > (this avoids the expense of position checking on each invocation of
    > the key template). in sum:
    >
    > <xsl:template match="command">
    > <tr>
    > <td>...cmd elements...</td>
    > <td>...markup with ref to first key <xsl:value-of select="key[1]"
    > /></td>
    > </tr>
    > <!-- continue processing of subsequent keys -->
    > <xsl:apply-templates select="key[position() > 1]" />
    > <xsl:template>
    >
    > <xsl:template match="key">
    > <tr>
    > <!-- insert some spacer elements for correct alignment ;) -->
    > <td>&nbsp;</td>
    > <!-- now process the key attributes + values -->
    > <td><xsl:value-of select="..etc..." /></td>
    > </tr>
    > <xsl:template>
    >
    > hth
    > ajm.
     
    Jyrki Keisala, Jun 14, 2005
    #5
  6. Jyrki Keisala

    William Park Guest

    Jyrki Keisala <> wrote:
    > Hi,
    >
    > I am trying to transform an XML file into a HTML table with XSLT. The
    > structure of my XML file is roughly this:
    >
    > <profile>
    >
    > <command name="..." phrase="...">
    > <key extended="..." pause="... repeat="..." duration="..."/>
    > ...
    > <key extended="..." pause="... repeat="..." duration="..."/>
    > </command>
    >
    > <command name="..." phrase="...">
    > <key extended="..." pause="... repeat="..." duration="..."/>
    > ...
    > <key extended="..." pause="... repeat="..." duration="..."/>
    > </command>
    >
    > ...
    >
    > </profile>


    > Command Key
    > Name Phrase Extended Pause Repeat Duration
    > --------------------------------------------------------------------
    > cmd1 cmd1 key1 key1 key1 key1
    > key2 key2 key2 key2
    > cmd2 cmd2 key1 key1 key1 key1
    > cmd3 cmd3 key1 key1 key1 key1
    > key2 key2 key2 key2
    > key3 key3 key3 key3
    > ...
    > -------------------------------------------------------------------


    I would use XML parser included with an extended Bash shell.

    start() {
    case $1 in
    command)
    declare "${@:2}"
    ;;
    key)
    declare "${@:2}"
    echo '<tr>'
    printf '<td>%s</td>' "$name" "$phrase" "$extended" "$pause" "$repeat" "$duration"
    echo '</tr>'
    ;;
    esac
    }
    slurp=`< file.xml`
    xml -s start "$slurp"

    --
    William Park <>, Toronto, Canada
    ThinFlash: Linux thin-client on USB key (flash) drive
    http://home.eol.ca/~parkw/thinflash.html
     
    William Park, Jun 15, 2005
    #6
    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. KatB
    Replies:
    0
    Views:
    467
  2. mark4asp
    Replies:
    2
    Views:
    2,270
    Harry Haller
    Nov 7, 2006
  3. Elmo Watson
    Replies:
    1
    Views:
    622
    Eliyahu Goldin
    May 8, 2007
  4. Ultrus
    Replies:
    3
    Views:
    412
    Stefan Behnel
    Jul 9, 2007
  5. Abby Lee
    Replies:
    5
    Views:
    473
    Abby Lee
    Aug 2, 2004
Loading...

Share This Page