[英]JAXB problems with schema validation and xsi:type
我正在嘗試使用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名稱空間嗎?
您的XML模式定義了復雜類型uiDataConnection
而XML文檔引用了復雜類型UIDataConnection
。 您可以使用@XmlType
批注控制類對應的類型名稱。
@XmlType(name="uiDataConnection")
public class UIDataConnection extends UIData
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.