XSLT:按类型分组XML元素,并将输出分组为HTML表

时间:2014-08-01 12:54:52

标签: xml xslt xslt-grouping

我有XML,包含描述对象的名称,值,sfid和feildsection的元素。对象按类型feildsection分组为providersection和devidetype。 我的xml给了打击

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

我想按照feildsection type.i想要输出分组xml节点 哪个节点具有相同的fieldsection值,如果节点没有feildsection那么节点会显示在last.pls中帮助找到解决方案

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

1 个答案:

答案 0 :(得分:1)

我不确定我的输出中是否包含了您想要的所有内容,但如果您能够使用XSLT 2.0,则下面的内容可以为您提供帮助:

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