• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1090
  • Last Modified:

Validate XML file aginset spicific XSD

please help in Validating XML file aginst XSD file by PL/SQL and the result of validation if not sucessful to display the error,
Note 1. the XML file stored in physical file
        2. XSD already registered

Thank You
0
malek_eng
Asked:
malek_eng
3 Solutions
 
slightwv (䄆 Netminder) Commented:
0
 
slightwv (䄆 Netminder) Commented:
for_yan,

Did you look at that link before posting it after you Googled it?   They want to use Oracle's PL/SQL to validate the XML.  That link only talks about 3rd party tools.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
malek_engAuthor Commented:
< I need Sample code  >
0
 
objectsCommented:
0
 
slightwv (䄆 Netminder) Commented:
Before I can provide code, I need more about the requirements.

How are you planning on getting the XML file from the filesystem into the Oracle stored procedure?
0
 
malek_engAuthor Commented:
Thanks a lot for you concerned
i use l_bfile := bfilename('XML_dir', file_name.xml);
and the XSD registered
Note : i dont' have Deep knowledge in PL/SQL
0
 
malek_engAuthor Commented:
i needed in Java or PL/SQL preffered to be in PL/SQL
0
 
slightwv (䄆 Netminder) Commented:
>>preffered to be in PL/SQL

Let me see what I can come up with.
0
 
slightwv (䄆 Netminder) Commented:
Quick and dirty.

I borrowed the XML and XSD from: http://en.wikipedia.org/wiki/XML_Schema_%28W3C%29

I borrowed the PL/SQL from:
http://forums.oracle.com/forums/thread.jspa?threadID=373116

Below you'll find the xsd (I named q.xsd).
a 'good' xml file and a 'bad' xml file ('XX' in country).

The register script and an anonymous PL/SQL block that runs it all.
declare
	xmldoc xmltype;
begin
    
    --Good
	xmldoc := (XMLType(bfilename('XMLDIR', 'q_good.xml'), nls_charset_id('AL32UTF8')));
	xmldoc := xmldoc.createSchemaBasedXML('http://xmlns.oracle.com/xdb/documentation/q.xsd');
	if xmldoc.isSchemaValid() = 1 then
		dbms_output.put_line('Valid');
	else
		dbms_output.put_line('InValid');
		xmldoc.schemaValidate();
	end if;

    --Bad
	xmldoc := (XMLType(bfilename('XMLDIR', 'q_bad.xml'), nls_charset_id('AL32UTF8')));
	xmldoc := xmldoc.createSchemaBasedXML('http://xmlns.oracle.com/xdb/documentation/q.xsd');
	if xmldoc.isSchemaValid() = 1 then
		dbms_output.put_line('Valid');
	else
		dbms_output.put_line('InValid');
		xmldoc.schemaValidate();
	end if;
end;
/

Open in new window

drop directory XMLDIR;
create directory XMLDIR as 'c:\';

BEGIN
  DBMS_XMLSCHEMA.registerSchema(
    SCHEMAURL => 'http://xmlns.oracle.com/xdb/documentation/q.xsd',
    SCHEMADOC => bfilename('XMLDIR','q.xsd'));
END;
/

Open in new window

<?xml version="1.0" encoding="utf-8"?>
<Address xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Recipient>Mr. Walter C. Brown</Recipient>
  <House>49</House>
  <Street>Featherstone Street</Street>
  <Town>LONDON</Town>
  <PostCode>EC1Y 8SY</PostCode>
  <Country>XX</Country>
</Address>

Open in new window

<?xml version="1.0" encoding="utf-8"?>
<Address xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Recipient>Mr. Walter C. Brown</Recipient>
  <House>49</House>
  <Street>Featherstone Street</Street>
  <Town>LONDON</Town>
  <PostCode>EC1Y 8SY</PostCode>
  <Country>UK</Country>
</Address>

Open in new window

<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Address">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Recipient" type="xs:string" />
        <xs:element name="House" type="xs:string" />
        <xs:element name="Street" type="xs:string" />
        <xs:element name="Town" type="xs:string" />
        <xs:element name="County" type="xs:string" minOccurs="0" />
        <xs:element name="PostCode" type="xs:string" />
        <xs:element name="Country">
          <xs:simpleType>
            <xs:restriction base="xs:string">
              <xs:enumeration value="FR" />
              <xs:enumeration value="DE" />
              <xs:enumeration value="ES" />
              <xs:enumeration value="UK" />
              <xs:enumeration value="US" />
            </xs:restriction>
          </xs:simpleType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Open in new window

0
 
malek_engAuthor Commented:
Many Thanks for your  effort .
If possible , Please to send to me how I can know the failed area of XML file or in which node not succeeded   such as printing the out put of validation
0
 
slightwv (䄆 Netminder) Commented:
I think the error is directly related to the 'type' of failure.  If you tweak the 'bad' XML to the one below, you get a more descriptive error message.


Here is the error I receive:
declare
*
ERROR at line 1:
ORA-30936: Maximum number (1) of 'County' XML node elements exceeded
ORA-06512: at "SYS.XMLTYPE", line 345
ORA-06512: at line 7
<?xml version="1.0" encoding="utf-8"?>
<Address xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Recipient>Mr. Walter C. Brown</Recipient>
  <House>49</House>
  <Street>Featherstone Street</Street>
  <Town>LONDON</Town>
  <County>XX</County>
  <County>YY</County>
  <PostCode>EC1Y 8SY</PostCode>
  <Country>UK</Country>
</Address>

Open in new window

0
 
malek_engAuthor Commented:
Many Thanks for the effort and help
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now