简体   繁体   中英

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

I have order data stored in an XML as follows and I'm being asked to convert it into a table. As this is not part of my job description and I have no relevant qualifications for this I either have to seek external help or do it manually for ~4000 or so customers.

<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>

The hope is to end up with a table as per the screenshot attached, where every item for each customer has its own row. 在此处输入图像描述 From what I could find, apparently xQuery is the way to go, although it still didn't give me much of an idea where to start. I would appreciate if anybody more experienced than me could help out with this.

So if you're talking about an editable table in a tool like Excel (and not an SQL database table), then so long as you have Excel installed, there's actually no XQuery programming or special tools needed.

You do need to format the data as a proper XML document though (so only one root element) - with the data you posted as is, you need to wrap the multiple <Customer /> elements inside a root element, such as <Customers /> .

Currently, in your XML you've got it as:

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

Change it so it's a proper XML document with a root document element and XML processing instruction:

<?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 -->

Once it's a valid XML document, open this directly in Excel and it will prompt you with: 作为 XML 表的 excel 提示

Choose the first option.

Then it will show this message:

在此处输入图像描述

Hit OK and then your editable data.table will show!

XML客户作为excel数据表

The only thing is that if there's more to the data not shown, and Excel doesn't display the table in the way you expect, you may have to either grab a schema for the XML (if one exists), or just make one yourself, as to map complex XML, it's much eaiser for Excel if it has a schema (XSD).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM