Sybase Technical Library - Product Manuals Home
[Search Forms] [Previous Section with Hits] [Next Section with Hits] [Clear Search] Expand Search

Chapter 3 XML Language and XML Query Language [Table of Contents] Chapter 5 XML Mappings

XML Services in Adaptive Server Enterprise

[-] Chapter 4 XML Mapping Functions

Chapter 4

XML Mapping Functions

This chapter describes the XML mapping functions in detail, and provides examples for them.

for xml clause

Specifies a SQL select statement that returns an XML representation of the result set.

Syntax

select ::=
     select [ all | distinct ] select_list
     [into_clause ]
     [where_clause ]
     [group_by_clause ]
     [having_clause ]
     [order_by_clause ]
     [compute_clause ]
     [read_only_clause ]
     [isolation_clause ]
     [browse_clause ]
     [plan_clause]for_xml_clause::=
    |for xml[option option_string]
 option_string::=basic_character_expression

For more information about option strings, see option_strings: general format.

Description

Options

The general format of the option_string is specified in option_strings: general format. The options for the for_xml_clause are specified in "SQLX Options."

Exceptions

Any SQL exception raised during execution of the basic select statement is raised by the for_xml select. For example, both of the following statements raise a zero divide exception:

select 1/0
select 1/0 for xml

Example

The for_xml clause:

select pub_id, pub_name 
from pubs2.dbo.publishers
for xml
go


 <resultset
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <row>
   <pub_id>0736</pub_id>
   <pub_name>NewAgeBooks</pub_name>
 </row>

 <row>
   <pub_id>0877</pub_id>
   <pub_name>Binnet & Hardley</pub_name>
 </row>

 <row>
   <pub_id>1389</pub_id>
   <pub_name>Algodata Infosystems</pub_name>
 </row>

 </resultset> 

forxmlj, forxmldtdj, forxmlschemaj, forxmlallj

The functions in this section are Java-based, and you must install them in your server before you can use them. For instructions see Appendix E, "The Java-Based XQL Processor. ".

The Java-based forxml functions map the result set of a SQL query to a SQLX-XML schema, result set document, or both. The SQL query is specified as a character string, containing an arbitrary SQL query expression. When xmlextract and xmltest process the output SQLX-XML documents that forxmlj functions generate, they provide an XML view of the SQL data that is represented by the SQLX-XML documents.

forxmlj is a functional form of the mapping provided by the for xml clause of the select statement. The differences are:

Syntax

forxmljfunction ::=
      forxmlj(sql_query_expression, option_string)
      | forxmldtdj(sql_query_expression, option_string)
      | forxmlschemaj(sql_query_expression, option_string)
 forxmlallj_procedure::=
     execute forxmlallj
        sql_query_expression, option_string
        rs_target_out, schema_target_out, dtd_target_out
 sql_query_expression::=basic_string_expression
 option_string::=basic_string_expression

Description

For a description of the SQLX-XML representation of SQL result sets, see Chapter 5, "XML Mappings."

Options

The general format of the option_string is specified in option_strings: general format. The options for the for xml clause are specified in Chapter 5, "XML Mappings."

Exceptions

Any SQL exception raised during execution of the sql_query_expression is raised by the forxmlj function.

Examples

The forxmlj function:

set stringsize 16384 
select forxmlj  
      ("select pub_id, pub_name 
         from pubs2.dbo.publishers", "")  
go

 <resultset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
 <row>
   <pub_id>0736</pub_id>
   <pub_name>New AgeBooks</pub_name>
 </row>

 <row>
   <pub_id>0877</pub_id>
   <pub_name>Binnet & Hardley</pub_name>
 </row>

 <row>
   <pub_id>1389</pub_id>
   <pub_name>Algodata Infosystems</pub_name>
 </row>

 </resultset> 

The forxmldtdj function:

set stringsize 16384 
select forxmldtdj    
      ("select pub_id, pub_name 
         from pubs2.dbo.publishers",     
      "tablename=extract null=omit") 
go

 <!ELEMENT extract (row*)>
 <!ELEMENT row (pub_id, pub_name?)>
 <!ELEMENT pub_id (#PCDATA)>
 <!ELEMENT pub_name (#PCDATA)>

The forxmlschemaj function:

set stringsize 16384
select forxmlschemaj
     ("select pub_id, pub_name 
        from pubs2.dbo.publishers",
     "tablename=extract null=omit")

 <xsd:schema
   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
   xmlns:sqlxml=
   "http://www.iso-standards.org/mra/9075/sqlx">

 <xsd:simpleType name="CHAR_4"> 
   <xsd:restriction base="xsd:string">
      <xsd:length value="4"/>
   </xsd:restriction>   
 </xsd:simpleType>

 <xsd:simpleType name="VARCHAR_40">
   <xsd:restriction base="xsd:string">
      <xsd:length value="40"/>
   </xsd:restriction>
 </xsd:simpleType>

 <xsd:complexType name="RowType.extract">
   <xsd:sequence>
       <xsd:element name="pub_id" type="CHAR_4"
          minOccurs="0" MaxOccurs="1"/>
        <xsd:element name="pub_name" type="VARCHAR_40"
          minOccurs="0" maxOccurs="1"/>
   </xsd:sequence>
 </xsd:complexType>   

 <xsd:complexType name="TableType.extract">
   <xsd:sequence>
       <xsd:element name="row" type="RowType.extract"
         minOccurs="0" maxOccurs="unbounded"/>
   </xsd:sequence>
 </xsd:complexType>   

 <xsd:element name="extract"type="TableType.extract"/>
 </xsd:schema> 

The forxmlallj procedure:

set stringsize 16384 
declare @rs varchar(16384) 
declare @schema varchar(16384) 
declare @dtd varchar(16384) 
execute forxmlallj  
  "select pub_id, pub_name from pubs2.dbo.publishers",
  "name=extract  null=attribute",  
  @rs out, @schema    out, @dtd out 
go

 <extract 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <row>
   <pub_id>0736</pub_id>
   <pub_name>New Age Books</pub_name>
 </row>

 <row>
   <pub_id>0877</pub_id>
   <pub_name>Binnet & Hardley</pub_name>
 </row>

 <row>
   <pub_id>1389</pub_id>
   <pub_name>Algodata Infosystems</pub_name>
 </row>
 </extract>

 <xsd:schema
   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
   xmlns:sqlxml=
   "http://www.iso-standards.org/mra/9075/sqlx">
 <xsd:simpleType name="CHAR_4">
   <xsd:restriction base="xsd:string">
      <xsd:length value="4"/>
   </xsd:restriction>
 </xsd:simpleType>

 <xsd:simpleType name="VARCHAR_40">
   <xsd:restriction base="xsd:string">
     <xsd:length value="40"/>
   </xsd:restriction>
 </xsd:simpleType> 

 <xsd:complexType name="RowType.extract">
   <xsd:sequence>
     <xsd:element name="pub_id" type="CHAR_4"
          nillable="true" />
     <xsd:element name="pub_name" type="VARCHAR_40"
          nillable="true" />
   </xsd:sequence> 
 </xsd:complexType> 

 <xsd:complexType name="TableType.extract">
   <xsd:sequence>
      <xsd:element name="row" type="RowType.extract"
         minOccurs="0" maxOccurs="unbounded"/>
   </xsd:sequence>
 </xsd:complexType>
 <xsd:elementname="extract"type="TableType.extract">
 </xsd:schema>

 <!ELEMENT extract (row*)>
 <!ELEMENT row (pub_id, pub_name)>
 <!ELEMENT pub_id (#PCDATA)>
 <!ELEMENT pub_name (#PCDATA)>

forsqlcreatej, forsqlinsertj, forsqlscriptj

The functions in this section are Java-based, and you must install them in your server before you can use them. For instructions, see Appendix E, "The Java-Based XQL Processor. ".

The Java-based forsql functions map SQLX-XML schema and SQLX-XML result set documents to a SQL script.

Syntax

sqlx_to_sql_script_function ::=
      forsqlcreatej(sqlx_schema, option_string)
      | forsqlinsertj(sqlx_resultset, option_string)
      |forsqlscriptj(sqlx_schema, sqlx_resultset, option_string)
 sqlx_schema::=basic_string_expression
 sqlx_resultset::=basic_string_expression
 option_string::=basic_string_expression

Description

Options

The general format of the option_string is described in option_strings: general format.

The forsqlcreatej, forsqlinsertj, and forsqlscripj functions support the following option, described in the "Exceptions" section, below.

xmlerror={exception | null | message}

Exceptions

If the value of sqlx_schema or sqlx_resultset is not valid XML:

Examples

The forsqlcreatej function:

set stringsize 16384
declare @schema varchar(16384) 
select @schema = forxmlschemaj(  
   "select pub_id, pub_name from pubs2.dbo.publishers",
   "tablename=extract  null=attribute") 
select forsqlcreatej(@schema, "") 
go
CREATE TABLE "extract"(      
   "pub_id" CHAR(4) null,       
   "pub_name" VARCHAR(40) null ) 

The forsqlinsertj function:

set stringsize 16384 
declare @rs varchar(16384) 
select @rs = forxmlj(   
   "select pub_id, pub_name from pubs2.dbo.publishers")
select forsqlinsertj(@rs, "")
go

   --Begin table "resultset"
insert into "resultset"   
   ("pub_id", "pub_name")    
    values ( '0736', 'New Age Books') 
insert into "resultset"   
   ("pub_id", "pub_name")    
    values ( '0877', 'Binnet & Hardley') 
insert into "resultset"   
   ("pub_id", "pub_name")    
    values ( '1389', 'Algodata Infosystems') 
--End table "resultset" 

The forsqlscriptj function:

set stringsize 16384
declare @rs varchar(16384)
declare @schema varchar(16384)
declare @dtd varchar(16384)
execute forxmlallj   
   "select pub_id, pub_name from pubs2.dbo.publishers",
   "tablename=extract  null=attribute",     
    @rs out, @schema out, @dtd out
declare @script varchar(16384)

select @script = forsqlscriptj(@schema, @rs, "") 
select @script
execute ("set quoted_identifier on " + @script )
execute ("select pub_id, pub_name from extract")
execute ("drop table extract")
go
(return status = 0)

Return parameters:

*****Values of @rs, @schema, and @dtd omitted********
(1 row affected)
(1 row affected) 

CREATE TABLE "extract"(      
   "pub_id" CHAR(4) null,      
    "pub_name" VARCHAR(40) null)

--Begin table "extract"
insert into "extract"   
   ("pub_id", "pub_name")    
    values ( '0736', 'New Age Books') 
insert into "extract"   
   ("pub_id", "pub_name")    
    values ( '0877', 'Binnet & Hardley') 
insert into "extract"   
   ("pub_id", "pub_name")    
    values ( '1389', 'Algodata Infosystems') 
--End table "extract" 

(1 row affected)
(1 row affected)
(1 row affected)
(1 row affected) 

pub_id pub_name
------ -------------------
1)    New Age Books                            
2)    Binnet & Hardley                         
3)    Algodata Infosystems                     

(3 rows affected)


Chapter 3 XML Language and XML Query Language [Table of Contents] Chapter 5 XML Mappings