Xml 如何为复杂类型元素设置默认/固定值?
元素名是“category”,我希望它始终默认为值“ABC” 上述定义在Soap UI中生成元素,如下所示:Xml 如何为复杂类型元素设置默认/固定值?,xml,xsd,Xml,Xsd,元素名是“category”,我希望它始终默认为值“ABC” 上述定义在Soap UI中生成元素,如下所示: <xsd:element name="category"> <xsd:complexType> <xsd:simpleContent> <xsd:extension base="xsd:string"> <xsd:attribute default="http://schemas
<xsd:element name="category">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute default="http://schemas.xmlsoap.org/soap/actor/next" ref="soapenv:actor" use="optional"/>
<xsd:attribute ref="soapenv:mustUnderstand" use="optional"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
?
我需要的是,元素应该作为
<urn:category soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next" soapenv:mustUnderstand="?">?</urn:category>
ABC
为了做到这一点,我尝试了
如何实现这一点?从XSD的角度来看,您尝试指定默认值或固定值的方法是正确的,对我来说,它们似乎工作得很好。也就是说,如果我创建了一个包含您给出的元素声明的模式文档,并且在
xsd:element
元素上有附加属性fixed=“ABC”
,那么文档ABC
对模式有效,而文档ABC
无效,并引发对值与所需值“ABC”不匹配的投诉
令人痛苦的是,这是我使用的完整模式文档:
<urn:category soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next" soapenv:mustUnderstand="?">ABC</urn:category>
也许问题出在您的SOAP工具上,而不是XSD模式文档。谢谢您的回复。这个东西适用于一个简单的元素。但是,在我的例子中,元素是带有simpleContent的complexType。你能把为你工作的元素的完整定义粘贴在这里吗。
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soapenv="http://example.com/soapenv"
elementFormDefault="qualified">
<xsd:import namespace="http://example.com/soapenv"
schemaLocation="complex-default.2.xsd"/>
<xsd:element name="category" fixed="ABC">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute
default="http://schemas.xmlsoap.org/soap/actor/next"
ref="soapenv:actor"
use="optional"/>
<xsd:attribute ref="soapenv:mustUnderstand"
use="optional"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://example.com/soapenv"
elementFormDefault="qualified">
<xs:attribute name="actor" type="xs:anyURI"/>
<xs:attribute name="mustUnderstand" type="xs:boolean"/>
</xs:schema>