postgresql - Postgres 使用带有 xmlnamespaces 的 xpath_table 解析
问题描述
我可以使用xpath_table
xmlnamespaces 解析吗
drop table if exists _xml;
create temporary table _xml (fbf_xml_id serial,str_Xml xml);
insert into _xml(str_Xml)
select '<DataSet1 xmlns="http://tempuri.org/DataSet_LocalMaMC.xsd">
<Stations>
<ID>5</ID>
</Stations>
<Stations>
<ID>1</ID>
</Stations>
<Stations>
<ID>2</ID>
</Stations>
<Stations>
<ID>10</ID>
</Stations>
<Stations>
<ID/>
</Stations>
</DataSet1>' ;
drop table if exists _y;
create temporary table _y as
SELECT *
FROM xpath_table('FBF_xml_id','str_Xml','_xml',
'/DataSet1/Stations/ID',
'true') AS t(FBF_xml_id int,ID text);
select * from _y
如果我采用 xmlnamespaces 它工作正常。我想使用 Xpath,但是当有 null 时,它会给我错误的结果。
解决方案
使用 Postgres 10 或更高版本,xmltable()
是执行此操作的首选方法。
您可以使用它轻松指定命名空间列表。
SELECT fbf_xml_id, xt.id
FROM _xml
cross join
xmltable(xmlnamespaces ('http://tempuri.org/DataSet_LocalMaMC.xsd' as x),
'/x:DataSet1/x:Stations'
passing str_xml
columns
id text path 'x:ID') as xt
请注意,在用于xmltable()
函数的 XPath 表达式中,标记以xmlnamespaces
选项中定义的命名空间别名为前缀,即使它们在输入 XML 中没有前缀。
推荐阅读
- python - 使用 2 列和 pandas 中第三列的值创建 dict 的 dict
- php - 如何在 wordpress 管理员和用户端使用相同的对象?
- python - 如何在 Jupyter Notebook 中使用 sklearn 树和 export_graph_viz 调整树的图像大小
- javascript - 最小最大百分比的短(er)手?
- mysql - 提交或页面重新加载后如何在选择框中保留选定的值是从数据库中检索的值和选项不同?
- javascript - 如何从包含在 html 表格列中的日期中提取月份?
- php - 表格上的PHP分页
- docker - 需要帮助来使用 docker 设置 Rasa NLU 服务器
- mysql - 通过 docker-compose 从 ASP.NET Core 2 Web 应用程序连接到 MySql 8
- javascript - 如何从返回 document.write() 的 URL 异步显示数据?