user3655953
user3655953

Reputation: 3

XSLT: Group XML elements by type and output as HTML tables

I have XML containing elements describing the name,value,sfid,feildsection of objects. Objects are grouped by type feildsection as providersection and devidetype . my xml given blow

 <RequestFields>
<RequestField>
  <Name>Financial Change Type</Name>
  <Value>F/R Change (IRU to CRU)</Value>
  <sfid>Type</sfid>
  <feildsection>devicetype</feildsection>
</RequestField>
<RequestField>
  <Name>Service Number</Name>
  <Value>111-111-1111</Value>
  <sfid>00N30000000lTGp</sfid>
  <feildsection>serviceNo</feildsection>
</RequestField>
<RequestField>
  <Name>Confirm Service Number</Name>
  <Value>111-111-1111</Value>
</RequestField>
<RequestField>
  <Name>Device User</Name>
  <Value>Manager</Value>
  <sfid>00N30000000m6c7</sfid>
  <feildsection>ServiceNo</feildsection>
</RequestField>
<RequestField>
  <Name>Provider</Name>
  <Value>ATT</Value>
  <sfid>Provider__c</sfid>
  <feildsection>Deviceotype</feildsection>
</RequestField>
<RequestField>
  <Name>ExistingDeviceType</Name>
  <Value>BlackBerry</Value>
  <sfid>Current_Equipment_Type__c</sfid>
  <feildsection>devicetype</feildsection>
</RequestField>
<RequestField>
  <Name>Current Account Number</Name>
  <Value>I don't know this information</Value>
  <sfid>00N30000001BRgi</sfid>

</RequestField>
<RequestField hide="1">
  <Name>Current Password</Name>
  <Value>I don't know this information</Value>
  <sfid>Active_Profile_Password__c</sfid>
</RequestField>
<RequestField>
  <Name>Current Billing Name</Name>
  <Value>I don't know this information</Value>
</RequestField>
<RequestField>
  <Name>Current Billing Address</Name>
  <Value>I don'tknowthisinformation</Value>
</RequestField>
<RequestField>
  <Name>Manufacturer</Name>
  <Value></Value>
  <sfid>00N30000001BRm8</sfid>
  <feildsection>devicetype</feildsection>
</RequestField>
<RequestField>
  <Name>Model</Name>
  <Value></Value>
  <sfid>Activate_New_Equipment_Model__c</sfid>
</RequestField>
<RequestField>
  <Name>DeviceType</Name>
  <Value>BlackBerry</Value>
  <sfid>Equipment_Type__c</sfid>
</RequestField>

I would like to group xml nodes grouping by feildsection type.i want to my output which nodes are have fieldsection value same that will come in one section group ,if node doesn't have feildsection then that feilds are displayed in last.pls help to find solution

<table>
<tr>
<td colspan=2>Devicetype</td>
</tr>
<tr>
<td>Financial Change Type</td>
<td>F/R Change (IRU to CRU)<td>
</tr>
<tr>
<td>Provider</td>
<td>ATT<td>
</tr>
<tr>
<td>ExistingDeviceType</td>
<td>BlackBerry<td>
</tr>
<tr>
<td>Current Account Number</td>
<td>I don't know this information<td>
</tr>
<tr>
<td>Manufacturer</td>
<td></td>
</tr>
<tr>
<td colspan=2>serviceNo</td>
</tr>
<tr>
<td>Service Number</td>
<td>111-111-1111</td>
</tr>
<tr>
<td>Device User</td>
<td>Manager</td>
</tr>
<tr>
<td>Confirm Service Number</td>
<td>111-111-1111</td>
<td>Current Account Number</td>
<td>I don't know this information</td>
<td>Current Password</td>
<td>I don't know this information</td>
<td>Current Billing Name</td>
<td>I don't know this information</td>
<td>Current Billing Address</td>
<td>I don'tknowthisinformation</td>
<td>Model</td>
<td></td>`
</tr>
</table>

Upvotes: 0

Views: 337

Answers (1)

Eric S
Eric S

Reputation: 462

I'm not sure I got all what you want in your output, but if you're able to use XSLT 2.0, something like below should help you :

<xsl:template match="/RequestFields">
   <table>
   <!-- This is the grouping command avalaible in XSLT 2.0. Two things : 
        First you're only selecting field with 'feildsection' in their childs.
        Second, you use the group-by attribute to define youre grouping key (here the value of the feildsection child)-->
   <xsl:for-each-group select="*[feildsection]" group-by="feildsection">
          <!-- Here you could output the value of the grouping key with the below function 
          (current-grouping-key() only avalaible in a for-each-group context)-->
          <tr><td colspan='2'><xsl:value-of select="current-grouping-key()"/></td></tr>
          <xsl:apply-templates/>
   </xsl:for-each-group>
   <!-- At the end, you're dealing with element without feildsection -->
   <xsl:apply-templates select="*[not(feildsection)]"/>
   </table>
<xsl:template/>

<!-- here is the template for the RequestField element.-->
<xsl:template match="RequestField">
    <tr>
        <td><xsl:value-of select="Name"/></td>
        <td><xsl:value-of select="Value"/></td>
    </tr>
</xsl:template>

Upvotes: 1

Related Questions