简体   繁体   中英

XSD declaration of a complex type with repeating attribute outside a list

I have a provider that outputs something like this

<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>

I'm trying to create an xsd to leverage jaxb, but I'm not sure how to handle the Property attribute appearing several times but not inside a list, so a sequence won't work. Any ideas?

This is a complete XML Schema, and compiles to Java code

<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>

A few lines of Java code:

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 ){
}

You could do something like the following:

<?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>

Note the following things about the XML Schema:

  1. The Property element has the attribute maxOccurs="unbounded" . This indicates that it is a repeating element.
  2. The Property element is a complex type with simple content. This means it can have a text value and XML attributes.

Declare the type of Result using something like this:

<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>

And learn the difference between elements and attributes, OK?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM