首页 > 解决方案 > 将 XML 文件导入 Excel 仅显示根目录

问题描述

我正在尝试将 XML 文件导入 Excel。该文件基本上是一个站点地图,它只是所有页面的树。如果在“数据”选项卡下,我使用“来自 Web”选项(文件来自在线 API),则导入只会返回标题和根节点,而忽略其他 3000 多个页面。

我相信问题源于子页面的表示方式。主页有一个 subpages 元素,其中包含所有较低的页面,而这些较低的页面也都有子页面。我认为 Excel 只是忽略了子页面(也许它不知道如何处理它)。

这是 XML 文件顶部的图片:

提前感谢您的任何帮助。我真的很难在在线帮助中找到其他有此问题的人。

标签: xmlexcel

解决方案


好的,我想出了一个解决方案。我会尽力解释。

首先,让我以一般的方式重申这个问题(它遗漏了一些细节)。我有一个根数据节点,其中一个字段是包含更多相同类型节点的表。这些新节点也有相同类型节点的表,而且这种情况可能会一直持续下去。使用 Power Query/Excel 导入时,只会显示根节点。我可以单击表格单元格并查看下一层,但不能同时查看所有内容。

我需要做的是提取这些子表的内容并将它们附加到现有表的底部,并对表中的每一行(包括这些新添加的节点)执行此操作。如果您熟悉搜索算法,则可以将其可视化为广度优先搜索(不是很重要,但对我有帮助)。

要复制的一篇有用的文章/代码来自此链接:https ://blog.crossjoin.co.uk/2014/05/21/expanding-all-columns-in-a-table-in-power-query/

这不是解决方案,而是类似的(我的就是基于它)。它水平扩展列,而我希望垂直附加内容。

最后,这是解决方案的代码。它是您可以调用的 Power Query 函数,输入是要展开的表。请注意,这个解决方案中有很多是针对我的问题的,如果你需要使用它,你需要对其进行编辑。我为您可能需要在评论中更改星号的内容添加了一些注释。

let
//Define function
Source = (TableToExpand as table, optional RowNumber as number) =>
    let
    //We only want subpages column, *you will need to change this*
    ActualColumnNumber = 6,

    //RowNumber starts at 0 by default
    ActualRowNumber = if (RowNumber=null) then 0 else RowNumber,

    //Find the column name relating to the column number
    ColumnName = Table.ColumnNames(TableToExpand){ActualColumnNumber},

    //Expands whole column once, only replaces column with next table
    //*My subpage table was under another table, you will probably not need this line*   
    TempExpand1 = Table.ExpandTableColumn(TableToExpand, ColumnName, {"page"}, {"subpages.page"}),

    //Get Cell for this iteration, the expansion table of the Cell
    Cell = TempExpand1{ActualRowNumber}[subpages.page],

    CanExpandCell = if Cell is table then true else false,

    //*This just fixes up the data types. You will need to edit or delete this.*
    ExpandedTable = if CanExpandCell = true
                      then
                      Table.TransformColumnTypes(Cell,{{"uri.ui", type text}, {"title", type text}, {"namespace", type text}, {"date.created", type datetime}, {"language", type text}, {"Attribute:id", Int64.Type}, {"Attribute:guid", type text}, {"Attribute:draft.state", type text}, {"Attribute:href", type text}, {"Attribute:deleted", type logical}})
                      else
                      Cell,

    //If there was nothing to be expanded, just go to the next iteration
    //Otherwise, Append ExpandedTable to TableToExpand,
    NewTable = if CanExpandCell = false
                 then
                 TableToExpand                         
                 else
                 Table.Combine({TableToExpand, Cell}),

    //If the row number is now greater than the number of row in the table
    //Then return the table as it is
    //Else call the ExpandAndAppend function recursively with the expanded table
    NewRowNumber = ActualRowNumber + 1,
    OutputTable = if NewRowNumber>(Table.RowCount(NewTable)-1) 
                    then 
                    NewTable
                    else 
                    ExpandAndAppend(NewTable, NewRowNumber)
    in
    OutputTable
in
Source

希望它有某种意义。显然,如果其他人需要它,则需要对其进行修改。我想确保我的解决方案被记录在案,因为我无法在网上其他地方找到答案。


推荐阅读