Jump to content

Print XML Table with XSL

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

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" ?>
<cn>Steering Committee</cn>

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

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

0 Subscribe

2 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>

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).
  Philip F's Photo
Posted Sep 27 2012 12:24 PM

Thanks a lot for your help!