例如,下面的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>

原文地址:https://blog.csdn.net/panghuangang/article/details/134642457

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_12443.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

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