c# - 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>
解决方案
试试这个方法?
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>
渲染为:
推荐阅读
- javascript - 使用 php、ajax、mysql 删除记录时遇到一些问题
- xamarin.forms - SQLite DB 创建后存储在 Desktop 中的位置
- r - 在参考类中定义字段时的有效性测试错误
- azure - Azure 应用程序网关
- asp.net - asp.net C#中的按钮单击事件错误
- dplyr - 如何使用 NA 值为数据框中的所有列创建汇总函数
- javascript - 显示活跃用户图标——如何确定谁不再在线(寻找 NG 或 JS 解决方案)
- hibernate - 如何在 WildFly 13 中使用 Hibernate 4
- react-native - 混合玩笑期待与排毒期待
- php - 如何在循环外显示数组变量