Using type
(local definition)
Here, we define elements directly with their datatypes.
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://example.com/auth"
xmlns="http://example.com/auth"
elementFormDefault="qualified">
<!-- Local elements -->
<xs:element name="UserName" type="xs:string"/>
<xs:element name="Password" type="xs:string"/>
</xs:schema>
✅ XML Output (local namespace elements):
<auth:UserName xmlns:auth="http://example.com/auth">usr</auth:UserName>
<auth:Password xmlns:auth="http://example.com/auth">pwd</auth:Password>
Using ref
(reusing global elements)
First, define global reusable elements in a common schema.
common.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://example.com/common"
xmlns="http://example.com/common"
elementFormDefault="qualified">
<!-- Global elements -->
<xs:element name="MessageId" type="xs:string"/>
<xs:element name="Timestamp" type="xs:string"/>
</xs:schema>
Now, reference those in the service schema.
service.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://example.com/service"
xmlns:svc="http://example.com/service"
xmlns:common="http://example.com/common"
elementFormDefault="qualified">
<!-- Import the common schema -->
<xs:import namespace="http://example.com/common" schemaLocation="common.xsd"/>
<!-- Reference global elements -->
<xs:element ref="common:MessageId"/>
<xs:element ref="common:Timestamp"/>
</xs:schema>
✅ XML Output (reused common namespace elements):
<common:MessageId xmlns:common="http://example.com/common">12345</common:MessageId>
<common:Timestamp xmlns:common="http://example.com/common">2025-09-03T10:00:00</common:Timestamp>
3. Combined SOAP Example
Both styles used together in a real request:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:auth="http://example.com/auth"
xmlns:common="http://example.com/common">
<soapenv:Header/>
<soapenv:Body>
<auth:LoginRequest>
<!-- Local elements (defined with type) -->
<auth:UserName>usr</auth:UserName>
<auth:Password>pwd</auth:Password>
<!-- Global reusable elements (referenced with ref) -->
<common:MessageId>12345</common:MessageId>
<common:Timestamp>2025-09-03T10:00:00</common:Timestamp>
</auth:LoginRequest>
</soapenv:Body>
</soapenv:Envelope>
✅ Summary
type
→ Local definition, used for service-specific fields (UserName
,Password
).ref
→ Reference to global elements, used for shared fields (MessageId
,Timestamp
).
This way you can mix both:
- Use
type
for fields unique to a service. - Use
ref
for fields that must come from a common namespace.
No comments:
Post a Comment