XML xpath?基于属性NOT在列表中过滤xml标记

时间:2012-04-17 15:28:21

标签: xml xpath

尝试根据列表过滤xml标记。

列表示例:filename list.txt

1
2
3
4
5
6个

示例XML

< id = 1 />
< id = 4 />
< id = 11 />
< id = 3 />
< id = 23 />

使用XPath?是否有可能编写一个xpath查询来过滤xml标签,只留下不在list.txt中的内容?

输出
< id = 11 />
< id = 23 />

这是否可以在xpath语句中使用?

1 个答案:

答案 0 :(得分:0)

感谢所有提供反馈和建议的人。我已经找到了自己的问题。请提供有关我的解决方案的反馈,看看它是否可以做得更好。感谢..

我的问题与Nessus有关。它的报道很糟糕。我能够查看.nessus文件,ver2并将我需要的标签中的信息拼凑在一起。我会在这里为其他可以使用类似报告的Nessus用户提供。

我需要一种方法来过滤掉报告中不重要的插件信息。我决定最好的方法是使用xslt过滤插件,我也用它来将xml文件格式化为电子表格应用程序的有用表格格式。我决定使用单独的xml文件来存储包含不需要的Nessus插件的标签。 xml文件的结构如下:

<?xml version="1.0" encoding="ISO-8859-1"?>
<exclude>
  <nessusPlugin>19506</nessusPlugin>
  <nessusPlugin>45590</nessusPlugin>
</exclude>

我遵循了关于查看'文档'功能的建议,并最终提出了这个xslt:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:variable name="excludedplugins" select="document('nessusExcludePluginList.xml')/exclude/nessusPlugin"/>

<xsl:template match="/">
  <html>
  <body>
    <h2>
      Nessus Report, Targets: <xsl:value-of select="count(NessusClientData_v2/Report/ReportHost)"/> 
    </h2>
    <br/>High Risk Count: <xsl:value-of select="count(NessusClientData_v2/Report/ReportHost/ReportItem[risk_factor='High'])"/>
    <br/>Medium Risk Count: <xsl:value-of select="count(NessusClientData_v2/Report/ReportHost/ReportItem[risk_factor='Medium'])"/>
    <br/>Low Risk Count: <xsl:value-of select="count(NessusClientData_v2/Report/ReportHost/ReportItem[risk_factor='Low'])"/>
    <br/>Irrelavant Risk (Severity &lt; 2) Count: <xsl:value-of select="count(NessusClientData_v2/Report/ReportHost/ReportItem[@severity&lt;2])"/>
    <table border="1">    
      <tr bgcolor="#9acd32">
        <th>DNS Name</th>
        <th>NetBios Name</th>
        <th>IP Address</th>
        <th>MAC Address</th>
        <th>Operating System</th>
        <th>Port</th>
        <th>Protocol</th>
        <th>Service Name</th>
        <th>n Plugin ID</th>
        <th>n Severity</th>
        <th>n Risk_Factor</th>
        <th>n Plugin Name</th>
        <th>n Plugin Family</th>        
        <th>Description</th>
        <th>Plugin Output</th>
        <th>Synopsis</th>
      </tr>
      <xsl:for-each select="NessusClientData_v2/Report/ReportHost">
        <xsl:for-each select="ReportItem">
          <xsl:choose>
            <xsl:when test="not(@pluginID=$excludedplugins)">
              <tr>
                <td><xsl:value-of select="../@name"/></td>
                <td><xsl:value-of select="../HostProperties/tag[@name='netbios-name']"/></td>
                <td><xsl:value-of select="../HostProperties/tag[@name='host-ip']"/></td>
                <td><xsl:value-of select="../HostProperties/tag[@name='mac-address']"/></td>
                <td><xsl:value-of select="../HostProperties/tag[@name='operating-system']"/></td>
                <td><xsl:value-of select="@port"/></td>
                <td><xsl:value-of select="@protocol"/></td>
                <td><xsl:value-of select="@svc_name"/></td>
                <td><xsl:value-of select="@pluginID"/></td>
                <td><xsl:value-of select="@severity"/></td>
                <td><xsl:value-of select="risk_factor"/></td>            
                <td><xsl:value-of select="@pluginName"/></td>
                <td><xsl:value-of select="@pluginFamily"/></td>
                <td><xsl:value-of select="description"/></td>            
                <td><xsl:value-of select="plugin_output"/></td>            
                <td><xsl:value-of select="synopsis"/></td>            
              </tr>
            </xsl:when>
            <xsl:otherwise>              
            </xsl:otherwise>
          </xsl:choose>
        </xsl:for-each>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

我一起攻击了这个并提出了这些解决方案: 1.使用不需要的插件列表添加xml文件:

<xsl:variable name="excludedplugins" select="document('nessusExcludePluginList.xml')/exclude/nessusPlugin"/>
  1. 与排除的插件进行比较:

  2. 在我尝试之前不确定比较。事实证明,这适用于节点中的所有项目。真有意义!我没想到的是为什么这不起作用:

    <xsl:when test="@pluginID!=$excludedplugins">
    

    =运算符将检查所有节点,但是!=似乎根本不起作用。 “不”功能存在的好处。

    我想要做的更好的是看看是否有办法用直接的插件号列表而不是xml文件来提供不需要的插件列表,如下所示: 58181 34252 34220 11219 10335 22964

    我也认为可能有更优雅的解决方案,不使用'for each'。无论如何,感谢大家的帮助。

相关问题