[英]cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'util:list
[英]XSD declaration of a complex type with repeating attribute outside a list
我有一個提供者輸出類似這樣的東西
<Result rawScore="623">
<Target>http://myUrl.com/test1</Target>
<Domain name="myUrl.search.com" />
<Property name="Language">en</Property>
<Property name="Teaser">This is the description</Property>
<Property name="LVCategory">Help</Property>
<Property name="Title">ProductTitle</Property>
<Property name="Last Modified">2012-04-06T21:44:11Z</Property>
</Result>
我正在嘗試創建一個xsd以利用jaxb,但是我不確定如何處理多次出現但不在列表中的Property屬性,因此序列將無法工作。 有任何想法嗎?
這是一個完整的XML模式,並編譯為Java代碼
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" version="2.0">
<xsd:element name="Result" type="ResultType"/>
<xsd:complexType name="ResultType">
<xsd:sequence>
<xsd:element name="Target" type="xsd:string"/>
<xsd:element name="Domain" type="xsd:string"/>
<xsd:element name="Property" type="PropertyType"
minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
<xsd:attribute name="rawScore" type="xsd:int"/>
<!-- xsd:integer => BigDecimal/PITA -->
</xsd:complexType>
<!-- I prefer explicit types to avoid nested class definitions -->
<xsd:complexType name="PropertyType">
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="name" type="xsd:string"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:schema>
幾行Java代碼:
JAXBContext jc = JAXBContext.newInstance( PACKAGE );
Unmarshaller m = jc.createUnmarshaller();
try {
File source = new File( XMLIN );
JAXBElement<ResultType> jbe = (JAXBElement<ResultType>)m.unmarshal( source );
ResultType result = (ResultType)jbe.getValue();
} catch( Exception e ){
}
您可以執行以下操作:
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema">
<element name="Result">
<complexType>
<sequence>
...
<element name="Property" minOccurs="0" maxOccurs="unbounded">
<complexType>
<simpleContent>
<extension base="string">
<attribute name="name" type="string"/>
</extension>
</simpleContent>
</complexType>
</element>
</sequence>
</complexType>
</element>
</schema>
請注意有關XML模式的以下內容:
Property
元素具有屬性maxOccurs="unbounded"
。 這表明它是重復元素。 Property
元素是具有簡單內容的復雜類型。 這意味着它可以具有文本值和XML屬性。 使用以下方法聲明Result
的類型:
<xsd:complexType name="ResultType">
<xsd:sequence>
<xsd:element ref="Target"/>
<xsd:element ref="Domain"/>
<xsd:element ref="Property"
minOccurs="0"
maxOccurs="unbounded"/>
</xsd:sequence>
<xsd:attribute name="rawScore" type="xsd:integer"/>
</xsd:complexType>
並了解元素和屬性之間的區別,好嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.