简体   繁体   English

列表外部具有重复属性的复杂类型的XSD声明

[英]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. 我正在尝试创建一个xsd以利用jaxb,但是我不确定如何处理多次出现但不在列表中的Property属性,因此序列将无法工作。 Any ideas? 有任何想法吗?

This is a complete XML Schema, and compiles to Java code 这是一个完整的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>

A few lines of Java code: 几行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 ){
}

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: 请注意有关XML模式的以下内容:

  1. The Property element has the attribute maxOccurs="unbounded" . Property元素具有属性maxOccurs="unbounded" This indicates that it is a repeating element. 这表明它是重复元素。
  2. The Property element is a complex type with simple content. Property元素是具有简单内容的复杂类型。 This means it can have a text value and XML attributes. 这意味着它可以具有文本值和XML属性。

Declare the type of Result using something like this: 使用以下方法声明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>

And learn the difference between elements and attributes, OK? 并了解元素和属性之间的区别,好吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 cvc-complex-type.2.4.c:匹配的通配符很严格,但是找不到元素&#39;util:list的声明 - cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'util:list 在Apache XmlBeans中编写xsd:type属性 - Write xsd:type attribute in Apache XmlBeans 在 xsd 中定义枚举类型属性的默认值 - defining default value of enumeration type attribute in xsd xsd:自定义类型列表生成到List <String>中 - xsd:list of custom type is generated into List<String> 解析XSD文件以从复杂类型获取元素名称 - Parse XSD file to get element names from complex type 针对XSD的XML验证:cvc-complex-type.2.4.a - XML Validation against XSD : cvc-complex-type.2.4.a 如何以编程方式更新并将带序列的复杂类型添加到XSD - How to Programmatically Update and Add a Complex Type with a Sequence to an XSD cvc-complex-type.3.2.2:不允许属性“ xsi:schemalocation”出现在元素“ jtis_journey_list”中 - cvc-complex-type.3.2.2: Attribute 'xsi:schemalocation' is not allowed to appear in element 'jtis_journey_list' Spring Data + Mongo列表使用id作为mongo中的属性嵌套复杂类型 - Spring Data + Mongo list nested complex type using id as attribute in mongo 关于响应XML中类型属性的XSD模式定义 - regarding xsd schema defination of type attribute in response xml
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM