Jump to content

Print XML Table with XSL

Philip F's Photo
Posted Sep 26 2012 12:00 PM
6600 Views

I apologize in advance if this is too simple, but I am not a programmer.

I am trying to display an XML file with XSL, the XML file is a list of groups and their subsequent users (the info displayed below is not everything, as there are 20+ addtl groups). The problem is that various groups have varying number of users. I can write a simple xsl statement to display the first user per group, but I don't know how to include all users.

The XML is:
<?xml-stylesheet type="text/xsl" href="groups.xsl" ?>
<GroupRecord>
<cn>Steering Committee</cn>
<UserList>
<uid>john</uid>
<uid>bob</uid>
</UserList>
</GroupRecord>
<GroupRecord>
<cn>Gentoo</cn>
<UserList>
<uid>mary</uid>
<uid>sam</uid>
<uid>mark</uid>
<uid>jennifer</uid>
</UserList>
</GroupRecord>


The XSL is:
<table width="100%" border="1">
<THEAD>
<TR>
<TD colspan="2" align="center">Users</TD>
</TR>
<TR>
<TD width="50%"><B>Group</B></TD>
<TD width="50%"><B>Users</B></TD>
</TR>
</THEAD>
<TBODY>
<xsl:for-each select="GroupRecord">
<TR>
<TD width="50%"><xsl:value-of select="cn" /></TD>
<TD width="50%"><xsl:value-of select="UserList/uid" /></TD>
</TR>
</xsl:for-each>
</TBODY>
</table>

The XML file will display the groups, and the first user per each group, but do I need to write a for-loop in Javascript to extract the other user data?


Thanks in advance

Tags:
0 Subscribe


5 Replies

+ 1
  mhalverson's Photo
Posted Sep 26 2012 03:23 PM

You can nest a second for-each loop to process all of the uid's. You will have to decide how you want to list them (for example separate TD's).

For example if you want to stay with your two column table and separate them by commas replace <xsl:value-of select="UserList/uid" /> with :


<xsl:for-each select="UserList/uid">
<xsl:sort select="."/>
<xsl:value-of select="."/>
<xsl:if test="not(position()=last())">,<xsl:text> </xsl:text></xsl:if>
</xsl:for-each>


This will loop over all the uid's (the sort puts them in alphabetical order leave that out if you want document order). The test element will add a comma and a space after each unless it is the last one (in the alphabetical order if the sort is present else document order). The text element ensures that the space does not get clobbered by whatever whitespace handling is present (there are other ways to do that). The periods in the select statements refer to the current context node (in this case the uid).
0
  Philip F's Photo
Posted Sep 27 2012 12:24 PM

Thanks a lot for your help!
0
  Ahana56's Photo
Posted Nov 25 2013 04:24 AM

Now many people know this topic. Very attractive. So I came to a published personal opinion, only a person point of view. Again, I recommend you use this station.
Nam Ho Travel Service
0
  JiriSri's Photo
Posted Mar 30 2014 07:20 AM

Studying the Print XML Table with XSL appreciate this very much. Help me out by the I do not think I have a portfolio website ( www.ballpromotion.com/ผลบอลสด ) finished.
0
  MartinJay's Photo
Posted Apr 01 2014 02:54 AM

Please add good good information that would help others in such good way.This post is exactly what I am concerned. we need some more reliable information.
assos butil otelleri