例如,下面的Java代码,将属性currency映射到了XML的属性currency:

package com.thb;

import jakarta.xml.bind.annotation.XmlAttribute;
import jakarta.xml.bind.annotation.XmlRootElement;
import jakarta.xml.bind.annotation.XmlType;
import jakarta.xml.bind.annotation.XmlValue;

@XmlRootElement
@XmlType(name = "", propOrder = {"value"})
public class Price {

    @XmlValue
    public String value;

    @XmlAttribute(name = "currency")
    public String currency;
}

生成的XML Schema

<?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;

  <xs:element name="price"&gt;
    <xs:complexType&gt;
      <xs:simpleContent&gt;
        <xs:extension base="xs:string"&gt;
          <xs:attribute name="currency" type="xs:string"/&gt;
        </xs:extension>
      </xs:simpleContent>
    </xs:complexType>
  </xs:element>
</xs:schema>
package com.thb;

import jakarta.xml.bind.annotation.XmlAttribute;
import jakarta.xml.bind.annotation.XmlRootElement;
import jakarta.xml.bind.annotation.XmlType;
import jakarta.xml.bind.annotation.XmlValue;

@XmlRootElement
@XmlType(name = "", propOrder = {"value"})
public class Price {

    @XmlValue
    public String value;

    @XmlAttribute(name = "currency", required = true)
    public String currency;
}

生成的XML Schema,属性currency中出现了use="required"

 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="price">
    <xs:complexType>
      <xs:simpleContent>
        <xs:extension base="xs:string">
          <xs:attribute name="currency" type="xs:string" use="required"/>
        </xs:extension>
      </xs:simpleContent>
    </xs:complexType>
  </xs:element>
</xs:schema>

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注