我有一个xlst查询,它会从xml原始数据中返回我想要的所有正确信息,但是因为它有多个对象,每个结果都有自己的头,但我只想在开始时使用1,而不是其他人可以有人帮我分组。这是xlst查询:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="CMDBSet">
<table border="1">
<tr bgcolor="">
<th>display label</th>
<th>root class</th>
<th>host servertype</th>
<th>host osreleasetype</th>
<th>host osinstalltype</th>
</tr>
<xsl:for-each select="CMDBObject/Properties">
<tr>
<td><xsl:value-of select="display_label"/></td>
<td><xsl:value-of select="root_class"/></td>
<td><xsl:value-of select="host_servertype"/></td>
<td><xsl:value-of select="host_osrelease"/></td>
<td><xsl:value-of select="host_osinstalltype"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
和xml原始数据的片段:
<?xml version="1.0" encoding="UTF-8"?>
<CMDBTopology>
<Objects>
<CMDBSet>
<name>80df0b42de8f31ac4cb7a30d325ff0c1</name>
<CMDBObject>
<identifier type="nt">80df0b42de8f31ac4cb7a30d325ff0c1</identifier>
<Properties>
<root_class type="STRING">nt</root_class>
<host_servertype type="STRING"></host_servertype>
<host_osrelease type="STRING"></host_osrelease>
<display_label type="STRING">pharsm-s3004</display_label>
<host_osinstalltype type="STRING"></host_osinstalltype>
</Properties>
</CMDBObject>
</CMDBSet>
<CMDBSet>
<name>8305e305bdaf613b50deab1da4ae469c</name>
<CMDBObject>
<identifier type="nt">8305e305bdaf613b50deab1da4ae469c</identifier>
<Properties>
<root_class type="STRING">nt</root_class>
<host_servertype type="STRING"></host_servertype>
<host_osrelease type="STRING">3790</host_osrelease>
<display_label type="STRING">phchbs-si442</display_label>
<host_osinstalltype type="STRING">Server Standard Edition</host_osinstalltype>
</Properties>
</CMDBObject>
我偶然发现了一些可能的方法,比如使用像
这样的分组<xsl:key name="keyrootclass" match="diplaylabel" use="rootclass" />
<xsl:key name="keyhostservertype" match="displaylabel" use="hostservertype" />
<xsl:key name="keyhostosreleasetype" match="displaylabel" use="hostosreleasetype" />
<xsl:key name="keyhostservertype" match="displaylabel" use="hostservertype" />
但我仍然坚持如何继续,好像我添加这些行它不会输出任何东西,但没有这些键名行我得到输出但每个cmdbobject有多个标题
答案 0 :(得分:0)
听起来你只需要这样做:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<table border="1">
<tr bgcolor="">
<th>display label</th>
<th>root class</th>
<th>host servertype</th>
<th>host osreleasetype</th>
<th>host osinstalltype</th>
</tr>
<xsl:apply-templates
select="CMDBTopology/Objects/CMDBSet/CMDBObject/Properties" />
</table>
</body>
</html>
</xsl:template>
<xsl:template match="Properties">
<tr>
<td>
<xsl:value-of select="display_label"/>
</td>
<td>
<xsl:value-of select="root_class"/>
</td>
<td>
<xsl:value-of select="host_servertype"/>
</td>
<td>
<xsl:value-of select="host_osrelease"/>
</td>
<td>
<xsl:value-of select="host_osinstalltype"/>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
在提供的输入上运行时(添加缺失的标记后),会产生以下输出:
<html>
<body>
<table border="1">
<tr bgcolor="">
<th>display label</th>
<th>root class</th>
<th>host servertype</th>
<th>host osreleasetype</th>
<th>host osinstalltype</th>
</tr>
<tr>
<td>pharsm-s3004</td>
<td>nt</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>phchbs-si442</td>
<td>nt</td>
<td></td>
<td>3790</td>
<td>Server Standard Edition</td>
</tr>
</table>
</body>
</html>