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