how do I parse the following xml

E

enkosi

<chart>
<row label="1">
<col>66</col>
<col>100</col>
</row>
<row label="2">
<col>14</col>
</row>
</chart>

into html looking like this:

<tr>
<td align="right" colspan="66">
</td>
<td align="right" colspan="34">
</td>
</tr>
<tr>
<td align="right" colspan="14">
</td>
</tr>

with xsl

Thanks any help appreciated.
H
 
J

Joe Kesselman

1) You mean "transform", not "parse". (The latter term refers just to
reading the document, not manipulating its contents or generating a new
one.)


2) Since this is a near-trivial question, it sounds like you need an
intro-to-XSLT tutorial. Many good ones exist on the Internet. My
standard recommendation is the documents at http://www.ibm.com/xml, but
I admit I'm biased.

To give you a slightly more specific hint: Start by writing out what you
want to do in detail, in terms of "When you see this, replace it with
that". (Row gets replaced with tr, col with td, etc.) Then write each of
those match-and-replace operations as a template, recusing with
xsl:apply-templates as necessary.
 
E

enkosi

Ok, transform then

I'm OK with the absolute basic, just having trouble with the column
width of the second third etc columns.

If I have one column, I do an
<td>
<xsl:attribute name ="colspan">
<xsl:value-of select ="cc:col"/>
</xsl:attribute>
</td>

can you do something like

<td>
<xsl:choose>

<xsl:when test ="position() > 1">
<xsl:variable name="start" select="cc:col/@id =
position()"/>
<xsl:attribute name ="colspan">
<xsl:value-of select ="cc:col - $start"/>
</xsl:attribute>
</xsl:when>
<xsl:eek:therwise>
<xsl:value-of select ="cc:col" />
</xsl:eek:therwise>

</xsl:choose>
</td>

but then with 3 columns it gets tricky
 
J

Joseph Kesselman

enkosi said:
If I have one column, I do an
<td>
<xsl:attribute name ="colspan">
<xsl:value-of select ="cc:col"/>
</xsl:attribute>
</td>


Set up a template for col rather than trying to do it in the template
for row.

Row becomes table row, then uses apply-template to process its columns.

Col becomes table data, with its value becoming the column width.
 
E

enkosi

Thanks, but it doesn't answer my question.

Joseph said:
Col becomes table data, with its value becoming the column width.

I don't want the value to become the column width, but 100 -
SUM(previous values) to be the column width
 
J

Joseph Kesselman

I don't want the value to become the column width, but 100 -
SUM(previous values) to be the column width

Ah. Thanks for the clarification. In XPath terms, that's
100-sum(preceeding-siblings::col)
 

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

Forum statistics

Threads
474,005
Messages
2,570,264
Members
46,859
Latest member
HeidiAtkin

Latest Threads

Top