对于Java框架(MATSim)的使用,我希望能够从XML导入和导出到Excel(以及从Excel到XML)。为此,我使用XSD文件,但是某些节点必须多次出现。
我的XML文件如下:
<population>
<attributes>
<attribute name="coordinateReferenceSystem" class="java.lang.String" >EPSG:2154</attribute>
</attributes>
<!-- ====================================================================== -->
<person id="100007">
<attributes>
<attribute name="age" class="java.lang.Integer" >50</attribute>
</attributes>
<plan score="125.08754835604087" selected="no">
<activity type="h" link="502570" x="645800.341" y="6860736.1577" end_time="07:58:00" >
</activity>
<leg mode="drt" dep_time="07:58:00" trav_time="00:30:09">
<route type="drt" start_link="502570" end_link="146319" trav_time="00:30:09" distance="10233.148696149716">720.0 806.0</route>
</leg>
<activity type="w" link="146319" x="645036.2443" y="6868434.1435" end_time="18:17:12" >
</activity>
<leg mode="drt" dep_time="18:17:12" trav_time="00:31:42">
<route type="drt" start_link="146319" end_link="502570" trav_time="00:31:42" distance="11558.582054437727">720.0 868.0</route>
</leg>
<activity type="h" link="502570" x="645800.341" y="6860736.1577" >
</activity>
</plan>
<plan score="129.66748741175877" selected="no">
<activity type="h" link="502570" x="645800.341" y="6860736.1577" end_time="07:58:37" >
</activity>
<leg mode="drt" dep_time="07:58:00" trav_time="00:30:09">
<route type="drt" start_link="502570" end_link="146319" trav_time="00:30:25" distance="10233.148696149716">720.0 817.0967661750838</route>
</leg>
<activity type="w" link="146319" x="645036.2443" y="6868434.1435" end_time="18:12:16" >
</activity>
<leg mode="drt" dep_time="18:17:12" trav_time="00:31:42">
<route type="drt" start_link="146319" end_link="502570" trav_time="00:31:47" distance="11558.582054437727">720.0 871.4448190285184</route>
</leg>
<activity type="h" link="502570" x="645800.341" y="6860736.1577" >
</activity>
</plan>
<plan score="129.02719843569574" selected="no">
<activity type="h" link="502570" x="645800.341" y="6860736.1577" end_time="07:58:00" >
</activity>
<leg mode="drt" dep_time="07:58:00" trav_time="00:30:29">
<route type="drt" start_link="502570" end_link="146319" trav_time="00:30:29" distance="10233.148696149716">720.0 819.4304164945643</route>
</leg>
<activity type="w" link="146319" x="645036.2443" y="6868434.1435" end_time="18:17:12" >
</activity>
<leg mode="drt" dep_time="18:17:12" trav_time="00:31:56">
<route type="drt" start_link="146319" end_link="502570" trav_time="00:31:56" distance="11558.582054437727">720.0 877.3676952931855</route>
</leg>
<activity type="h" link="502570" x="645800.341" y="6860736.1577" >
</activity>
</plan>
<plan score="128.83469902010924" selected="no">
<activity type="h" link="502570" x="645800.341" y="6860736.1577" end_time="08:01:19" >
</activity>
<leg mode="drt" dep_time="07:58:00" trav_time="00:30:09">
<route type="drt" start_link="502570" end_link="146319" trav_time="00:30:34" distance="10233.148696149716">720.0 823.0953896303872</route>
</leg>
<activity type="w" link="146319" x="645036.2443" y="6868434.1435" end_time="18:19:45" >
</activity>
<leg mode="drt" dep_time="18:17:12" trav_time="00:31:42">
<route type="drt" start_link="146319" end_link="502570" trav_time="00:31:56" distance="11558.582054437727">720.0 877.956354092079</route>
</leg>
<activity type="h" link="502570" x="645800.341" y="6860736.1577" >
</activity>
</plan>
<plan score="125.08754835604087" selected="yes">
<activity type="h" link="502570" x="645800.341" y="6860736.1577" end_time="07:58:00" >
</activity>
<leg mode="drt" dep_time="07:58:00" trav_time="00:30:41">
<route type="drt" start_link="502570" end_link="146319" trav_time="00:30:41" distance="10233.148696149716">720.0 827.8504105620923</route>
</leg>
<activity type="w" link="146319" x="645036.2443" y="6868434.1435" end_time="18:17:12" >
</activity>
<leg mode="drt" dep_time="18:17:12" trav_time="00:32:03">
<route type="drt" start_link="146319" end_link="502570" trav_time="00:32:03" distance="11558.582054437727">720.0 882.6003787117806</route>
</leg>
<activity type="h" link="502570" x="645800.341" y="6860736.1577" >
</activity>
</plan>
</person>
</population>
但是在Excel中,它将显示如下:
对于一个计划,它最终会显示在5条不同的行上,但是我希望每个计划只能是一个行
id, age, score, selected, type, link, x, y, end_time, mode, dep_time, trav_time, route, start_link, end_link, type, link, x, y, end_time, mode, dep_time, trav_time, route, start_link, end_link, type, link, x, y, end_time
如果我希望可以对它们进行整理,那么我不介意每个计划只使用一行,但是仅将所有数据放在一条线上会真正改变事情并使其易于使用。
我对.XSD不太了解,我只使用在线转换器来创建它。
<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid Technologies Online Tools 1.0 (https://www.liquid-technologies.com) -->
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="population">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="person">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="attributes">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="attribute">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:unsignedByte">
<xs:attribute name="name" type="xs:string" use="optional" />
<xs:attribute name="class" type="xs:string" use="optional" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element minOccurs="0" maxOccurs="unbounded" name="plan">
<xs:complexType>
<xs:sequence>
<xs:choice maxOccurs="unbounded">
<xs:element minOccurs="0" maxOccurs="unbounded" name="activity">
<xs:complexType>
<xs:attribute name="type" type="xs:string" use="optional" />
<xs:attribute name="link" type="xs:unsignedInt" use="optional" />
<xs:attribute name="x" type="xs:decimal" use="optional" />
<xs:attribute name="y" type="xs:decimal" use="optional" />
<xs:attribute name="end_time" type="xs:time" use="optional" />
</xs:complexType>
</xs:element>
<xs:element minOccurs="0" maxOccurs="unbounded" name="leg">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="route">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="type" type="xs:string" use="optional" />
<xs:attribute name="start_link" type="xs:unsignedInt" use="optional" />
<xs:attribute name="end_link" type="xs:unsignedInt" use="optional" />
<xs:attribute name="trav_time" type="xs:time" use="optional" />
<xs:attribute name="distance" type="xs:decimal" use="optional" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="mode" type="xs:string" use="optional" />
<xs:attribute name="dep_time" type="xs:time" use="optional" />
<xs:attribute name="trav_time" type="xs:time" use="optional" />
</xs:complexType>
</xs:element>
</xs:choice>
</xs:sequence>
<xs:attribute name="score" type="xs:decimal" use="optional" />
<xs:attribute name="selected" type="xs:string" use="optional" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="id" type="xs:unsignedInt" use="optional" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
我试图修改数字“ minOccurs”和“ maxOccurs”,但没有给出预期的结果,据我所知,我什至不知道是否可以这样做。
非常感谢您的宝贵时间。
答案 0 :(得分:0)
您的XSD中有两个错误:
您缺少在<attributes>
元素之前(在定界符<plan>
之前)复制<!-- =====...==== -->
的定义
complexType
元素的<activity>
需要一个mixed="true"
属性,因为
混合的复杂类型元素可以包含属性,元素和文本。
因此,整个XSD应该如下所示(两个更改都带有注释):
<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid Technologies Online Tools 1.0 (https://www.liquid-technologies.com) -->
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="population">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="attributes"> <!-- Added for solution -->
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="attribute">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:unsignedByte">
<xs:attribute name="name" type="xs:string" use="optional" />
<xs:attribute name="class" type="xs:string" use="optional" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element minOccurs="0" name="person">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="attributes">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="attribute">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:unsignedByte">
<xs:attribute name="name" type="xs:string" use="optional" />
<xs:attribute name="class" type="xs:string" use="optional" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element minOccurs="0" maxOccurs="unbounded" name="plan">
<xs:complexType>
<xs:sequence>
<xs:choice maxOccurs="unbounded">
<xs:element minOccurs="0" maxOccurs="unbounded" name="activity">
<xs:complexType mixed="true"> <!-- Added for solution -->
<xs:attribute name="type" type="xs:string" use="optional" />
<xs:attribute name="link" type="xs:unsignedInt" use="optional" />
<xs:attribute name="x" type="xs:decimal" use="optional" />
<xs:attribute name="y" type="xs:decimal" use="optional" />
<xs:attribute name="end_time" type="xs:time" use="optional" />
</xs:complexType>
</xs:element>
<xs:element minOccurs="0" maxOccurs="unbounded" name="leg">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="route">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="type" type="xs:string" use="optional" />
<xs:attribute name="start_link" type="xs:unsignedInt" use="optional" />
<xs:attribute name="end_link" type="xs:unsignedInt" use="optional" />
<xs:attribute name="trav_time" type="xs:time" use="optional" />
<xs:attribute name="distance" type="xs:decimal" use="optional" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="mode" type="xs:string" use="optional" />
<xs:attribute name="dep_time" type="xs:time" use="optional" />
<xs:attribute name="trav_time" type="xs:time" use="optional" />
</xs:complexType>
</xs:element>
</xs:choice>
</xs:sequence>
<xs:attribute name="score" type="xs:decimal" use="optional" />
<xs:attribute name="selected" type="xs:string" use="optional" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="id" type="xs:unsignedInt" use="optional" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
为避免混合内容的必要性,您必须将<activity>
元素更改为不包含任何内容(甚至空格)。所以你可以改变
<activity type="h" link="502570" x="645800.341" y="6860736.1577" end_time="07:58:00">
</activity>
到
<activity type="h" link="502570" x="645800.341" y="6860736.1577" end_time="07:58:00" />
或
<activity type="h" link="502570" x="645800.341" y="6860736.1577" end_time="07:58:00"></activity>
但是对于当前的空白,您必须在mixed="true"
上使用complexType
属性。