Index   Search   Add FAQ   Ask Question  
 

Please note: This page is not maintained anymore. Please visit the new and improved FAQ at http://www.orafaq.com/faq/xml

Oracle XML FAQ

$Date: 13-Jan-2004 $
$Revision: 1.04 $
$Author: Frank Naudé $

Topics

  • What is XML and what is it used for?
  • What is a DTD what is it used for?
  • What is XML Schema and what is it used for?
  • What is DOM and XPATH? What is it used for?
  • What is XSL and what is it used for?
  • What XML compliant products do Oracle provide?
  • How does one install XDK capabilities on an Oracle Database?
  • How does one map relational data from tables to XML?
  • How does one store and extract XML data from Oracle?
  • Can one index XML data within the database?
  • Where can one get more info about XML?

  • Back to Oracle FAQ Index

    What is XML and what is it used for?

    XML (eXtensible Markup Language) is a W3C initiative that allows information and services to be encoded with meaningful structure and semantics that both computers and humans can understand. XML is great for information exchange, and can easily be extended to include user-specified and industry-specified tags. Look at this simple example defining a FAQ:
            <?xml version="1.0"?>
    	<!DOCTYPE question-list SYSTEM "faq.dtd">
            <?xml-stylesheet type="text/xml" href="faq.xsl"?>
    
            <FAQ-LIST>
               <QUESTION>
                    <QUERY>Question goes here</QUERY>
                    <RESPONSE>Answer goes here.</RESPONSE>
               </QUESTION>
               <QUESTION>
                    <QUERY>Another question goes here.</QUERY>
                    <RESPONSE>The answer goes here.</RESPONSE>
               </QUESTION>
            </FAQ-LIST>
    
    

  • Back to top of file

  • What is a DTD and what is it used for?

    A Document Type Definition (DTD) defines the elements or record structure of a XML document. A DTD allows your XML files to carry a description of its format with it. The DTD for the above XML example looks like this:
    	<?xml version="1.0"?>
    
    	<!ELEMENT faq-list (question+)>
    	   <!ELEMENT question (query, response*)>
    	      <!ELEMENT query    (#PCDATA)>
    	      <!ELEMENT response (#PCDATA)>
    
    Notes: The W3C also formulated a new standard, called XML Schemas that superceded DTD's. Schemas allow for more complex data types within your tags and better ways to constrain (validate) data within these tags.

  • Back to top of file

  • What is XML Schema and what is it used for?

    A DTD cannot easily be converted into a CREATE TABLE statement as DTD's only describe character data. The W3C defined a XML language called XML Schema that can better describe data (with propper data types). XML Schemas can easily be converted into CREATE TABLE statments. Look at this example:
       <?xml version="1.0" encoding="UTF-8"?>
       <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"> 
    	<xs:element name="FAQ-LIST">
    		<xs:annotation>
    			<xs:documentation>Example XML schema</xs:documentation>
    		</xs:annotation>
    
    		<xs:complexType>
    			<xs:sequence maxOccurs="unbounded">
    				<xs:element name="QUESTION">
    					<xs:complexType>
    						<xs:sequence>
    							<xs:element name="QUERY"/>
    							<xs:element name="RESPONSE"/>
    						</xs:sequence>
    					</xs:complexType>
    				</xs:element>
    			</xs:sequence>
    		</xs:complexType>
    	</xs:element>
       </xs:schema>
    
    Note: It is quite difficult to compile XML Schemas by hand. Best would be to get a tool like XMLSpy to help you getting the job done.

  • Back to top of file

  • What is DOM and XPATH? What is it used for?

    Document Object Model (DOM) is a tree-structured representation of an XML document. The DOM allows applications to programmatically create and navigate through XML documents. One can also use XPATH to search through your XML documents.

  • Back to top of file

  • What is XSL and what is it used for?

    Extensible Stylesheet Language (XSL) is a language for transforming XML documents into other document formats like HTML. One can say XSL is to XML what CSS (Cascading Style Sheets) is to HTML.

  • Back to top of file

  • What XML compliant products do Oracle provide?

  • Back to top of file

  • How does one install the XDK capabilities on an Oracle Database?

    For Oracle 9i and above:

  • Ensure the Oracle JServer Option is installed. See the JServer/JVM FAQ.

  • Run SQL script $ORACLE_HOME/rdbms/admin/initxml.sql to load the XSK into the database.

    Install steps for older versions (Oracle 8i and below):

  • Ensure the Oracle JServer Option is installed. See the JServer/JVM FAQ.

  • Run SQL script $ORACLE_HOME/rdbms/admin/catxsu.sql to load the XMLSQL Utility (DBMS_XMLQuery) into the database.

  • Note that the following line is commented out in file catxsu.sql:
    -- call sys.dbms_java.loadjava ('-v -r -grant PUBLIC xdk/lib/xmlparserv2.jar');
    Run the above command manually from OS command line:
    $ loadjava -v -r -grant PUBLIC $ORACLE_HOME/xdk/lib/xmlparserv2.jar

  • Load the XMLParser into Oracle by executing $ORACLE_HOME/xdk/plsql/parser/bin/load.sql

  • Grant users access to XMLParser and XMLDom packages:
    	grant execute on xmldom to public;
    	grant execute on xmlparser to public;
    	create public synonym xmldom for sys.xmldom;
    	create public synonym  xmlparser  for sys.xmlparser;
         

  • Use the DBMS_XMLSave, DBMS_XMLQuery, XMLDOM and XMLParser procedures in your programs. See examples below.

  • Back to top of file

  • How does one map relational data from tables to XML?

    If you're using Oracle 8i, use the DBMS_XMLQUERY and DBMS_XMLSAVE JAVA based packages. For Oracle 9i, use the C-based package DBMS_XMLGEN.

    Look at the following Oracle 9i code example:

    	connect scott/tiger
    	set serveroutput on
    	DECLARE
    	        Ctx    DBMS_XMLGEN.ctxHandle;   -- Var's to convert SQL output to XML
    	        xml    clob;
    	        emp_no NUMBER := 7369;
    
    	        xmlc   varchar2(4000);          -- Var's required to convert lob to varchar
    	        off    integer := 1;
    	        len    integer := 4000;
    	BEGIN
    	        Ctx := DBMS_XMLGEN.newContext('SELECT * FROM emp WHERE empno = '||emp_no);
    	        DBMS_XMLGen.setRowsetTag(Ctx, 'EMP_TABLE');
    	        DBMS_XMLGen.setRowTag(Ctx, 'EMP_ROW');
    	        DBMS_XMLGEN.closeContext(Ctx);
    	        xml := DBMS_XMLGEN.getXML(Ctx);
    
    	        DBMS_LOB.READ(xml, len, off, xmlc);   -- Display first part on screen
    	        DBMS_OUTPUT.PUT_LINE(xmlc);
    	END;
    	/
    
    The same results can be achieved using SQLX (see http://sqlx.org/). Some of the SQLX functions are XMLElement(), XMLForest(), XMLSequence(), etc. Look at this example.
    	set long 32000
    	SELECT XMLELEMENT("EMP_TABLE",
    	        (select XMLELEMENT("EMP_ROW",
    	                   XMLFOREST(empno, ename, job, mgr, hiredate, sal, deptno)
    	                )
    	         from   emp
    	         where  empno = 7369))
    	from dual;
    
    An older Oracle 8i example:
    	connect scott/tiger
    	set serveroutput on
    	DECLARE
    		Ctx    DBMS_XMLQuery.ctxType;	-- Var's to convert SQL output to XML
    	        xml    clob;
    		emp_no NUMBER := 7369;
    
    		xmlc   varchar2(4000);		-- Var's required to convert lob to varchar
    		off    integer := 1;
    		len    integer := 4000;
    	BEGIN
    		Ctx := DBMS_XMLQuery.newContext('SELECT * FROM emp WHERE empno = :empno');
    		DBMS_XMLQuery.setBindValue(Ctx, 'empno', emp_no);
    		xml := DBMS_XMLQuery.getXML(Ctx);
    		DBMS_XMLQuery.closeContext(Ctx);
    
    		DBMS_LOB.READ(xml, len, off, xmlc);   -- Display first part on screen
    		DBMS_OUTPUT.PUT_LINE(xmlc);
    	END;
    	/
    

  • Back to top of file

  • How does one store and extract XML data from Oracle?

    XML data can be stored in Oracle (9.2.0 and above) using the XMLType data type. Look at this example:
    	connect scott/tiger
    
    	create table XMLTable (doc_id number, xml_data XMLType);
    
    	insert into XMLTable values (1,
    	        XMLType('<FAQ-LIST>
    	           <QUESTION>
    	                <QUERY>Question 1</QUERY>
    	                <RESPONSE>Answer goes here.</RESPONSE>
    	           </QUESTION>
    	        </FAQ-LIST>'));
    
    	select extractValue(xml_data, '/FAQ-LIST/QUESTION/RESPONSE')  -- XPath expression
    	from   XMLTable
    	where  existsNode(xml_data, '/FAQ-LIST/QUESTION[QUERY="Question 1"]') = 1;
    

  • Back to top of file

  • Can one index XML data loaded into the database?

    Yes, look at this example:
    	create index XMLTable_ind on XMLTable
    	        (extractValue(xml_data, '/FAQ-LIST/QUESTION/QUERY') );
    

  • Back to top of file

  • Where can one get more info about XML?

  • Back to top of file
  • HOME | ASK QUESTION | ADD FAQ | SEARCH | E-MAIL US