簡體   English   中英

列表外部具有重復屬性的復雜類型的XSD聲明

[英]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模式的以下內容:

  1. Property元素具有屬性maxOccurs="unbounded" 這表明它是重復元素。
  2. 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM