我有一个API,我目前正在使用SOAP调用,它正在输出一个XML文件。我遇到麻烦的地方是把文件放进桌子里方便阅读。最大的问题是asset.device和相应的asset.os没有包装在任何东西中。
产出大致如下:
<?xml version="1.0" encoding="UTF-8"?>
<DeviceAssetInfoExport>
<asset.device>
<asset.device.id>123</asset.device.id>
...
</asset.device>
<asset.os>
<asset.os.reportedos>abc</asset.os.reportedos>
...
<asset.os>
<asset.device>
<asset.device.id>321</asset.device.id>
...
</asset.device>
<asset.os>
<asset.os.reportedos>cba</asset.os.reportedos>
...
<asset.os>
</DeviceAssetInfoExport>
所需的输出是一个html表,如下所示:
<html>
<body>
<h2>Servers</h2>
<table>
<tr>
<th>Name</th>
<th>Address</th>
<th>OS</th>
</tr>
<tr>
<td>123</td>
<td>home.co</td>
<td>abc</td>
</tr>
</table>
</body>
</html>
我目前的尝试如下:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" >
<xsl:template match="DeviceAssetInfoExport">
<h2>Servers</h2>
<table border="1">
<tr bgcolor="gray">
<th>Name</th>
<th>Address</th>
<th>OS</th>
</tr>
<xsl:apply-templates select="asset.device | asset.os"/>
</table>
</xsl:template>
<xsl:template match="asset.device | asset.os">
<tr>
<td><xsl:value-of select="asset.device.longname"/></td>
<td><xsl:value-of select="asset.device.uri"/></td>
<td><xsl:value-of select="asset.os.reportedos"/> - <xsl:value-of select="asset.os.osarchitecture"/></td>
</tr>
</xsl:template>
</xsl:stylesheet>
不幸的是,我从这里接收到的输出将asset.os.reportedos信息放在表中它自己的行中。这是在正确的第三个位置,它只是不正确的行与设备的信息。
如果有什么我可以做的,使我想要的结果更清楚,请让我知道。
谢谢!
发布于 2015-06-29 16:06:47
看看这能不能让你开始:
输入XML
<DeviceAssetInfoExport>
<asset.device>
<asset.device.id>123</asset.device.id>
</asset.device>
<asset.os>
<asset.os.reportedos>abc</asset.os.reportedos>
</asset.os>
<asset.device>
<asset.device.id>456</asset.device.id>
</asset.device>
<asset.os>
<asset.os.reportedos>def</asset.os.reportedos>
</asset.os>
</DeviceAssetInfoExport>
XSLT
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/DeviceAssetInfoExport">
<html>
<body>
<h2>Servers</h2>
<table border="1">
<tr>
<th>ID</th>
<th>OS</th>
</tr>
<xsl:apply-templates select="asset.device"/>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="asset.device">
<tr>
<td><xsl:value-of select="asset.device.id"/></td>
<td><xsl:value-of select="following-sibling::asset.os[1]/asset.os.reportedos"/></td>
</tr>
</xsl:template>
</xsl:stylesheet>
结果
<html>
<body>
<h2>Servers</h2>
<table border="1">
<tr>
<th>ID</th>
<th>OS</th>
</tr>
<tr>
<td>123</td>
<td>abc</td>
</tr>
<tr>
<td>456</td>
<td>def</td>
</tr>
</table>
</body>
</html>
https://stackoverflow.com/questions/31126981
复制相似问题