将XML文件导入Excel仅显示root

时间:2018-05-04 13:40:43

标签: xml excel

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

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

Here is a picture of the top of the XML file:

提前感谢您的任何帮助。在在线帮助中,我甚至无法找到其他任何有此问题的人。

1 个答案:

答案 0 :(得分:0)

好的,我已经找到了解决方案。我会尽力解释它。

首先,让我以一般方式重述问题(省略一些细节)。我有一个根数据节点,其中一个字段是一个包含更多相同类型节点的表。这些新节点也有相同类型节点的表,这可以永远持续下去。使用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

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

相关问题