如何在Power Query中提取XML属性并显示为列?
提取XML中produit节点的id属性到Power Query列
XML结构
<?xml version='1.0' encoding='UTF-8'?> <catalog xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' language='FR' country='FR'> <produit id='1234'> <color><![CDATA[red]]></color> <fermete><![CDATA[hard]]></fermete> <forme><![CDATA[square]]></forme> </produit> </catalog>
问题描述
将该XML加载到Power Query后,能获取color、fermete、forme对应的列,但无法提取produit节点的id='1234'属性作为列。
原Power Query代码
let Source = Xml.Tables(File.Contents("file.xml")), #"Type modifié" = Table.TransformColumnTypes(Source,{{"Attribute:language", type text}, {"Attribute:country", type text}}), #"produit développé" = Table.ExpandTableColumn(#"Type modifié", "produit", {"color", "fermete", "forme"}) in #"produit développé"
解决方案
可以实现提取该属性为列。Xml.Tables会将节点属性命名为Attribute:属性名格式,只需在展开produit列时加入Attribute:id字段即可:
修改后的代码(直接提取id属性)
let Source = Xml.Tables(File.Contents("file.xml")), #"Type modifié" = Table.TransformColumnTypes(Source,{{"Attribute:language", type text}, {"Attribute:country", type text}}), #"produit développé" = Table.ExpandTableColumn(#"Type modifié", "produit", {"Attribute:id", "color", "fermete", "forme"}) in #"produit développé"
可选:重命名id列
如果需要将Attribute:id改为更直观的列名,可添加重命名步骤:
let Source = Xml.Tables(File.Contents("file.xml")), #"Type modifié" = Table.TransformColumnTypes(Source,{{"Attribute:language", type text}, {"Attribute:country", type text}}), #"produit développé" = Table.ExpandTableColumn(#"Type modifié", "produit", {"Attribute:id", "color", "fermete", "forme"}), #"Colonnes renommées" = Table.RenameColumns(#"produit développé", {{"Attribute:id", "produit_id"}}) in #"Colonnes renommées"
内容的提问来源于stack exchange,提问作者bahamut100
相关产品推荐
相关产品推荐

