【问题标题】:How can i generate a XML-Mapping on Excel-list?如何在 Excel 列表上生成 XML 映射?
【发布时间】:2019-09-24 21:48:26
【问题描述】:

主要目的:我有一个给定的 XML 文件和 Excel 中的客户名称列表。我想创建一个 XML 映射,所以我可以用它生成一个新的 XML 文件。

我的问题:Excel 不会映射整个列表(列),而只是映射一个单元格。

这是我的 XML 文件:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Profiles xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file:///C:/Users/baj426/Documents/Altova/XMLSpy2018/Examples/test.xsd">
    <Profile CompanyId="" Duns="">
        <Tab name="Tab_Address" label="Address">
            <Group Name="Group_IDs">
                <Field Value="true" Name="Field_IDs_SIM_Strategic"/>
            </Group>
        </Tab>
    </Profile>
</Profiles>

这是我由 XMLSPy 生成的 XML-Schema:

<?xml version="1.0" encoding="UTF-8"?>

<!-- W3C Schema generated by XMLSpy v2018 rel. 2 (x64) (http://www.altova.com) -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="Tab">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="Group"/>
            </xs:sequence>
            <xs:attribute name="name" use="required">
                <xs:simpleType>
                    <xs:restriction base="xs:string">
                        <xs:enumeration value="Tab_Address"/>
                    </xs:restriction>
                </xs:simpleType>
            </xs:attribute>
            <xs:attribute name="label" use="required">
                <xs:simpleType>
                    <xs:restriction base="xs:string">
                        <xs:enumeration value="Address"/>
                    </xs:restriction>
                </xs:simpleType>
            </xs:attribute>
        </xs:complexType>
    </xs:element>
    <xs:element name="Field">
        <xs:complexType>
            <xs:attribute name="Value" use="required" type="xs:boolean"/>
            <xs:attribute name="Name" use="required">
                <xs:simpleType>
                    <xs:restriction base="xs:string">
                        <xs:enumeration value="Field_IDs_SIM_Strategic"/>
                    </xs:restriction>
                </xs:simpleType>
            </xs:attribute>
        </xs:complexType>
    </xs:element>
    <xs:element name="Group">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="Field"/>
            </xs:sequence>
            <xs:attribute name="Name" use="required">
                <xs:simpleType>
                    <xs:restriction base="xs:string">
                        <xs:enumeration value="Group_IDs"/>
                    </xs:restriction>
                </xs:simpleType>
            </xs:attribute>
        </xs:complexType>
    </xs:element>
    <xs:element name="Profile">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="Tab"/>
            </xs:sequence>
            <xs:attribute name="CompanyId" use="required">
                <xs:simpleType>
                    <xs:restriction base="xs:string">
                        <xs:enumeration value=""/>
                    </xs:restriction>
                </xs:simpleType>
            </xs:attribute>
            <xs:attribute name="Duns" use="required">
                <xs:simpleType>
                    <xs:restriction base="xs:string">
                        <xs:enumeration value=""/>
                    </xs:restriction>
                </xs:simpleType>
            </xs:attribute>
        </xs:complexType>
    </xs:element>
    <xs:element name="Profiles">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="Profile"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

这是我想要的示例代码:

这是示例 XSD:

<?xml version="1.0" encoding="UTF-8"?>

<shiporder orderid="889923"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="shiporder.xsd">
  <orderperson>John Smith</orderperson>
  <shipto>
    <name>Ola Nordmann</name>
    <address>Langgt 23</address>
    <city>4000 Stavanger</city>
    <country>Norway</country>
  </shipto>
  <item>
    <title>Empire Burlesque</title>
    <note>Special Edition</note>
    <quantity>1</quantity>
    <price>10.90</price>
  </item>
  <item>
    <title>Hide your heart</title>
    <quantity>1</quantity>
    <price>9.90</price>
  </item>
</shiporder> 

使用 Excel 中的某些值映射后的 XML 文件

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<shiporder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <item>
        <quantity>1</quantity>
        <price>120</price>
    </item>
    <item>
        <quantity>2</quantity>
        <price>120</price>
    </item>
    <item>
        <quantity>3</quantity>
        <price>120</price>
    </item>
    <item>
        <quantity>4</quantity>
        <price>120</price>
    </item>
    <item>
        <quantity>5</quantity>
        <price>120</price>
    </item>
    <item>
        <quantity>6</quantity>
        <price>120</price>
    </item>
    <item>
        <quantity>7</quantity>
        <price>120</price>
    </item>
    <item>
        <quantity>8</quantity>
        <price>120</price>
    </item>
</shiporder>

【问题讨论】:

    标签: excel xml xsd mapping


    【解决方案1】:

    Excel 无法导出具有多个 (!) 层次结构级别的 XML 文件,因此您希望创建的 XML 文件无法由 Excel 导出,除非您使用 VBA 构造 XML。

    【讨论】:

    • 您好,感谢您的回复。你有什么建议作为实现我想要的替代方案?其他软件?
    • 如何在 VBA 中构造 XML(并且是 Notepad++)也可以?
    • 从 VBA 创建 XML 并不难,您基本上可以构造一个包含整个 XML 作为文本的大字符串变量,然后使用 VBA 文件函数将字符串保存到文件中。我建议你试一试,当你遇到困难时,在这里重新发布你的尝试。
    猜你喜欢
    • 2013-09-06
    • 2012-01-23
    • 1970-01-01
    • 2023-02-22
    • 2013-03-29
    • 1970-01-01
    • 2016-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多