在树中添加后搜索期间在 lxml 树中找不到 Lxml 对象
Lxml object not found in lxml tree during search after adding in it in tree
Content inside Test.arxml
<?xml version="1.0" encoding="utf-8"?>
<AUTOSAR xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://autosar.org/schema/r4.0" xsi:schemaLocation="http://autosar.org/schema/r4.0 AUTOSAR_4-2-1.xsd">
<AR-PACKAGES>
<AR-PACKAGE>
<SHORT-NAME>AUTOSAR</SHORT-NAME>
</AR-PACKAGE>
<AR-PACKAGE>
<SHORT-NAME>BALL</SHORT-NAME>
</AR-PACKAGE>
from lxml import etree
from lxml import objectify
from lxml.etree import fromstring
tree = objectify.parse('Test.arxml')
root = tree.getroot()
namespaces = {}
namespaces['ar'] = root.nsmap[None]
xpath = './/ar:AR-PACKAGE/ar:SHORT-NAME'
hits = root.xpath(xpath, namespaces=namespaces)
hits
的状态
new_element_string = "<AR-PACKAGE xmlns:None='http://autosar.org/schema/r4.0'><SHORT-NAME xmlns:None='http://autosar.org/schema/r4.0'></SHORT-NAME>{0}</AR-PACKAGE>".format("AUTOSAR_Platform")
new_element_node = node = objectify.fromstring(new_element_string)
root['AR-PACKAGES'].append(new_element_node)
second_hits = root.xpath(xpath, namespaces=namespaces)
second_hits
的状态
Question:
Why 'AUTOSAR_Platform' is not seen as third package inside second_hits ?
您在追加的节点中的文本位置错误。你可能想要
new_element_string = "<AR-PACKAGE xmlns:None='http://autosar.org/schema/r4.0'><SHORT-NAME xmlns:None='http://autosar.org/schema/r4.0'></SHORT-NAME>{0}</AR-PACKAGE>".format("AUTOSAR_Platform")
成为
new_element_string = "<AR-PACKAGE xmlns:None='http://autosar.org/schema/r4.0'><SHORT-NAME xmlns:None='http://autosar.org/schema/r4.0'>{0}</SHORT-NAME></AR-PACKAGE>".format("AUTOSAR_Platform")
然后获取要匹配的命名空间:
new_element_string = "<AR-PACKAGE xmlns='http://autosar.org/schema/r4.0'><SHORT-NAME>{0}</SHORT-NAME></AR-PACKAGE>".format("AUTOSAR_Platform")
Content inside Test.arxml
<?xml version="1.0" encoding="utf-8"?>
<AUTOSAR xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://autosar.org/schema/r4.0" xsi:schemaLocation="http://autosar.org/schema/r4.0 AUTOSAR_4-2-1.xsd">
<AR-PACKAGES>
<AR-PACKAGE>
<SHORT-NAME>AUTOSAR</SHORT-NAME>
</AR-PACKAGE>
<AR-PACKAGE>
<SHORT-NAME>BALL</SHORT-NAME>
</AR-PACKAGE>
from lxml import etree
from lxml import objectify
from lxml.etree import fromstring
tree = objectify.parse('Test.arxml')
root = tree.getroot()
namespaces = {}
namespaces['ar'] = root.nsmap[None]
xpath = './/ar:AR-PACKAGE/ar:SHORT-NAME'
hits = root.xpath(xpath, namespaces=namespaces)
hits
的状态new_element_string = "<AR-PACKAGE xmlns:None='http://autosar.org/schema/r4.0'><SHORT-NAME xmlns:None='http://autosar.org/schema/r4.0'></SHORT-NAME>{0}</AR-PACKAGE>".format("AUTOSAR_Platform")
new_element_node = node = objectify.fromstring(new_element_string)
root['AR-PACKAGES'].append(new_element_node)
second_hits = root.xpath(xpath, namespaces=namespaces)
second_hits
的状态Question: Why 'AUTOSAR_Platform' is not seen as third package inside second_hits ?
您在追加的节点中的文本位置错误。你可能想要
new_element_string = "<AR-PACKAGE xmlns:None='http://autosar.org/schema/r4.0'><SHORT-NAME xmlns:None='http://autosar.org/schema/r4.0'></SHORT-NAME>{0}</AR-PACKAGE>".format("AUTOSAR_Platform")
成为
new_element_string = "<AR-PACKAGE xmlns:None='http://autosar.org/schema/r4.0'><SHORT-NAME xmlns:None='http://autosar.org/schema/r4.0'>{0}</SHORT-NAME></AR-PACKAGE>".format("AUTOSAR_Platform")
然后获取要匹配的命名空间:
new_element_string = "<AR-PACKAGE xmlns='http://autosar.org/schema/r4.0'><SHORT-NAME>{0}</SHORT-NAME></AR-PACKAGE>".format("AUTOSAR_Platform")