我试图通过这些节点之外的值将xml中的几个节点分组。节点我试图将链接分组到外部对象,这些对象保留了一些值。 这是一个xml的例子:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="testgr.xsl"?>
<root>
<data>
<host ip="10.0.0.1">
<soft>
<name>service1</name>
<vulners>
<vulner id="1"/>
<vulner id="2"/>
<vulner id="3"/>
</vulners>
</soft>
<soft>
<name>service2</name>
<vulners>
<vulner id="1"/>
<vulner id="2"/>
<vulner id="4"/>
</vulners>
</soft>
</host>
<host ip="10.0.0.2">
<soft>
<name>service1</name>
<vulners>
<vulner id="1"/>
<vulner id="2"/>
<vulner id="3"/>
</vulners>
</soft>
<soft>
<name>service3</name>
<vulners>
<vulner id="5"/>
<vulner id="6"/>
<vulner id="7"/>
</vulners>
</soft>
</host>
</data>
<vulners>
<vulner id="1">
<name>vuln1</name>
<how_to_fix>update x1</how_to_fix>
</vulner>
<vulner id="2">
<name>vuln2</name>
<how_to_fix>update x1</how_to_fix>
</vulner>
<vulner id="3">
<name>vuln3</name>
<how_to_fix>update x2</how_to_fix>
</vulner>
<vulner id="4">
<name>vuln4</name>
<how_to_fix>update x3</how_to_fix>
</vulner>
<vulner id="5">
<name>vuln5</name>
<how_to_fix>update x4</how_to_fix>
</vulner>
<vulner id="6">
<name>vuln6</name>
<how_to_fix>update x4</how_to_fix>
</vulner>
<vulner id="7">
<name>vuln7</name>
<how_to_fix>update x4</how_to_fix>
</vulner>
</vulners>
</root>
如您所见,每个/ root / data / host / soft / vulners节点都包含一些漏洞/ @id对象,这是一个指向/ root / vulners / vulner [@id]的链接 我想分组/ root / vulners / vulner [@id] / how_to_fix值 所以期望的结果是:
<html>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<TITLE></TITLE>
</HEAD>
<body>
<table border="1">
<tr bgcolor="#9acd32">
<th>№</th>
<th>name</th>
<th>fix</th>
</tr>
<tr>
<th colspan="3">10.0.0.1,
service: service1
</th>
</tr>
<tr>
<td>1</td>
<td>multiple vulns</td>
<td>update x1</td>
</tr>
<tr>
<td>2</td>
<td>vuln3</td>
<td>update x2</td>
</tr>
<tr>
<th colspan="3">10.0.0.1,
service: service2
</th>
</tr>
<tr>
<td>1</td>
<td>multiple vulns</td>
<td>update x1</td>
</tr>
<tr>
<td>2</td>
<td>vuln4</td>
<td>update x3</td>
</tr>
<tr>
<th colspan="3">10.0.0.2,
service: service1
</th>
</tr>
<tr>
<td>1</td>
<td>multiple vulns</td>
<td>update x1</td>
</tr>
<tr>
<td>2</td>
<td>vuln3</td>
<td>update x2</td>
</tr>
<tr>
<th colspan="3">10.0.0.2,
service: service3
</th>
</tr>
<tr>
<td>1</td>
<td>multiple vulns</td>
<td>update x4</td>
</tr>
</table>
</body>
</html>
我尝试了以下方法,但我没有做对。请问有人帮忙吗?
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<HEAD>
<TITLE></TITLE>
</HEAD>
<body>
<table border="1">
<tr bgcolor="#9acd32">
<th>№</th>
<th>name</th>
<th>fix</th>
</tr>
<xsl:apply-templates select="root/data/host"/>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="root/data/host">
<xsl:variable name="myhost" select="." />
<xsl:for-each select="soft">
<xsl:variable name="myservice" select="." />
<xsl:if test="./vulners/vulner">
<tr>
<th colspan='3'>
<xsl:value-of select="$myhost/@ip"/>,
service: <xsl:value-of select="$myservice/name"/>
</th>
</tr>
</xsl:if>
<xsl:for-each-group select="vulners/vulner" group-by="//root/vulners/vulner[@id=./@id]/how_to_fix">
<xsl:for-each select="current-group()">
<tr>
<xsl:variable name="myId" select="@id" />
<xsl:variable name="myvuln" select="//root/vulners/vulner[@id=$myId]" />
<td><xsl:value-of select="position()"/></td>
<td><xsl:value-of select="$myvuln/name" /></td>
<td><xsl:value-of select="$myvuln/how_to_fix" /></td>
</tr>
</xsl:for-each>
</xsl:for-each-group>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
答案 0 :(得分:1)
我猜你想做点什么:
XSLT 2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:key name="vulner-by-id" match="vulner" use="@id" />
<xsl:template match="/root">
<table border="1">
<tr bgcolor="#9acd32">
<th>No.</th>
<th>name</th>
<th>fix</th>
</tr>
<xsl:apply-templates select="data/host/soft[vulners/vulner]"/>
</table>
</xsl:template>
<xsl:template match="soft">
<tr>
<th colspan='3'>
<xsl:value-of select="../@ip"/>
<xsl:text>, service: </xsl:text>
<xsl:value-of select="name"/>
</th>
</tr>
<xsl:for-each-group select="key('vulner-by-id', vulners/vulner/@id)" group-by="how_to_fix">
<tr>
<td>
<xsl:value-of select="position()"/>
</td>
<td>
<xsl:value-of select="current-group()/name" separator=", " />
</td>
<td><xsl:value-of select="how_to_fix" /></td>
</tr>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
结果(已呈现):