I am still new to XSLT. My last question was trying to get similar IDs and Names grouped together. I got a great answer that did help me group by ID but now it's grabbing all the sibling nodes that match the ID and bunching them together in one row. Looking at my following examples, should I try working with variables or do I have my select/conditions set up incorrectly?
My XML Code:
<?xml version="1.0"?>
<Scroll0 class="table table-hover" label="Guest List">
<Row>
<Field2 label="Guest">
<![CDATA[12345678 - Matthews, Jacob]]>
</Field2>
<Field3 label="Access Type">
<![CDATA[Gym]]>
</Field3>
<Field4 label="Description">
<![CDATA[Access to all gym facilities.]]>
</Field4>
</Row>
<Row>
<Field2 label="Guest">
<![CDATA[12345678 - Matthews, Jacob]]>
</Field2>
<Field3 label="Access Type">
<![CDATA[Mathematics]]>
</Field3>
<Field4 label="Description">
<![CDATA[Access to all math facilities.]]>
</Field4>
</Row>
<Row>
<Field2 label="Guest">
<![CDATA[87654321 - Smith, Maggie]]>
</Field2>
<Field3 label="Access Type">
<![CDATA[Gym]]>
</Field3>
<Field4 label="Description">
<![CDATA[Access to all gym facilities.]]>
</Field4>
</Row>
<Row>
<Field2 label="Guest">
<![CDATA[87654321 - Smith, Maggie]]>
</Field2>
<Field3 label="Access Type">
<![CDATA[Mathematics]]>
</Field3>
<Field4 label="Description">
<![CDATA[Access to all math facilities.]]>
</Field4>
</Row>
</Scroll0>
My XSL Code:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:key name="Match-by-ID" match="Row" use="Field2" />
<xsl:template match="/Scroll0">
<div>
<caption>
<strong><xsl:value-of select="@label" /></strong>
</caption>
<xsl:choose>
<xsl:when test="(count(Row) > 0)">
<xsl:for-each select="Row[count(. | key('Match-by-ID', Field2)[1])=1]" >
<table class="table table-condensed table-hover">
<xsl:if test="Field2/@label != ''">
<div>
<strong><xsl:value-of select="Field2" /></strong>
</div>
</xsl:if>
<tr>
<th class="" id="Field3"><xsl:value-of select="Field3/@label"/></th>
<th class="" id="Field4"><xsl:value-of select="Field4/@label"/></th>
</tr>
<tr>
<td class="" headers="Field3">
<xsl:for-each select="key('Match-by-ID', Field2)/Field3">
<xsl:value-of select="."/>
</xsl:for-each>
</td>
<td class="" headers="Field4">
<xsl:for-each select="key('Match-by-ID', Field2)/Field4">
<xsl:value-of select="."/>
</xsl:for-each>
</td>
</tr>
</table>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<div class="alert alert-info">No Guest Listed</div>
</xsl:otherwise>
</xsl:choose>
</div>
</xsl:template>
</xsl:stylesheet>
My Output HTML:
<?xml version="1.0"?>
<div>
<caption><strong>Guest List</strong></caption>
<table class="table table-condensed table-hover">
<div>
<strong>12345678 - Matthews, Jacob</strong>
</div>
<tr>
<th class="" id="Field3">Access Type</th>
<th class="" id="Field4">Description</th>
</tr>
<tr>
<td class="" headers="Field3"> Gym Mathematics </td>
<td class="" headers="Field4"> Access to all gym facilities. Access to all math facilities. </td>
</tr>
</table>
<table class="table table-condensed table-hover">
<div>
<strong>87654321 - Smith, Maggie</strong>
</div>
<tr>
<th class="" id="Field3">Access Type</th>
<th class="" id="Field4">Description</th>
</tr>
<tr>
<td class="" headers="Field3"> Gym Mathematics </td>
<td class="" headers="Field4"> Access to all gym facilities. Access to all math facilities. </td>
</tr>
</table>
</div>
What I am looking for with the final HTML Output?:
<?xml version="1.0"?>
<div>
<caption><strong>Guest List</strong></caption>
<table class="table table-condensed table-hover">
<div>
<strong>12345678 - Matthews, Jacob</strong>
</div>
<tr>
<th class="" id="Field3">Access Type</th>
<th class="" id="Field4">Description</th>
</tr>
<tr>
<td class="" headers="Field3"> Gym </td>
<td class="" headers="Field4"> Access to all gym facilities. </td>
</tr>
<tr>
<td class="" headers="Field3"> Mathematics </td>
<td class="" headers="Field4"> Access to all math facilities. </td>
</tr>
</table>
<table class="table table-condensed table-hover">
<div>
<strong>87654321 - Smith, Maggie</strong>
</div>
<tr>
<th class="" id="Field3">Access Type</th>
<th class="" id="Field4">Description</th>
</tr>
<tr>
<td class="" headers="Field3"> Gym </td>
<td class="" headers="Field4"> Access to all gym facilities. </td>
</tr>
<tr>
<td class="" headers="Field3"> Mathematics </td>
<td class="" headers="Field4"> Access to all math facilities. </td>
</tr>
</table>
</div>
If you want to have a row for each item in a group, you must create a row for each item in a group. Change this part:
<tr>
<td class="" headers="Field3">
<xsl:for-each select="key('Match-by-ID', Field2)/Field3">
<xsl:value-of select="."/>
</xsl:for-each>
</td>
<td class="" headers="Field4">
<xsl:for-each select="key('Match-by-ID', Field2)/Field4">
<xsl:value-of select="."/>
</xsl:for-each>
</td>
</tr>
to:
<xsl:for-each select="key('Match-by-ID', Field2)">
<tr>
<td class="" headers="Field3">
<xsl:value-of select="Field3"/>
</td>
<td class="" headers="Field4">
<xsl:value-of select="Field4"/>
</td>
</tr>
</xsl:for-each>