Elements within elements

J

Jyrki Keisala

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

Jyrki Keisala

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

Gerald Aichholzer

Hi,

Jyrki said:
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
 
A

a_j_moran

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

Jyrki Keisala

That solution certainly looks elegant, and I'll try it out. Thanks for
the tip!

Jyrki
 
W

William Park

Jyrki Keisala said:
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"
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top