如何在xsd中添加元素级别的属性

时间:2014-03-06 10:45:07

标签: xml xsd

我想使用xsd

生成这种xml
<UnassignedSecurityRoleUsers>
    <UserName errorCode= "1" errorMessage="">?</UserName>
    <UserName errorCode= "1" errorMessage="">?</UserName>
    ....
    ....
</UnassignedSecurityRoleUsers>

我先尝试过这个

<xsd:element name="UnassignedSecurityRoleUsers" type="UnassignedSecurityRoleUsers"/>
<xsd:complexType name="UnassignedSecurityRoleUsers">  
    <xsd:sequence>  
        <xsd:element name="UnassignedUsers" type="UnassignedUsers" minOccurs="1" maxOccurs="unbounded" />
    </xsd:sequence> 
</xsd:complexType>

<xsd:element name="UnassignedUsers" type="UnassignedUsers"/>  
<xsd:complexType name="UnassignedUsers">  
    <xsd:sequence>  
        <xsd:element name="UserName" type="xsd:string" minOccurs="1" maxOccurs="unbounded" />
    </xsd:sequence> 
    <xsd:attribute name="errorCode" type="xsd:string" />
    <xsd:attribute name="errorMessage" type="xsd:string" /> 
</xsd:complexType>

但这会生成类似这样的内容

<UnassignedSecurityRoleUsers>  
    <UnassignedUsers errorCode="" errorMessage="" >
        <UserName></UserName>
    </UnassignedUsers>  
    <UnassignedUsers errorCode="" errorMessage="" >
        <UserName></UserName>
    </UnassignedUsers>
...
....
</UnassignedSecurityRoleUsers>

如果我使用

<xsd:element name="UnassignedSecurityRoleUsers" type="UnassignedSecurityRoleUsers"/>
    <xsd:complexType name="UnassignedSecurityRoleUsers">  
    <xsd:sequence>  
        <xsd:element name="UserName" type="xsd:string" minOccurs="1" maxOccurs="unbounded" />
    </xsd:sequence> 
    <xsd:attribute name="errorCode" type="xsd:string" />
    <xsd:attribute name="errorMessage" type="xsd:string" />
</xsd:complexType>

然后这将生成

<UnassignedSecurityRoleUsers errorCode="" errorMessage="">  
<UserName></UserName>
</UnassignedSecurityRoleUsers>  

我如何使用xsd

生成此内容
<UnassignedSecurityRoleUsers>
    <UserName errorCode= "1" errorMessage="">?</UserName>
    <UserName errorCode= "1" errorMessage="">?</UserName>
    ....
    ....
</UnassignedSecurityRoleUsers>

由于

1 个答案:

答案 0 :(得分:1)

如果您提供UserName元素type="xsd:string",则它不能拥有属性。要允许两者文本内容,您需要为元素complexType添加simpleContent 扩展 xsd:string,并声明该类型的属性而不是UnassignedSecurityRoleUsers类型:

<xsd:element name="UnassignedSecurityRoleUsers" type="UnassignedSecurityRoleUsers"/>

<xsd:complexType name="UnassignedSecurityRoleUsers">
  <xsd:sequence>  
    <xsd:element name="UserName" minOccurs="1" maxOccurs="unbounded">
      <xsd:complexType>
        <xsd:simpleContent>
          <xsd:extension base="xs:string">
            <xsd:attribute name="errorCode" type="xsd:string" />
            <xsd:attribute name="errorMessage" type="xsd:string" />
          </xsd:extension>
        </xsd:simpleContent>
      </xsd:complexType>
    </xsd:element>
  </xsd:sequence> 
</xsd:complexType>

如果您希望能够声明具有相同属性的其他元素,那么您可以使用命名的complexType而不是将其嵌套在UserName元素中:

<xsd:complexType name="valueWithError">
  <xsd:simpleContent>
    <xsd:extension base="xs:string">
      <xsd:attribute name="errorCode" type="xsd:string" />
      <xsd:attribute name="errorMessage" type="xsd:string" />
    </xsd:extension>
  </xsd:simpleContent>
</xsd:complexType>

<xsd:complexType name="UnassignedSecurityRoleUsers">
  <xsd:sequence>  
    <xsd:element name="UserName" minOccurs="1" maxOccurs="unbounded"
                 type="valueWithError"/>
  </xsd:sequence>
</xsd:complexType>