[castor-dev] XML Validation with its XSD Schema when marshalling and unmarshalling

J p <[email protected]> Mon, 28 May 2012 06:24:14 +0530
Newsgroups gmane.comp.java.castor.devel
Message-ID <CAGDpBkennWd-1F19uL+07t1q5hi6qrbLtbeq5TALPJh+WX4iTg@mail.gmail.com>
--047d7b33dbec32873204c10e2839
Content-Type: text/plain; charset=ISO-8859-1

Hi Werner,

i neeed to validate pojo xml ( not mapping xml ) with its xsd when
marshalling and unmarshalling with castor. How can i validate xml with xsd
on castor?

am working on this since last last week. below is xml and xsd files.

Book.xml :

<?xml version="1.0" encoding="UTF-8"?>

<books xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:NamespaceSchemaLocation='books.xsd'>
   <book id="bk001">
      <author>Writer</author>
      <title>The First Book</title>
      <genre>Fiction</genre>
      <price>44.95</price>
      <pub_date>2000-10-01</pub_date>
      <review>An amazing story of nothing.</review>
   </book>

   <book id="bk002">
      <author>Poet</author>
      <title>The Poet's First Poem</title>
      <genre>Poem</genre>
      <price>24.95</price>
      <review>Least poetic poems.</review>
   </book>
</books>


XSD :

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

  <xsd:element name="books" type="bks:BooksForm"/>

  <xsd:complexType name="BooksForm">
    <xsd:sequence>
      <xsd:element name="book"
                  type="bks:BookForm"
                  minOccurs="0"
                  maxOccurs="unbounded"/>
      </xsd:sequence>
  </xsd:complexType>

  <xsd:complexType name="BookForm">
    <xsd:sequence>
      <xsd:element name="author"   type="xsd:string"/>
      <xsd:element name="title"    type="xsd:string"/>
      <xsd:element name="genre"    type="xsd:string"/>
      <xsd:element name="price"    type="xsd:float" />
      <xsd:element name="pub_date" type="xsd:date" />
      <xsd:element name="review"   type="xsd:string"/>
    </xsd:sequence>
    <xsd:attribute name="id"   type="xsd:string"/>
  </xsd:complexType>
</xsd:schema>

Thanks,
John

--047d7b33dbec32873204c10e2839
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Hi Werner,<br><br>i neeed to validate pojo xml ( not mapping xml ) with its=
 xsd when marshalling and unmarshalling with castor. How can i validate xml=
 with xsd on castor?<br><br>am working on this since last last week. below =
is xml and xsd files. <br>
<br>Book.xml :<br><br>&lt;?xml version=3D&quot;1.0&quot; encoding=3D&quot;U=
TF-8&quot;?&gt;<br><br>&lt;books xmlns:xsi=3D&quot;<a href=3D"http://www.w3=
.org/2001/XMLSchema-instance">http://www.w3.org/2001/XMLSchema-instance</a>=
&quot;<br>
=A0=A0=A0 =A0=A0 xsi:NamespaceSchemaLocation=3D&#39;books.xsd&#39;&gt;<br>=
=A0=A0 &lt;book id=3D&quot;bk001&quot;&gt;<br>=A0=A0=A0=A0=A0 &lt;author&gt=
;Writer&lt;/author&gt;<br>=A0=A0=A0=A0=A0 &lt;title&gt;The First Book&lt;/t=
itle&gt;<br>=A0=A0=A0=A0=A0 &lt;genre&gt;Fiction&lt;/genre&gt;<br>
=A0=A0=A0=A0=A0 &lt;price&gt;44.95&lt;/price&gt;<br>=A0=A0=A0=A0=A0 &lt;pub=
_date&gt;2000-10-01&lt;/pub_date&gt;<br>=A0=A0=A0=A0=A0 &lt;review&gt;An am=
azing story of nothing.&lt;/review&gt;<br>=A0=A0 &lt;/book&gt;<br><br>=A0=
=A0 &lt;book id=3D&quot;bk002&quot;&gt;<br>
=A0=A0=A0=A0=A0 &lt;author&gt;Poet&lt;/author&gt;<br>=A0=A0=A0=A0=A0 &lt;ti=
tle&gt;The Poet&#39;s First Poem&lt;/title&gt;<br>=A0=A0=A0=A0=A0 &lt;genre=
&gt;Poem&lt;/genre&gt;<br>=A0=A0=A0=A0=A0 &lt;price&gt;24.95&lt;/price&gt;<=
br>=A0=A0=A0=A0=A0 &lt;review&gt;Least poetic poems.&lt;/review&gt;<br>
=A0=A0 &lt;/book&gt;<br>&lt;/books&gt;<br><br><br>XSD :<br><br>&lt;?xml ver=
sion=3D&quot;1.0&quot; encoding=3D&quot;UTF-8&quot;?&gt;<br>&lt;xsd:schema =
xmlns:xsd=3D&quot;<a href=3D"http://www.w3.org/2001/XMLSchema">http://www.w=
3.org/2001/XMLSchema</a>&quot;<br>
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 targetNamespace=3D&quot;urn:books&quot;<b=
r>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 xmlns:bks=3D&quot;urn:books&quot;&gt;<b=
r><br>=A0 &lt;xsd:element name=3D&quot;books&quot; type=3D&quot;bks:BooksFo=
rm&quot;/&gt;<br><br>=A0 &lt;xsd:complexType name=3D&quot;BooksForm&quot;&g=
t;<br>
=A0=A0=A0 &lt;xsd:sequence&gt;<br>=A0=A0=A0=A0=A0 &lt;xsd:element name=3D&q=
uot;book&quot; <br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 type=
=3D&quot;bks:BookForm&quot; <br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=
=A0=A0=A0 minOccurs=3D&quot;0&quot; <br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=
=A0=A0=A0=A0=A0=A0 maxOccurs=3D&quot;unbounded&quot;/&gt;<br>
=A0=A0=A0=A0=A0 &lt;/xsd:sequence&gt;<br>=A0 &lt;/xsd:complexType&gt;<br><b=
r>=A0 &lt;xsd:complexType name=3D&quot;BookForm&quot;&gt;<br>=A0=A0=A0 &lt;=
xsd:sequence&gt;<br>=A0=A0=A0=A0=A0 &lt;xsd:element name=3D&quot;author&quo=
t;=A0=A0 type=3D&quot;xsd:string&quot;/&gt;<br>
=A0=A0=A0=A0=A0 &lt;xsd:element name=3D&quot;title&quot;=A0=A0=A0 type=3D&q=
uot;xsd:string&quot;/&gt;<br>=A0=A0=A0=A0=A0 &lt;xsd:element name=3D&quot;g=
enre&quot;=A0=A0=A0 type=3D&quot;xsd:string&quot;/&gt;<br>=A0=A0=A0=A0=A0 &=
lt;xsd:element name=3D&quot;price&quot;=A0=A0=A0 type=3D&quot;xsd:float&quo=
t; /&gt;<br>
=A0=A0=A0=A0=A0 &lt;xsd:element name=3D&quot;pub_date&quot; type=3D&quot;xs=
d:date&quot; /&gt;<br>=A0=A0=A0=A0=A0 &lt;xsd:element name=3D&quot;review&q=
uot;=A0=A0 type=3D&quot;xsd:string&quot;/&gt;<br>=A0=A0=A0 &lt;/xsd:sequenc=
e&gt;<br>=A0=A0=A0 &lt;xsd:attribute name=3D&quot;id&quot;=A0=A0 type=3D&qu=
ot;xsd:string&quot;/&gt;<br>
=A0 &lt;/xsd:complexType&gt;<br>&lt;/xsd:schema&gt;<br><br>Thanks,<br>John<=
br>

--047d7b33dbec32873204c10e2839--