validate element with attribute xsi:type="xsd:string"

M

Martin Honnen

How do you validate the following XML document, I'm having problems
with element 'one' with the attribute xsi:type="xsd:string"

<?xml version="1.0" encoding="UTF-8"?>
<zero xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<one xsi:type="xsd:string">test</one>
</zero>

To validate you need a schema I think, I don't know any validators
checking the xsi:type attribute alone.
And of course you need to bind the prefix xsd e.g.
<zero
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
 
J

johnsocs

I've been working with the following schema without luck.

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" >

<xsd:element name="zero">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="one" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>

<xsd:element name="one" type="xsd:string"/>

<xsd:complexType name="xsd:string">
<xsd:simpleContent>
<xsd:extension base="xsd:string"/>
</xsd:simpleContent>
</xsd:complexType>
</xsd:schema>
 
M

Martin Honnen

I've been working with the following schema without luck.

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" >

<xsd:element name="zero">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="one" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>

<xsd:element name="one" type="xsd:string"/>

<xsd:complexType name="xsd:string">
<xsd:simpleContent>
<xsd:extension base="xsd:string"/>
</xsd:simpleContent>
</xsd:complexType>

Why are you trying to redefine xsd:string then?
 
J

johnsocs

I'm trying to write a schema to validate the xml document, and I dont
know how to handle the one element. Based on the following I came up
with the previous schema.

<?xml version="1.0" encoding="UTF-8"?>
<zero xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<one xsi:type="A">test</one>
</fullName>

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" >

<xsd:element name="zero">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="one" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>

<xsd:element name="one" type="A"/>

<xsd:complexType name="A">
<xsd:simpleContent>
<xsd:extension base="xsd:string"/>
</xsd:simpleContent>
</xsd:complexType>

</xsd:schema>
 
M

Martin Honnen

I'm trying to write a schema to validate the xml document, and I dont
know how to handle the one element.

The schema is simple but you need to bind the prefix xsd as already
explained:

<?xml version="1.0" encoding="UTF-8"?>
<zero xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="test2005060801Xsd.xml"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<one xsi:type="xsd:string">test</one>
</zero>

then the schema looks as

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="1.0">

<xs:element name="zero">
<xs:complexType>
<xs:sequence>
<xs:element name="one" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>

</xs:schema>
 
J

johnsocs

If you could help me out with more more thing that would be great, you
have been very helpful.

Again trying to build a schema based on the following XML. I
understand how to describe the 'one' element I dont understand how to
describe the soap message within the schema.

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>

<one xsi:type="xsd:string>test</one>

</soapenv:Body>
</soapenv:Envelope>


<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
version="1.0">

<xsd:element name="zero">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="one" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>

</xsd:schema>

Thanks in advance.
 
M

Martin Honnen

If you could help me out with more more thing that would be great, you
have been very helpful.

Again trying to build a schema based on the following XML. I
understand how to describe the 'one' element I dont understand how to
describe the soap message within the schema.

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>

<one xsi:type="xsd:string>test</one>

</soapenv:Body>
</soapenv:Envelope>

You need to have two schemas then as one schema can only describe the
elements in one particular target namespace while your XML instance
above has elements in the namespace
http://schemas.xmlsoap.org/soap/envelope/ and in no namespace.

So a possible solution is

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
targetNamespace="http://schemas.xmlsoap.org/soap/envelope/"
version="1.0">

<xs:import schemaLocation="test2005060902Xsd.xml" />

<xs:element name="Envelope">
<xs:complexType>
<xs:sequence>
<xs:element name="Body">
<xs:complexType>
<xs:sequence>
<xs:element ref="one" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>

</xs:schema>

where the imported schema test2005060902Xsd.xml is

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="1.0">

<xs:element name="one" type="xs:string" />

</xs:schema>

and the instance is

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://schemas.xmlsoap.org/soap/envelope/
test2005060901XSd.xml">
<soapenv:Body>
<one xsi:type="xsd:string">test</one>
</soapenv:Body>
</soapenv:Envelope>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top