我想使用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>
由于
答案 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>