架构验证和xsi:type的JAXB问题

时间:2014-06-20 10:13:58

标签: java xml jaxb xsd xsd-validation

我正在尝试使用JAXB生成XSD来验证给定的XML文件,并且我在某些元素上遇到了xsi:type属性的问题。我怀疑这是某种命名空间问题,但我无法确定我究竟做错了什么。

当我尝试验证时,我收到了UIDataConnection的cvc-elt.4.2错误

Fehler: cvc-elt.4.2: Cannot resolve 'UIDataConnection' to a type definition for element 'UIData'.

向我暗示这是一个命名空间问题

这是XML

<?xml version="1.0" encoding="utf-8"?>
<InformationWorkflow xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://foo/bar/baz" workflowguid="22b1941c-efc1-4084-89e6-25dc5eae5161" creator="rbo" creationdate="2014-05-22T14:19:20.4356295+02:00" idnprefix="">
  <Name>QA_14-05-22_v1</Name>
  <Connections>
    <C idn="c:14" start="out:2.2" end="end:3">
      <UIData xsi:type="UIDataConnection" row="0" column="0">
        <ConnectionAnchor>Unset</ConnectionAnchor>
        <ConnectionAnchorOffset>0</ConnectionAnchorOffset>
      </UIData>
      <ConnectionType>Node</ConnectionType>
    </C>
    <C idn="c:15" start="out:2.3" end="end:2">
      <UIData xsi:type="UIDataConnection" row="0" column="0">
        <ConnectionAnchor>Unset</ConnectionAnchor>
        <ConnectionAnchorOffset>0</ConnectionAnchorOffset>
      </UIData>
      <ConnectionType>Node</ConnectionType>
    </C>
  </Connections>
</InformationWorkflow>

这是我的XSD:

<xs:schema elementFormDefault="qualified" version="1.0" targetNamespace="http://foo/bar/baz" xmlns:tns="http://foo/bar/baz" xmlns:knsf="http://foo/bar/thing" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

  <xs:import namespace="http://foo/bar/thing" schemaLocation="schema2.xsd"/>

  <xs:import schemaLocation="schema3.xsd"/>

  <xs:element name="C" type="tns:connection"/>

  <xs:element name="InformationWorkflow" type="tns:typeKnsModel"/>

  <xs:complexType name="typeKnsModel">
    <xs:complexContent>
      <xs:extension base="tns:dataType">
        <xs:sequence>
          <xs:element name="Name" type="xs:string"/>
          <xs:element name="Connections" minOccurs="0">
            <xs:complexType>
              <xs:sequence>
                <xs:element ref="tns:C" maxOccurs="unbounded"/>
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:sequence>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

  <xs:complexType name="dataType" abstract="true">
    <xs:sequence/>
  </xs:complexType>

  <xs:complexType name="connection">
    <xs:sequence>
      <xs:element name="label" type="xs:string" minOccurs="0"/>
      <xs:element name="UIData" type="tns:uiDataConnection" minOccurs="0"/>
      <xs:element name="ConnectionType" type="xs:string" minOccurs="0"/>
      <xs:element name="Description" type="xs:string" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="uiDataConnection">
    <xs:complexContent>
      <xs:extension base="tns:uiData">
        <xs:sequence>
          <xs:element name="ConnectionAnchor" type="xs:string" minOccurs="0"/>
          <xs:element name="ConnectionAnchorOffset" type="xs:double"/>
        </xs:sequence>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

  <xs:complexType name="uiData">
    <xs:sequence/>
    <xs:attribute name="column" type="xs:int" use="required"/>
    <xs:attribute name="row" type="xs:int" use="required"/>
  </xs:complexType>
</xs:schema>

在带注释的Java代码中,类UIDataConnection扩展了UIData

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "C")
public class Connection
{
    @XmlElement(name = "label")
    private String label;

    @XmlElement(name = "UIData", type = UIDataConnection.class)
    private UIDataConnection uiData;

    @XmlElement(name = "ConnectionType")
    private String connectionType;

    @XmlElement(name = "Description")
    private String description;
}

...

import javax.xml.bind.annotation.XmlAttribute;
public class UIData
{
    @XmlAttribute( name = "column", required = true)
    private int column;

    @XmlAttribute( name = "row", required = true)
    private int row;

}

...

import javax.xml.bind.annotation.XmlElement;

public class UIDataConnection extends UIData
{
    @XmlElement( name = "ConnectionAnchor")
    private String connectionAnchor;

    @XmlElement( name = "ConnectionAnchorOffset")
    private double connectionAnchorOffset;

}

我的package-info.java包含

@XmlSchema(
    namespace = "http://foo/bar/baz",
    elementFormDefault = XmlNsForm.QUALIFIED,
    xmlns={
        @XmlNs(prefix="knsf", namespaceURI="http://foo/bar/thing"),
        @XmlNs(prefix="xsi", namespaceURI = "http://www.w3.org/2001/XMLSchema-instance")
    }
)

XML和架构中有更多UIData元素,但我认为它们在这里不相关。

如何正确处理xsi:type?我真的需要在package-info中定义xsi名称空间吗?

1 个答案:

答案 0 :(得分:0)

您的XML Schema定义了复杂类型uiDataConnection,您的XML文档引用了复杂类型UIDataConnection。您可以使用@XmlType注释控制类对应的类型名称。

@XmlType(name="uiDataConnection")
public class UIDataConnection extends UIData