XML to HTML table with XSL. Could someone help me please?

M

mslyman

Hi,
I could do with some help. I have this XML.

<region>
<region_code>567</region_code>
<store>
<store_code>345</store_code>
<dept>
<dept_code>32</dept_code>
</dept>
<dept>
<dept_code>33</dept_code>
</dept>
</store>
<store>
<store_code>7</store_code>
<dept>
<dept_code>9</dept_code>
</dept>
<dept>
<dept_code>8</dept_code>
</dept>
</store>
<store>
<!--- more depts -->
</store>
</region>
<region>
<region_code>776</region_code>
<!--- more stores -->
</region>

I need this output to html as follows:
<table>
<tr>
<td title="region">567</td>
<td title="store">345</td>
<td title="dept">34</td>
</tr>
<tr>
<td></td>
<td></td>
<td title="dept">33</td>
</tr>
<tr>
<td></td>
<td title="store">7</td>
<td title="dept">8</td>
</tr>
<tr>
<td></td>
<td></td>
<td title="dept">9</td>
</tr>
<tr>
<td title="region">776</td>
<!-- etc -->
</tr>
<!-- etc -->
</table>

I have tried to achieve this with my limited amount of xsl knowledge of
<xsl:for-each >, nested <xsl:for-each > and <xsl:if
test="position()=1"> and i get close ( by using <xsl:for-each
select="dept"> ) but it's not right. I guessing I have to
apply-templates/recursion but not sure how.
Please if anyone could help that would be great.

Thanks,
Mark
 
A

A. Bolmarcich

Hi,
I could do with some help. I have this XML.

<region>
<region_code>567</region_code>
<store>
<store_code>345</store_code>
<dept>
<dept_code>32</dept_code>
</dept>
<dept>
<dept_code>33</dept_code>
</dept>
</store>
<store>
<store_code>7</store_code>
<dept>
<dept_code>9</dept_code>
</dept>
<dept>
<dept_code>8</dept_code>
</dept>
</store>
<store>
<!--- more depts -->
</store>
</region>
<region>
<region_code>776</region_code>
<!--- more stores -->
</region>

I need this output to html as follows:
<table>
<tr>
<td title="region">567</td>
<td title="store">345</td>
<td title="dept">34</td>
</tr>
<tr>
<td></td>
<td></td>
<td title="dept">33</td>
</tr>
<tr>
<td></td>
<td title="store">7</td>
<td title="dept">8</td>
</tr>
<tr>
<td></td>
<td></td>
<td title="dept">9</td>
</tr>
<tr>
<td title="region">776</td>
<!-- etc -->
</tr>
<!-- etc -->
</table>

I have tried to achieve this with my limited amount of xsl knowledge of
<xsl:for-each >, nested <xsl:for-each > and <xsl:if
test="position()=1"> and i get close ( by using <xsl:for-each
select="dept"> ) but it's not right. I guessing I have to
apply-templates/recursion but not sure how.
Please if anyone could help that would be great.

It would help if you posted what you tried that was "close" but "not
right".

Having nested for-each loops on region, store, and dept and generating
a table row in the dept loop should work. To determine whether to
output a <td title="region"> element or an empty <td> element you need
to test whether the dept position is 1 and the store position is 1.
In the store loop you can set a variable to the value of position() and
test that variable in the dept loop.

In you example xml with region=567 and store=7, dept=9 was before
dept=8, but in the output the corresponding table row for dept=9 was
after the table row for dept=8. Was this intentional, that is, is
the order of table rows in the output not the same as the order in the
xml document?
 
S

Stephen Slyman

Hi,

Many thanks for reply.
Firstly order of nodes is not important for my example.

Like you said, I did nested for-each and generated the table row in the
dept loop however once inside "store" for-each I was repeating
output the region_code within the region. This is something like I'm
doing ** Note title attribute on <td> in my original post was to make
the out more readable *** :

<xsp:template match="region">
<xsp:for-each select="store">
<xsp:for-each select="dept">
<tr>
<xsp:choose>
<xsp:when test="position()=1">
<td> <xsp:value select="../../region_code"/></td>
<td> <xsp:value select="../store_code"/></td>
<td> <xsp:value select="dept_code"/></td>
</xsp:when>
<xsp:eek:therwise>
<td> </td>
<td> </td>
<td> <xsp:value select="dept_code"/></td>

</xsp:eek:therwise>

</xsp:choose>
</tr>
</xsp:for-each>
</xsp:fior-each>
</xsp:template>
However this produced following:
<tr>
<td>567</td>
<td>345</td>
<td>34</td>
</tr>
<tr>
<td></td>
<td></td>
<td>33</td>
</tr>
<tr>
<td>567</td> <!--- loop repeats the region code and I dont what that
-->
<td>7</td>
<td>8</td>
</tr>
<tr>
<td></td>
<td></td>
<td>9</td>
</tr>

However having read your post, I think can prevent repeat of region
code output within store, by creating a variable for position() of
region for-each loop. That make sense I think. But it's 1am here and my
bed is calling. Will try tomorrow.

Thanks to you (and everyone) for your posts again,

Regards

mark
 

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,001
Messages
2,570,254
Members
46,850
Latest member
VMRKlaus8

Latest Threads

Top