简体   繁体   English

xml xpath 和 python 中的嵌套

[英]xml xpath and nesting in python

Im trying to print out the description within .//statement/statement/description which would be the following statements我试图打印出.//statement/statement/description中的描述,这将是以下语句

" Implements a process for ensuring that organizational plans for conducting security testing, training, and monitoring activities associated with organizational information systems:" “ 实施一个过程,以确保组织计划进行与组织信息系统相关的安全测试、培训和监控活动:”

"Reviews testing, training, and monitoring plans for consistency with the organizational risk management strategy and organization-wide priorities for risk response actions." “审查测试、培训和监控计划,以与组织风险管理战略和组织范围内的风险响应行动优先事项保持一致。”

but for some reason it also drills deeper and prints out the following two statement as well但出于某种原因,它也钻得更深,并打印出以下两个语句

"Are developed and maintained; and" “被开发和维护;和”

"Continue to be executed in a timely manner;" “继续按时执行;”

This is the order it prints it in这是它打印的顺序

Implements a process for ensuring that organizational plans for conducting security testing, training, and monitoring activities associated with organizational information systems:实施一个过程,以确保组织计划进行与组织信息系统相关的安全测试、培训和监控活动:

Reviews testing, training, and monitoring plans for consistency with the organizational risk management strategy and organization-wide priorities for risk response actions.审查测试、培训和监控计划,以与组织风险管理战略和组织范围内的风险应对行动优先级保持一致。

Are developed and maintained;被开发和维护; and

Continue to be executed in a timely manner;继续按时执行;

what should I change so that it only prints我应该更改什么以使其仅打印

Implements a process for ensuring that organizational plans for conducting security testing, training, and monitoring activities associated with organizational information systems:实施一个过程,以确保组织计划进行与组织信息系统相关的安全测试、培训和监控活动:

Reviews testing, training, and monitoring plans for consistency with the organizational risk management strategy and organization-wide priorities for risk response actions.审查测试、培训和监控计划,以确保与组织风险管理战略和组织范围内的风险应对行动优先级保持一致。

Python Code Python代码

import xml.etree.ElementTree as ET 
import csv


xmlFile='/Users/username/Desktop/xmlFile.xml'
tree = ET.parse(xmlFile) 
root = tree.getroot()

# open a file for writing
excelFile = open('/Users/username/Desktop/table2.csv', 'w')

# creates the csv writer object / varible to write to csv
csvwriter = csv.writer(excelFile)

# list that contains the header
list_head = []
count = 0

for element in root.findall('control'):
    list_nodes=[]
    if count == 0:

        number = element.find('number').tag
        list_head.append(number)

        description =element.find('.//statement/description').tag
        list_head.append(description)

        csvwriter.writerow(list_head)
        count = count + 1

    # Control number 
    number = 'Nist800-53-V4-' + element.find('number').text  
    list_nodes.append(number)


    # Control Description 
    if element.find('.//statement'):
        if element.find('.//statement/statement/') is not None:
            for descrip in element.findall('.//statement/statement/description'):
                descrip_value = descrip.text
                print(descrip_value)

    csvwriter.writerow(list_nodes)
excelFile.close()

XML file XML文件

<?xml version="1.0" encoding="UTF-8"?>
<controls>
  <control>
    <family>PROGRAM MANAGEMENT</family>
    <number>PM-14</number>
    <title>TESTING, TRAINING, AND MONITORING</title>
    <statement>
      <description>The organization:</description>
      <statement>
        <number>PM-14a.</number>
        <description>
        Implements a process for ensuring that organizational plans for conducting security testing, training, and monitoring activities associated with organizational information systems:
        </description>
        <statement>
          <number>PM-14a.1.</number>
          <description>Are developed and maintained; and</description>
        </statement>
        <statement>
          <number>PM-14a.2.</number>
          <description>Continue to be executed in a timely manner;</description>
        </statement>
      </statement>
      <statement>
        <number>PM-14b.</number>
        <description>
        Reviews testing, training, and monitoring plans for consistency with the organizational risk management strategy and organization-wide priorities for risk response actions.
        </description>
      </statement>
    </statement>
    <supplemental-guidance>
      <description>
      This control ensures that organizations provide oversight for the security testing, training, and monitoring activities conducted organization-wide and that those activities are coordinated. With the importance of continuous monitoring programs, the implementation of information security across the three tiers of the risk management hierarchy, and the widespread use of common controls, organizations coordinate and consolidate the testing and monitoring activities that are routinely conducted as part of ongoing organizational assessments supporting a variety of security controls. Security training activities, while typically focused on individual information systems and specific roles, also necessitate coordination across all organizational elements. Testing, training, and monitoring plans and activities are informed by current threat and vulnerability assessments.
      </description>
      <related>AT-3</related>
      <related>CA-7</related>
      <related>CP-4</related>
      <related>IR-3</related>
      <related>SI-4</related>
    </supplemental-guidance>
    <references>
      <reference>
        <item xml:lang="en-US" href="https://csrc.nist.gov/publications/search?keywords-lg=800-16">NIST Special Publication 800-16</item>
      </reference>
      <reference>
        <item xml:lang="en-US" href="https://csrc.nist.gov/publications/search?keywords-lg=800-37">NIST Special Publication 800-37</item>
      </reference>
      <reference>
        <item xml:lang="en-US" href="https://csrc.nist.gov/publications/search?keywords-lg=800-53A">NIST Special Publication 800-53A</item>
      </reference>
      <reference>
        <item xml:lang="en-US" href="https://csrc.nist.gov/publications/search?keywords-lg=800-137">NIST Special Publication 800-137</item>
      </reference>
    </references>
  </control>
</controls>

Your XPath expression您的 XPath 表达式

.//statement/description

retrieves all <description> elements that are a direct child of a <statement> element.检索作为<statement>元素的直接子元素的所有<description>元素。 These are many - as you experienced.这些有很多 - 正如你所经历的。
Change your expression to将你的表达改为

statement/statement/description

and you will get the result you want, because you will only select the <description> elements that have two <statement> ancestors (not exact, but sufficient to get the gist).并且您将得到您想要的结果,因为您将只选择具有两个<statement>祖先的<description>元素(不准确,但足以获得要点)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM