我有两个xmlns属性,我尝试对一个节点进行xpath,但它不起作用
我正在使用XmlDocument,并且尝试从该xml进行xpath。它返回null,因为根节点具有两个xml属性。
<CreateRequest
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://fex.com/ws/openship/v15">
<WebAuthenticationDetail>
<Parent>
<Key/>
<Password />
</Parent>
<UserCredential>
<Key />
<Password />
</UserCredential>
</WebAuthenticationDetail>
<ClientDetail>
<AccountNumber />
<MeterNumber />
</ClientDetail>
</CreateRequest>
var nsmgr = new XmlNamespaceManager(xml.NameTable);
nsmgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
var
nodes=xml.SelectNodes("/CreateRequest/ClientDetail/AccountNumber",nsmgr);
答案 0 :(得分:1)
您的xml具有默认的名称空间。您必须添加它,并为其分配一个前缀(我用过
import re
import subprocess
import time
import mysql.connector
from mysql.connector import Error
from mysql.connector import errorcode
print "----------------------------------"
print 'Started: {} {}'.format(time.strftime('%d/%m/%y %H:%M:%S'), "")
response = subprocess.Popen('speedtest-cli --simple', shell=True, stdout=subprocess.PIPE).stdout.read()
ping = re.findall('Ping:\s(.*?)\s', response, re.MULTILINE)
download = re.findall('Download:\s(.*?)\s', response, re.MULTILINE)
upload = re.findall('Upload:\s(.*?)\s', response, re.MULTILINE)
ping[0] = ping[0].replace(',', '.')
download[0] = download[0].replace(',', '.')
upload[0] = upload[0].replace(',', '.')
try:
if os.stat('/var/www/html/speed/log.txt').st_size == 0:
print 'Date,Time,Ping (ms),Download (Mbit/s),Upload (Mbit/s)'
except:
pass
print 'PING: {}, DOWN: {}, UP: {}'.format(ping[0], download[0], upload[0])
try:
connection = mysql.connector.connect(host='localhost',
database='dev',
user='dev',
password='dev1')
sql_insert_query = ("""INSERT INTO speedtest(ping, download, upload) VALUES (%s,%s,%s)""", (ping[0], download[0], upload[0]))
cursor = connection.cursor()
result = cursor.execute(*sql_insert_query)
connection.commit()
print ("Insert success into speedtest tbl")
except mysql.connector.Error as error :
connection.rollback() #rollback if any exception occured
print("Failed inserting record into speedtest table {}".format(error))
finally:
#closing database connection.
if(connection.is_connected()):
cursor.close()
connection.close()
print("MySQL conn closed")
print 'Finished: {} {}'.format(time.strftime('%d/%m/%y %H:%M:%S'), "")
)。然后在xpath中使用此前缀。
按以下方式使用它:
ns
答案 1 :(得分:0)
您可以忽略名称空间。
xml.SelectNodes("/*[local-name()='CreateRequest']/*[local-name()='ClientDetail']/*[local-name()='AccountNumber']");