I am trying to use xsd:any to allow arbitrary unqualified XML code below an
element.
However, doing so leads to validation failure (sample xml, xsd and error see
below).
What am I doing wrong? Any help would be greatly appreciated.
Thanks
dpomt
person.xsd:
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema elementFormDefault="qualified" targetNamespace="TEST"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string" />
<xs:element name="lastname" type="xs:string" />
<xs:choice minOccurs="0" maxOccurs="1">
<xs:any minOccurs="0" namespace="##local" processContents="lax" />
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
sample xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<person xmlns="TEST" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="TEST person.xsd">
<firstname>Hege</firstname>
<lastname>Refsnes</lastname>
<childrenXx>
<childname>Cecilie</childname>
</childrenXx>
</person>
Validation error:
In content of element <person>: The content model does not allow element
<childrenXx> to appear here. Expected: anyOf{##local}
From your XML schema you use the xs:choice element which wrapper the xs:any
element, xs:choice may expect a given set of elements(and choose from
them), those elements is likely to be well definted one instead of any
possible XML elements. Have you tried xml:all or just using xs:sequence as
the wrapper element around xs:any to see whether it works?
Sincerely,
Steven Cheng
Microsoft MSDN Online Support Lead
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msd...@microsoft.com.
--------------------
>From: =?Utf-8?B?ZHBvbXQ=?= <dpo...@nospam.nospam>
>Subject: Issue validating xml against xsd with xsd:any
>Date: Wed, 30 Dec 2009 16:41:01 -0800
> <?xml version="1.0" encoding="utf-8" ?>
> <xs:schema elementFormDefault="qualified" targetNamespace="TEST"
> xmlns:xs="http://www.w3.org/2001/XMLSchema">
> <xs:element name="person">
> <xs:complexType>
> <xs:sequence>
> <xs:element name="firstname" type="xs:string" />
> <xs:element name="lastname" type="xs:string" />
> <xs:choice minOccurs="0" maxOccurs="1">
> <xs:any minOccurs="0" namespace="##local" processContents="lax" />
##local means in _no namespace_, if you want to allow elements in the
targetNamespace then you need to use
namespace="##targetNamespace"
on the xs:any element.
You could however get other problems that way, in particular ambiguity.
--
Martin Honnen --- MVP XML
http://msmvps.com/blogs/martin_honnen/
thanks for your post.
Using xml:all or xml:sequence does not change the described behavior.
Best regards
dpomt
""Steven Cheng"" wrote:
> .
>
thanks for your post.
> ##local means in _no namespace_, if you want to allow elements in the
> targetNamespace then you need to use
> namespace="##targetNamespace"
> on the xs:any element.
just changing ##local to ##targetNamespace still lead to same validation
error.
The solution was to add an additional namespace that contains the childrenXx
element:
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema elementFormDefault="qualified" targetNamespace="TEST"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="childrenXx">
<xs:complexType>
<xs:sequence>
<xs:element name="childname" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
and the XML:
<?xml version="1.0" encoding="ISO-8859-1"?>
<person xmlns="TEST" xmlns:add="TEST.EXTENSION"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="TEST person.xsd TEST.EXTENSION add.xsd">
<firstname>Hege</firstname>
<lastname>Refsnes</lastname>
<childrenXx>
<childname>Cecilie</childname>
</childrenXx>
</person>
It is possible to have arbitrary XML tags with xsd:any without defining them
in a (separate) XSD?
Thanks,
dpomt