如何在XSD中向元素添加属性

时间:2022-05-15 17:17:13

I'm using jibx to create a xml. the requirement i have is to get Xml as below

我正在使用jibx来创建一个xml。我的要求是得到如下的Xml

<report>
  <info>
   <meta name="acntNo">11111111</meta> 
   <meta name="location">USA</meta> 
   <meta name="Id">2222222222</meta> 
  </info>
</report>

My Question is how to add name attribute to the complexElement meta. I'll get the values of name attribute and meta text from java code.

我的问题是如何将name属性添加到complexElement元。我将从java代码中获取name属性和元文本的值。

I tried using

我试过用

<xsd:complexType name="CareInfoType">
    <xsd:sequence>
        <!--  root of the meta -->
        <xsd:element name="meta" type="qdx:CareMetaInfo" minOccurs="1" maxOccurs="3">
        </xsd:element>
    </xsd:sequence>
</xsd:complexType>

<xsd:complexType name="CareMetaInfo">
    <xsd:attribute name="name" type="xsd:string" ></xsd:attribute>
</xsd:complexType>

Thanks in advance

提前致谢

1 个解决方案

#1


1  

You should use xsd:simpleContent mechanism to add an attribute to an element that can contain values of simple types. You can read the tutorial here. Below is another example

您应该使用xsd:simpleContent机制将属性添加到可以包含简单类型值的元素。你可以在这里阅读教程。下面是另一个例子

Schema

架构

<xsd:complexType name="SizeType">
  <xsd:simpleContent>
    <xsd:extension base="xsd:integer">
      <xsd:attribute name="system" type="xsd:token"/>
    </xsd:extension>
  </xsd:simpleContent>
</xsd:complexType>

Example

<size system="US-DRESS">10</size>

#1


1  

You should use xsd:simpleContent mechanism to add an attribute to an element that can contain values of simple types. You can read the tutorial here. Below is another example

您应该使用xsd:simpleContent机制将属性添加到可以包含简单类型值的元素。你可以在这里阅读教程。下面是另一个例子

Schema

架构

<xsd:complexType name="SizeType">
  <xsd:simpleContent>
    <xsd:extension base="xsd:integer">
      <xsd:attribute name="system" type="xsd:token"/>
    </xsd:extension>
  </xsd:simpleContent>
</xsd:complexType>

Example

<size system="US-DRESS">10</size>