簡體   English   中英

需要使用 Xquery 從 XML 文件生成一個表 - 不知道從哪里開始

[英]Need to generate a table from XML file using Xquery - not sure where to start

我將訂單數據存儲在 XML 中,如下所示,系統要求我將其轉換為表格。 由於這不是我的工作描述的一部分,而且我沒有相關資格,所以我要么尋求外部幫助,要么為大約 4000 名客戶手動完成。

<Customer Name="John Smith" Method="Credit">

<Order Number="1">

<Item Code="INK001">
<Price>24</Price>
<Details>Quantity: 12</Details>
</Item>

</Order>

<Order Number="2">

<Item Code="PAPER001">
<Price>12</Price>
<Details>Quantity: 240</Details>
</Item>

<Item Code="INK002">
<Price>48</Price>
<Details>Quantity: 24</Details>
</Item>

</Order>

</Customer>

<Customer Name="Jane Doe" Method="Cash">

<Order Number="1">

<Item Code="INK001">
<Price>24</Price>
<Details>Quantity: 12</Details>
</Item>

</Order>

<Order Number="2">

<Item Code="PAPER001">
<Price>12</Price>
<Details>Quantity: 240</Details>
</Item>

<Item Code="INK002">
<Price>48</Price>
<Details>Quantity: 24</Details>
</Item>

</Order>

</Customer>

希望最終得到一張按照所附屏幕截圖顯示的表格,其中每個客戶的每個項目都有自己的行。 在此處輸入圖像描述 據我所知,顯然 xQuery 是通往 go 的途徑,盡管它仍然沒有讓我知道從哪里開始。 如果有人比我更有經驗可以幫助解決這個問題,我將不勝感激。

因此,如果您談論的是 Excel(而不是 SQL 數據庫表)等工具中的可編輯表,那么只要安裝了 Excel,實際上就不需要 XQuery 編程或特殊工具。

不過,您確實需要將數據格式化為正確的 XML 文檔(因此只有一個根元素)- 使用您按原樣發布的數據,您需要將多個<Customer />元素包裝在一個根元素中,例如<Customers />

目前,在您的 XML 中,您已將其設置為:

<Customer Name="John Smith" Method="Credit"></Customer>
<Customer Name="Jane Doe" Method="Cash"></Customer>
<!-- Left out the contents of each Customer for brevity -->

更改它,使其成為具有根文檔元素和 XML 處理指令的正確 XML 文檔:

<?xml version="1.0" encoding="UTF-8"?>
<Customers>
    <Customer Name="John Smith" Method="Credit"></Customer>
    <Customer Name="Jane Doe" Method="Cash"></Customer>
</Customers>
<!-- Left out the contents of each Customer for brevity -->

一旦它是一個有效的 XML 文件,直接在 Excel 中打開它,它會提示你: 作為 XML 表的 excel 提示

選擇第一個選項。

然后它將顯示此消息:

在此處輸入圖像描述

點擊確定,然后您的可編輯 data.table 將顯示!

XML客戶作為excel數據表

唯一的問題是,如果還有更多數據未顯示,並且 Excel 未按您期望的方式顯示表格,您可能必須獲取 XML 的架構(如果存在),或者自己制作一個,至於 map complex XML,如果 Excel 有一個模式(XSD),它就容易多了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM