Team Fly 

Page 277

xmlelement()

The xmlelement() function is one of the most important SQLX functions to understand, because creating XML elements is the fundamental reason for SQLX's existence. Since every XML document starts with a root element, this function is always called first. It takes as parameters the element name, an optional xmlattibutes() call, and other xmlelement() or SQLX functions calls, which make up the children of the element. It's important to understand that the xmlelement() function returns an XMLType value, not a character string value.

xmlattributes()

Within every XML element there may be defined an attribute. xmlattibutes() is used to create attributes inside an xmlelement().

xmlforest()

The word forest means ''a collection of trees," or in the case of XML, nodes. A fitting name—since the xmlforest() function allows you to pass 1 to n values making up a collection of child elements under an xmlelement() call. xmlforest() uses the name of the column as the name of the XML element, unless otherwise specified in an optional as clause, and uses the value of the SQL value expression as the content of the XML element.

NOTE
The disadvantage of using xmlforest() is that you cannot specify element attributes. If you need to specify attributes for an element, you must use xmlelement() in conjunction with xmlattibutes().

Now, let's take a look at these SQLX functions in action by generating an XMLType that has a list of products belonging to the electronics product category and their list price. The following listing shows the PRODUCTS table currently in our database. Following it, in Figure 8-1, is a proposed hierarchy we want to produce for our XML.

SQL> desc products
 Name                      Null?    Type
 ------------------------- -------- ----------------------------
 PROD_ID                   NOT NULL NUMBER(6)
 PROD_NAME                 NOT NULL VARCHAR2(50)
 PROD_DESC                 NOT NULL VARCHAR2(4000)
 PROD_SUBCATEGORY          NOT NULL VARCHAR2(50)
 PROD_SUBCATEGORY_ID       NOT NULL NUMBER
Team Fly 
0296