未生成XSD文件中元素内的元素

时间:2015-08-31 21:10:45

标签: java xml xsd jaxb

我正在尝试在XSD文件中的元素中添加元素 当我尝试这样做时,我收到了错误:
[错误] s4s-elt-invalid-content.1:'#AnonType_nodeosm'的内容是无效的。元素'元素'无效,错位或过于频繁发生。   文件第32行:/ D:/.../ src / conf / xml-resources / jaxb / generated / scheme.xsd

当我添加<xs:element name="edge".... </xs:element>时,错误开始了 没有它,它工作正常。

我想做什么:
Class Edge将成为Node类中的内部类 我也希望类Node有成员List<Edge> m_Edges,我不知道该怎么做 我试图这样做的XSD元素有效,但没有成功。

我的XSD文件:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified">
    <xs:element name="osm">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="bounds">
                    <xs:complexType>
                        <xs:simpleContent>
                            <xs:extension base="xs:string">
                                <xs:attribute type="xs:float" name="minlat" />
                                <xs:attribute type="xs:float" name="minlon" />
                                <xs:attribute type="xs:float" name="maxlat" />
                                <xs:attribute type="xs:float" name="maxlon" />
                            </xs:extension>
                        </xs:simpleContent>
                    </xs:complexType>
                </xs:element>
                <xs:element name="node" maxOccurs="unbounded" minOccurs="0">
                    <xs:complexType mixed="true">
                        <xs:sequence>
                            <xs:element name="tag" maxOccurs="unbounded" minOccurs="0">
                                <xs:complexType>
                                    <xs:simpleContent>
                                        <xs:extension base="xs:string">
                                            <xs:attribute type="xs:string" name="k" use="optional" />
                                            <xs:attribute type="xs:string" name="v" use="optional" />
                                        </xs:extension>
                                    </xs:simpleContent>
                                </xs:complexType>
                            </xs:element>          
                        </xs:sequence>
                    <xs:element name="edge" maxOccurs="unbounded" minOccurs="0">
                        <xs:complexType mixed="true">
                            <xs:attribute type="xs:node" name="source" use="optional" />
                            <xs:attribute type="xs:node" name="destination" use="optional" />
                            <xs:attribute type="xs:double" name="weight" use="optional" />
                        </xs:complexType>
                    </xs:element>

                        <xs:attribute type="xs:long" name="id" use="optional" />
                        <xs:attribute type="xs:string" name="visible" use="optional" />
                        <xs:attribute type="xs:byte" name="version" use="optional" />
                        <xs:attribute type="xs:int" name="changeset" use="optional" />
                        <xs:attribute type="xs:dateTime" name="timestamp" use="optional" />
                        <xs:attribute type="xs:string" name="user" use="optional" />
                        <xs:attribute type="xs:int" name="uid" use="optional" />
                        <xs:attribute type="xs:float" name="lat" use="optional" />
                        <xs:attribute type="xs:float" name="lon" use="optional" />

                    </xs:complexType>

                </xs:element>


                <xs:element name="way" maxOccurs="unbounded" minOccurs="0">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="nd" maxOccurs="unbounded" minOccurs="0">
                                <xs:complexType>
                                    <xs:simpleContent>
                                        <xs:extension base="xs:string">
                                            <xs:attribute type="xs:long" name="ref" use="optional" />
                                        </xs:extension>
                                    </xs:simpleContent>
                                </xs:complexType>
                            </xs:element>
                            <xs:element name="tag" maxOccurs="unbounded" minOccurs="0">
                                <xs:complexType>
                                    <xs:simpleContent>
                                        <xs:extension base="xs:string">
                                            <xs:attribute type="xs:string" name="k" use="optional" />
                                            <xs:attribute type="xs:string" name="v" use="optional" />
                                        </xs:extension>
                                    </xs:simpleContent>
                                </xs:complexType>
                            </xs:element>
                        </xs:sequence>
                        <xs:attribute type="xs:int" name="id" use="optional" />
                        <xs:attribute type="xs:string" name="visible" use="optional" />
                        <xs:attribute type="xs:byte" name="version" use="optional" />
                        <xs:attribute type="xs:int" name="changeset" use="optional" />
                        <xs:attribute type="xs:dateTime" name="timestamp" use="optional" />
                        <xs:attribute type="xs:string" name="user" use="optional" />
                        <xs:attribute type="xs:int" name="uid" use="optional" />
                    </xs:complexType>
                </xs:element>
                <xs:element name="relation" maxOccurs="unbounded" minOccurs="0">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="member" maxOccurs="unbounded" minOccurs="0">
                                <xs:complexType>
                                    <xs:simpleContent>
                                        <xs:extension base="xs:string">
                                            <xs:attribute type="xs:string" name="type" use="optional" />
                                            <xs:attribute type="xs:long" name="ref" use="optional" />
                                            <xs:attribute type="xs:string" name="role" use="optional" />
                                        </xs:extension>
                                    </xs:simpleContent>
                                </xs:complexType>
                            </xs:element>
                            <xs:element name="tag" maxOccurs="unbounded" minOccurs="0">
                                <xs:complexType>
                                    <xs:simpleContent>
                                        <xs:extension base="xs:string">
                                            <xs:attribute type="xs:string" name="k" use="optional" />
                                            <xs:attribute type="xs:string" name="v" use="optional" />
                                        </xs:extension>
                                    </xs:simpleContent>
                                </xs:complexType>
                            </xs:element>
                        </xs:sequence>
                        <xs:attribute type="xs:int" name="id" use="optional" />
                        <xs:attribute type="xs:string" name="visible" use="optional" />
                        <xs:attribute type="xs:short" name="version" use="optional" />
                        <xs:attribute type="xs:int" name="changeset" use="optional" />
                        <xs:attribute type="xs:dateTime" name="timestamp" use="optional" />
                        <xs:attribute type="xs:string" name="user" use="optional" />
                        <xs:attribute type="xs:int" name="uid" use="optional" />
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
            <xs:attribute type="xs:float" name="version" />
            <xs:attribute type="xs:string" name="generator" />
            <xs:attribute type="xs:string" name="copyright" />
            <xs:attribute type="xs:anyURI" name="attribution" />
            <xs:attribute type="xs:anyURI" name="license" />
        </xs:complexType>
    </xs:element>
</xs:schema>

我想生成将包含类Edge的类Node,如下所示:

public class Edge {

    Node m_Source;
    Node m_Destination;

    double m_Weight;

    public Edge(Node i_Source, Node i_Destination, double i_Weight) {
        m_Source = i_Source;
        m_Destination = i_Destination;
        m_Weight = i_Weight;
    }

    public Node getSource() {
        return m_Source;
    }

    public Node getDestination() {
        return m_Destination;
    }

    public double getWeight() {
        return m_Weight;
    }


    public void setSource(Node i_SourceNode)
    {
        m_Source = i_SourceNode;
    }

    public void setDestination(Node i_DestinationNode)
    {
        m_Destination = i_DestinationNode;
    }
}

XSD解决方案没有错误(按照Andreas的回答):
enter image description here

1 个答案:

答案 0 :(得分:1)

您的错误&#34;在[您]添加<xs:element name="edge".... </xs:element>&#34;时启动,这是第32行,如错误消息所示。

您在<xs:sequence>标记之外添加了元素。将插入的元素向上移动一行。

这假设您希望<node>包含两个元素:<tag><edge>(按此顺序)。