首页 > 解决方案 > Need to add newly added entry to new column for given xml using xsl

问题描述

Hi I have a requirement where i need to add newly added information of child to a separate column. I have complimented the same but facing some issues. As of now newly added entries are getting added to same date and versioninfo column But I want this information to be added in separate column.

Any help is appropriated.

Thanks

XML :

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<DeviceHistory>
  <Part Name="ERUI_Touch_LEFT">
    <Details>
      <Date>2019-01-28</Date>
      <DeviceInfo>Firmware Version: -1</DeviceInfo>
       <Date>2019-01-29</Date>
      <DeviceInfo>Firmware Version: -2</DeviceInfo>
    </Details>
  </Part>
  <Part Name="ERUI_Touch_RIGHT">
    <Details>
      <Date>2019-01-30</Date>
      <DeviceInfo>Firmware Version: -1</DeviceInfo>
       <Date>2019-01-31</Date>
      <DeviceInfo>Firmware Version: -2</DeviceInfo>
    </Details>
  </Part>
</DeviceHistory>  

xsl:

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0 "
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">


<xsl:template match="Part[@Name]/Details">
  <html>
  <body> 

   <table-header> <xsl:value-of select ="../@Name"/></table-header>
  <table border="1" style="width:14cm">
    <tr bgcolor="lightgray" margin-top="10pt">
      <th>Date</th>
      <td>
      <td><xsl:apply-templates select="Date"/></td>   
      </td>
    </tr>  
    <tr bgcolor="lightgray">
      <th>VersionInfo</th>    
      <td>
       <td><xsl:apply-templates select="DeviceInfo"/></td>
    </td> 
    </tr>   
  </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

标签: c#asp.netxmlxslt

解决方案


试试这个方法?

XSLT 1.0

<xsl:stylesheet version="1.0 "
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/DeviceHistory">
    <html>
        <body> 
            <xsl:apply-templates select="Part"/>
        </body>
    </html>
</xsl:template>     

<xsl:template match="Part">
    <h3>
        <xsl:value-of select="@Name"/>
    </h3>
    <table border="1">
        <tr>
            <th>Date</th>
            <xsl:apply-templates select="Details/Date"/>
        </tr>
        <tr>
            <th>Version Info</th>
            <xsl:apply-templates select="Details/DeviceInfo"/>
        </tr>
    </table>        
</xsl:template>

<xsl:template match="Date | DeviceInfo">
    <td>
        <xsl:value-of select="."/>
    </td>   
</xsl:template>

</xsl:stylesheet>

应用于您的示例 XML,结果将是:

<html><body>
<h3>ERUI_Touch_LEFT</h3>
<table border="1">
<tr>
<th>Date</th>
<td>2019-01-28</td>
<td>2019-01-29</td>
</tr>
<tr>
<th>Version Info</th>
<td>Firmware Version: -1</td>
<td>Firmware Version: -2</td>
</tr>
</table>
<h3>ERUI_Touch_RIGHT</h3>
<table border="1">
<tr>
<th>Date</th>
<td>2019-01-30</td>
<td>2019-01-31</td>
</tr>
<tr>
<th>Version Info</th>
<td>Firmware Version: -1</td>
<td>Firmware Version: -2</td>
</tr>
</table>
</body></html>

渲染为:

在此处输入图像描述


推荐阅读