将可重复的子元素转换为列

问题描述 投票:0回答:1

首先,我对 power query 非常陌生......我有类似的 xml,如下所示:

<root>
  <entries>
    <entry>
      <otherData>
        <id>1</id>
      </otherData>
      <images>
        <image>
          <url>http://example.com/1.img</url>
        </image>
        <image>
          <url>http://example.com/1.img</url>
        </image>
        <image>
          <url>http://example.com/1.img</url>
        </image>
      </images>
    </entry>
    <entry>
      <otherData>
        <id>2</id>
      </otherData>
      <images>
        <image>
          <url>http://example.com/4.img</url>
        </image>
        <image>
          <url>http://example.com/5.img</url>
        </image>
      </images>
    </entry>
  </entries>
<root>

我想要的是一个由

id
组成的表格,然后是基于
/images/image/url
的动态列数。我不知道是否可以动态添加列并以某种方式命名它们(带有数字后缀
img1
...
imgN

id img1 img2 img3
1 http://example.com/1.img http://example.com/2.img http://example.com/3.img|
2 http://example.com/4.img http://example.com/5.img
powerquery
1个回答
0
投票

我认为你可以先将XML加载到powerquery中,然后继续单击“展开列”图标:

enter image description here

直到你的表格如下所示:

enter image description here

然后您需要打开高级编辑器并编写自己的M代码以将表格转换为您想要的格式。

我使用的代码是这样的:

let
  Source = Xml.Tables(File.Contents("/Users/userName/Documents/test.xml")),
  // below code are generated by clicking "expand column" icon
  #"Expanded Table" = Table.ExpandTableColumn(Source, "Table", {"Name", "Table"}, {"Name.1", "Table.1"}),
  #"Expanded Table.1" = Table.ExpandTableColumn(#"Expanded Table", "Table.1", {"otherData", "images"}, {"otherData", "images"}),
  #"Expanded otherData" = Table.ExpandTableColumn(#"Expanded Table.1", "otherData", {"id"}, {"id"}),
  #"Expanded images" = Table.ExpandTableColumn(#"Expanded otherData", "images", {"image"}, {"image"}),

  
  // below codes are written by hand
  GetUrls = Table.AddColumn(#"Expanded images", "urls", (row)=> row[image][url]),
  GetHeaders = Table.AddColumn(GetUrls, "headers", (row)=>List.Transform({1..List.Count(row[urls])}, (x)=> "Image" & Number.ToText(x))),
  AllHeaders = List.Distinct(List.Combine(GetHeaders[headers])),
  CreateUrlsByHeader = Table.AddColumn(GetHeaders, "urlsByHeader", (row)=> Record.FromList(row[urls], row[headers])),
  SelectColumns = Table.SelectColumns(CreateUrlsByHeader, {"id", "urlsByHeader"}),
  #"Expanded urlsByHeader" = Table.ExpandRecordColumn(SelectColumns, "urlsByHeader", AllHeaders, AllHeaders)
in
  #"Expanded urlsByHeader"

结果:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.