Merge elements when exporting to XML

十年热恋 提交于 2019-12-04 05:14:41

问题


I'm trying to export a table in Excel to an XML file. I created a schema but I don’t have exactly the result I expect. Here is a simple example of my table: my table.

My schema:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="list">
    <xs:complexType>
        <xs:sequence minOccurs="0" maxOccurs="2">
             <xs:element name="Group" type="groupType" minOccurs="0" maxOccurs="unbounded" />
        </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:complexType name="groupType">
    <xs:sequence>
        <xs:element name="City" type="cityType" />
    </xs:sequence>
    <xs:attribute name="name" />
</xs:complexType>
<xs:complexType name="cityType">
    <xs:sequence>
        <xs:element name="value" type="xs:string" />
    </xs:sequence>
    <xs:attribute name="name" />
</xs:complexType>

This is the result I get when I export to XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<list>
    <Group name="groupA">
        <City name="Dublin">
            <value>yes</value>
        </City>
    </Group>
    <Group name="groupA">
        <City name="Prague">
            <value>yes</value>
        </City>
    </Group>
    <Group name="groupA">
        <City name="Sofia">
            <value>no</value>
        </City>
    </Group>
    <Group name="groupA">
        <City name="Tunis">
            <value>yes</value>
        </City>
    </Group>
    <Group name="groupB">
        <City name="Paris">
            <value>no</value>
        </City>
    </Group>
    <Group name="groupB">
        <City name="Lisbon">
            <value>no</value>
        </City>
    </Group>
    <Group name="groupB">
        <City name="Madrid">
            <value>no</value>
        </City>
    </Group>
</list>

It's not bad, but I would like to merge the rows which have the same group name (groupA and groupB) in the same node. In other terms, I would like to have this result:

<list>
    <Group name="groupA">
        <City name="Dublin">
            <value>yes</value>
        </City>
        <City name="Prague">
            <value>yes</value>
        </City>
        <City name="Sofia">
            <value>no</value>
        </City>
        <City name="Tunis">
            <value>yes</value>
        </City>
    </Group>
    <Group name="groupB">
        <City name="Paris">
            <value>no</value>
        </City>
        <City name="Lisbon">
            <value>no</value>
        </City>
        <City name="Madrid">
            <value>no</value>
        </City>
    </Group>
</list>

What should I do in my schema to make the result like I want?


回答1:


Excel is only able directly exporting XML in table form. This is a sequence of data rows (records) in which each data field has exactly one element. It is not able exporting list of lists. That is: One list of items has a second list of items. See https://support.office.com/en-us/article/Export-XML-data-0b21f51b-56d6-48f0-83d9-a89637cd4360.

But you want list of lists. A sequence of Groups, each with a sequence of Cities. The schema would be:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="list">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Group" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="City" maxOccurs="unbounded">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element type="xs:string" name="value"/>
                  </xs:sequence>
                  <xs:attribute type="xs:string" name="name"/>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
            <xs:attribute type="xs:string" name="name"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

But as documented this can't be exported directly with Excel's XML export feature.

So I suggest using VBA for this. The following will create the needed XML from the shown data.

Sub testXLStoXML()

 Set oXMLDoc = CreateObject("MSXML2.DOMDocument")
 Set oPI = oXMLDoc.createProcessingInstruction("xml", "version=""1.0"" encoding=""UTF-8"" standalone=""yes""")
 Set oRoot = oXMLDoc.createNode(1, "list", "")
 oXMLDoc.appendChild oRoot
 oXMLDoc.InsertBefore oPI, oXMLDoc.ChildNodes.Item(0)

 With ActiveSheet

  lRow = 2
  sGroupName = ""

  Do While .Cells(lRow, 1).Value <> ""

   sGroupName = .Cells(lRow, 1).Value
   Set oElmGroup = oXMLDoc.createNode(1, "Group", "")
   oXMLDoc.DocumentElement.appendChild oElmGroup
   Set oAttr = oXMLDoc.createNode(2, "name", "")
   oAttr.NodeValue = sGroupName
   oElmGroup.setAttributeNode oAttr

   Do While .Cells(lRow, 1).Value = sGroupName

    Set oElmCity = oXMLDoc.createNode(1, "City", "")
    Set oAttr = oXMLDoc.createNode(2, "name", "")
    oAttr.NodeValue = .Cells(lRow, 2).Value
    oElmCity.setAttributeNode oAttr

    Set oElmValue = oXMLDoc.createNode(1, "Value", "")
    oElmValue.appendChild oXMLDoc.createTextNode(.Cells(lRow, 3).Value)

    oElmCity.appendChild oElmValue
    oElmGroup.appendChild oElmCity

    lRow = lRow + 1

   Loop

  Loop

 End With

 MsgBox oXMLDoc.XML

 oXMLDoc.Save "test.xml"

End Sub

The Worksheet with the shown data must be the active worksheet while the macro is running.



来源:https://stackoverflow.com/questions/33707220/merge-elements-when-exporting-to-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!