作为参考,我正在使用Nmap 7.8,并且尝试使用xsl文件格式化输出。在xsl文件中,我调用第二个rules.xml文件进行比较并添加补充信息。这是rules.xml的结构
<?xml version="1.0" encoding="UTF-8"?>
<root>
<Item id="1">
<info>Rule Information</info>
<port>123</port>
<port>321</port>
</Item>
</root>
[当我尝试使用<xsl:value-of select="@portid"/>
打印nmap端口的输出时,它可以工作,但是如果执行if或select / when操作时,它将失败。当我进行静态比较<xsl:if test="port = 321">
时,它可以使用任一端口值。
<xsl:for-each select="ports/port">
<xsl:choose>
<xsl:when test="state/@state = 'open'">
<xsl:variable name="rules" select="document('rules.xml')"/>
<xsl:for-each select="$rules/root/Item">
<xsl:if test="port = @portid">
<xsl:value-of select="info"/>
</xsl:if>
</xsl:for-each>
</xsl:when>
</xsl:choose>
</xsl:for-each>
期望的行为是遍历nmap输出中的所有端口,并在包含匹配端口标签的Item中添加其他信息标签。映射是1个项目到许多端口,因此,我想避免在顶级端口索引rules.xml,以防止重复。
我猜你想做:
<xsl:for-each select="ports/port">
<xsl:choose>
<xsl:when test="state/@state = 'open'">
<xsl:variable name="portid" select="@portid"/>
<xsl:variable name="rules" select="document('rules.xml')"/>
<xsl:for-each select="$rules/root/Item">
<xsl:if test="port = $portid">
<xsl:value-of select="info"/>
</xsl:if>
</xsl:for-each>
</xsl:when>
</xsl:choose>
</xsl:for-each>
或者也许更简洁:
<xsl:variable name="rules" select="document('rules.xml')"/>
<xsl:for-each select="ports/port[state/@state = 'open']">
<xsl:variable name="portid" select="@portid"/>
<xsl:for-each select="$rules/root/Item[port = $portid]">
<xsl:value-of select="info"/>
</xsl:for-each>
</xsl:for-each>
为什么现在要查找具有其子Item
的值等于port
属性父级portid
的值]的Item
。