XML 编组:如何将另一个名称空间中的属性添加到元素

发布于 2024-09-15 20:13:03 字数 464 浏览 1 评论 0原文

我想生成这个 XML:

<myElement myAttribute="whateverstring" xsi:type="hardPart"/>

我有这个 XSD:

<xsd:element name="myElement">
    <xsd:complexType>
        <xsd:attribute name="myAttribute" type="xsd:boolean" />
        <!-- need to add the xsi:attribue here -->
    </xsd:complexType>
</xsd:element>

我到底如何在 XSD 中完成此操作(仅供参考:我使用它使用 JiBX 将对象编组到 Java 中的 XML 中)。

I want to generate this XML:

<myElement myAttribute="whateverstring" xsi:type="hardPart"/>

I have this XSD:

<xsd:element name="myElement">
    <xsd:complexType>
        <xsd:attribute name="myAttribute" type="xsd:boolean" />
        <!-- need to add the xsi:attribue here -->
    </xsd:complexType>
</xsd:element>

How exactly can I accomplish this in my XSD (FYI: I am using it to marshall objects into XML in Java, using JiBX).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

洛阳烟雨空心柳 2024-09-22 20:13:03

假设当您说 xsi:type 时,您指的是“http:// www.w3.org/2001/XMLSchema-instance”命名空间。它不是添加到 XML 模式中的东西,它是限定元素的保留方法(类似于 Java 中的强制转换)。

为了使以下内容有效:

<myElement myAttribute="whateverstring" xsi:type="hardPart"/> 

您需要有一个 XML 模式,如下所示:

<xsd:element name="myElement" type="myElementType"/>  
<xsd:complexType name="myElementType">  
    <xsd:attribute name="myAttribute" type="xsd:boolean" />  
</xsd:complexType>  
<xsd:complexType name="hardPart">
    <xsd:complexContent>
        <xsd:extension base="myElementType">
            ...
        </xsd:extension>
    </xsd:complexContent>
</xsd:complexType>

然后,当您的 XML 绑定解决方案封送与类型“hardPart”对应的对象时,它可能会将其表示为:

<myElement myAttribute="whateverstring" xsi:type="hardPart"/> 

由于 myElement 对应于超类型“myElementType”,并且需要使用 xsi:type="hardPart" 进行限定,以表示内容实际上对应于子类型“hardPart”。

JAXB 示例

MyElementType

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class MyElementType {

    private String myAttribute;

    @XmlAttribute
    public void setMyAttribute(String myAttribute) {
        this.myAttribute = myAttribute;
    }

    public String getMyAttribute() {
        return myAttribute;
    }

}

HardPart

public class HardPart extends MyElementType {

}

演示

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;
import javax.xml.namespace.QName;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(HardPart.class, MyElementType.class);

        HardPart hardPart = new HardPart();
        hardPart.setMyAttribute("whateverstring");
        JAXBElement<MyElementType> jaxbElement = new JAXBElement(new QName("myElement"), MyElementType.class, hardPart);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.marshal(jaxbElement, System.out);
    }
}

Assuming when you say xsi:type, you mean the "type" attribute from the "http://www.w3.org/2001/XMLSchema-instance" namespace. It is not something that you add to your XML schema, it is a reserved means of qualifying an element (similar to a cast in Java).

For the following to be valid:

<myElement myAttribute="whateverstring" xsi:type="hardPart"/> 

You would need to have an XML schema like:

<xsd:element name="myElement" type="myElementType"/>  
<xsd:complexType name="myElementType">  
    <xsd:attribute name="myAttribute" type="xsd:boolean" />  
</xsd:complexType>  
<xsd:complexType name="hardPart">
    <xsd:complexContent>
        <xsd:extension base="myElementType">
            ...
        </xsd:extension>
    </xsd:complexContent>
</xsd:complexType>

Then when your XML binding solution marshals an object corresponding to the type "hardPart" it may represent it as:

<myElement myAttribute="whateverstring" xsi:type="hardPart"/> 

Since myElement corresponds to the super type "myElementType", and needs to be qualified with xsi:type="hardPart" to represent that the content actually corresponds to the subtype "hardPart".

JAXB Example

MyElementType

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class MyElementType {

    private String myAttribute;

    @XmlAttribute
    public void setMyAttribute(String myAttribute) {
        this.myAttribute = myAttribute;
    }

    public String getMyAttribute() {
        return myAttribute;
    }

}

HardPart

public class HardPart extends MyElementType {

}

Demo

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;
import javax.xml.namespace.QName;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(HardPart.class, MyElementType.class);

        HardPart hardPart = new HardPart();
        hardPart.setMyAttribute("whateverstring");
        JAXBElement<MyElementType> jaxbElement = new JAXBElement(new QName("myElement"), MyElementType.class, hardPart);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.marshal(jaxbElement, System.out);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文