Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python无法将某些CLOB xml保存到xml文件中_Python_Xml_Oracle - Fatal编程技术网

Python无法将某些CLOB xml保存到xml文件中

Python无法将某些CLOB xml保存到xml文件中,python,xml,oracle,Python,Xml,Oracle,我已将此xml另存为oracle数据库中的CLOB: <?xml version="1.0" encoding="UTF-8"?> <DCResponse> ... </DCResponse> 而是使用这个xml <?xml version="1.0" encoding="ISO-8859-1"?> <PIPEDocument xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi

我已将此xml另存为oracle数据库中的CLOB:

<?xml version="1.0" encoding="UTF-8"?>
<DCResponse>
...
</DCResponse>
而是使用这个xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<PIPEDocument xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:XML-PIPE 
PIPEDocument.xsd" ReferenceNumber="567862650" CreationDate="20200115155255" Version="1.0"  
xmlns="urn:XML-PIPE">
...
</PIPEDocument>
在我的oracle客户端中,python中使用的以下sql查询的输出为空:

select extract(xmltype.createxml(xml), '//PIPEDocument').getStringVal() from table t where id = 7;
当xml内容出现在my DB中时:

select xml from table where id =7
不确定是什么问题,可能是select查询中的关键字“//PIPEDocument”或两个XML文件之间的不同编码,但不知道如何解决此问题

请帮忙 顺致敬意,
Giancarlo

问题在于,在第二个XML文档中,您有名称空间。元素位于名称空间xmlns=urn:XML-PIPE中,您的XPath表达式//PIPEDocument'只匹配“default”名称空间中的元素

如果要将名称空间与提取函数一起使用,则必须添加:

前缀到命名空间URI的命名空间映射,使用可选的第三个参数进行提取。第三个参数是一个字符串,其格式与XML文档中的xmlns:*属性相同。您不能以这种方式映射默认名称空间,因此xmlns=urn:XML-PIPE在这里不起作用。相反,使用前缀,例如p,或者管道

在要提取的第二个参数中向XPath表达式添加前缀

我对代码做了这些更改,选择使用名称空间前缀p。我还将整个SQL字符串包装在三个引号中,以避免转义其中的引号。这给了我以下信息,返回了所需的XML输出:

sql = """select extract(xmltype.createxml(xml), '//p:PIPEDocument', 'xmlns:p="urn:XML-PIPE"').getStringVal() from table t where id = 7"""
for row in cursor.execute(sql):
    print(row[0])

对于查询性能,您可能希望以字符串形式获取LOB,请参阅

作为一般参考,如果数据库类型为XMLType,则使用XMLType.getclobval以避免有限的XML长度

select xml from table where id =7
sql = """select extract(xmltype.createxml(xml), '//p:PIPEDocument', 'xmlns:p="urn:XML-PIPE"').getStringVal() from table t where id = 7"""
for row in cursor.execute(sql):
    print(row[0])