Updated on 2024-05-07 GMT+08:00

Functions of the XMLType Type

  • createxml(varchar2 [,varchar2 ,numeric ,numeric])

    Description: Statically creates the XMLType type. The input parameters are of the varchar2 type.

    Parameters: The first parameter is the character string to be converted to XMLType (mandatory column). The second parameter is the optional schema URL used to make the input comply with the specified schema (optional column, which is empty by default and does not take effect currently). The third parameter is the flag indicating whether the instance is valid according to the given XML schema (optional column, which is 0 by default and does not take effect currently). The fourth parameter is the flag indicating whether the instance is well-formed (optional column, which is 0 by default and does not take effect currently).

    Return type: XMLType

    Example:
    1
    2
    3
    4
    5
    gaussdb=# SELECT createxml('<a>123</a>');
     createxml
    --------------
     <a>123</a>
    (1 row)
    
    • Different from that in database A, in PL/SQL, createxml allows input parameters to be empty strings and returns NULL.
    • For character encoding, only UTF-8, GBK, and LATIN1 to LATIN10 are supported, and the version field can only be set to 1.x.
    • The createxml function can be called using the xmltype.createxml() syntax.

      Example:

      1
      2
      3
      4
      5
      gaussdb=# SELECT xmltype.createxml('<a>123</a>');
       createxml  
      ------------
       <a>123</a>
      (1 row)
      
    • In this chapter, the function whose input parameter is xmltype() can be called in xmltype().func() mode. The XMLType type returned by a function is transferred to the next function as the input parameter. This syntax supports multi-layer nesting. (If the input parameter is defined as XMLType by a user, this syntax is not supported.)
      Example:
      1
      2
      3
      4
      5
      gaussdb=# select xmltype('<a>123<b>456</b></a>').extract('/a/b').getstringval();
       xmltypefunc 
      -------------
       <b>456</b>
      (1 row)
      
      The actual effect of the preceding example is the same as that of the following function nesting:
      1
      2
      3
      4
      5
      gaussdb=# select getstringval(extractxml(xmltype('<a>123<b>456</b></a>'),'/a/b'));
       getstringval 
      --------------
       <b>456</b>
      (1 row)
      
    • In a stored procedure, variables of the XMLType type can call functions in a.func() mode. This syntax supports one-layer nesting.
      Example:
       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      gaussdb=# declare
                a xmltype;
                b varchar2;
      begin
                a:=xmltype('<a>123<b>456</b></a>');
                b:=a.getstringval();
                RAISE NOTICE 'xmltype_str is : %',b;
      end;
      /
      NOTICE:  xmltype_str is : <a>123<b>456</b></a>
      
  • createxml(clob [,varchar2 ,numeric ,numeric])

    Description: Statically creates the XMLType type. The input parameters are of the CLOB type.

    Parameters: The first parameter is the CLOB to be converted to XMLType (mandatory column). The second parameter is the optional schema URL used to make the input comply with the specified schema (optional column, which is empty by default and does not take effect currently). The third parameter is the flag indicating whether the instance is valid according to the given XML schema (optional column, which is 0 by default and does not take effect currently). The fourth parameter is the flag indicating whether the instance is well-formed (optional column, which is 0 by default and does not take effect currently).

    Return type: XMLType

    Example:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    gaussdb=# declare
     xmltype_clob clob;
     xmltype_obj xmltype;
     xmltype_str varchar2(1000);
    begin
     xmltype_clob := '<a>123</a>';
     xmltype_obj := createxml(xmltype_clob);
     xmltype_str := xmltype_obj.getstringval();
     RAISE NOTICE 'xmltype_str is : %',xmltype_str;
    end;
    /
    NOTICE:  xmltype_str is : <a>123</a>
    

    The maximum size of the input parameter of the CLOB type is 1 GB minus 1 byte.

  • createxml(blob, numeric [,varchar2 ,numeric ,numeric])

    Description: Statically creates the XMLType type. The input parameters are of the BLOB type.

    Parameters: The first parameter is the BLOB to be converted to XMLType (mandatory column). The second parameter is the character set ID of the input XML data (mandatory column). The third parameter is the optional schema URL used to make the input comply with the specified schema (optional column, which is empty by default and does not take effect currently). The fourth parameter is the flag indicating whether the instance is valid according to the given XML schema (optional column, which is 0 by default and does not take effect currently). The fifth parameter is the flag indicating whether the instance is well-formed (optional column, which is 0 by default and does not take effect currently).

    Return type: XMLType

    Example:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    gaussdb=# declare
     xmltype_blob blob;
     xmltype_obj xmltype;
     xmltype_str varchar2(1000);
    begin
     xmltype_blob := xmltype('<a>123</a>').getblobval(7);
     xmltype_obj := createxml(xmltype_blob,7);
     xmltype_str := xmltype_obj.getstringval();
     RAISE NOTICE 'xmltype_str is : %',xmltype_str;
    end;
    /
    NOTICE:  xmltype_str is : <?xml version="1.0" encoding="UTF8"?>
    <a>123</a>
    
    • The maximum size of input parameters of the BLOB type is 256 MB minus 1 byte.
    • The character set ID ranges from 1 to 41.
  • getblobval(xmltype, numeric)

    Description: Converts the XMLType type to the BLOB type. The xmltype().func() method can be called.

    Parameters: The first parameter is of the XMLType type, and the second parameter is the ID of the target character set to be converted.

    Return type: BLOB

    Example:
    1
    2
    3
    4
    5
    gaussdb=# SELECT getblobval(xmltype('<asd/>'),7);
                                            getblobval                                        
    ------------------------------------------------------------------------------------------
     3C3F786D6C2076657273696F6E3D22312E302220656E636F64696E673D2255544638223F3E0A3C6173642F3E
    (1 row)
    
    xmltype ().func ():
    1
    2
    3
    4
    5
    gaussdb=# select xmltype('<asd/>').getblobVal(7);
                                           xmltypefunc                                        
    ------------------------------------------------------------------------------------------
     3C3F786D6C2076657273696F6E3D22312E302220656E636F64696E673D2255544638223F3E0A3C6173642F3E
    (1 row)
    

    The maximum length of the input parameter of the XMLType type is 256 MB minus 1 byte.

  • getclobval(xmltype)

    Description: Converts the XMLType type to the CLOB type. The xmltype().func() method can be called.

    Parameter: The input parameter is of the XMLType type.

    Return type: CLOB

    Example:
    1
    2
    3
    4
    5
    gaussdb=# SELECT getclobval(xmltype('<a>123</a>'));
     getclobval
    --------------
     <a>123</a>
    (1 row)
    
    xmltype ().func ():
    1
    2
    3
    4
    5
    gaussdb=# SELECT xmltype('<a>123</a>').getclobval();
     xmltypefunc
    --------------
     <a>123</a>
    (1 row)
    

  • getnumberval(xmltype)

    Description: Converts the XMLType type to the numeric type. The xmltype().func() method can be called.

    Parameter: The input parameter is of the XMLType type.

    Return type: numeric

    Example:
    1
    2
    3
    4
    5
    gaussdb=# SELECT getnumberval(xmltype('<a>123</a>').extract('/a/text()'));
     getnumberval
    --------------
               123
    (1 row)
    
    xmltype ().func ():
    1
    2
    3
    4
    5
    gaussdb=# SELECT xmltype('<a>123</a>').extract('/a/text()').getnumberval();
     xmltypefunc
    --------------
               123
    (1 row)
    

  • isfragment(xmltype)

    Description: Returns a result indicating whether the XMLType type is fragment (1) or document (0). The xmltype().func() method can be called.

    Parameter: The input parameter is of the XMLType type.

    Return type: numeric

    Example:
    1
    2
    3
    4
    5
    gaussdb=# SELECT isfragment(xmltype('<a>123</a>'));
     isfragment
    --------------
                 0
    (1 row)
    
    xmltype ().func ():
    1
    2
    3
    4
    5
    gaussdb=# SELECT xmltype('<a>123</a>').isfragment();
     xmltypefunc
    --------------
                 0
    (1 row)
    

  • xmltype(varchar2 [,varchar2 ,numeric ,numeric])

    Description: Creates the XMLType type from the varchar2 type.

    Parameters: The first parameter is the character string to be converted to XMLType (mandatory column). The second parameter is the optional schema URL used to make the input comply with the specified schema (optional column, which is empty by default and does not take effect currently). The third parameter is the flag indicating whether the instance is valid according to the given XML schema (optional column, which is 0 by default and does not take effect currently). The fourth parameter is the flag indicating whether the instance is well-formed (optional column, which is 0 by default and does not take effect currently).

    Return type: XMLType

    Example:
    1
    2
    3
    4
    5
    gaussdb=# SELECT xmltype('<a>123</a>');
     xmltype
    --------------
     <a>123</a>
    (1 row)
    
    • Different from that in database A, in PL/SQL, XMType allows input parameters to be empty strings and returns NULL.
    • For character encoding, only UTF-8, GBK, and LATIN1 to LATIN10 are supported, and the version field can only be set to 1.x.
  • xmltype(clob [,varchar2 ,numeric ,numeric])

    Description: Creates the XMLType type from the CLOB type.

    Parameters: The first parameter is the CLOB to be converted to XMLType (mandatory column). The second parameter is the optional schema URL used to make the input comply with the specified schema (optional column, which is empty by default and does not take effect currently). The third parameter is the flag indicating whether the instance is valid according to the given XML schema (optional column, which is 0 by default and does not take effect currently). The fourth parameter is the flag indicating whether the instance is well-formed (optional column, which is 0 by default and does not take effect currently).

    Return type: XMLType

    Example:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    gaussdb=# declare
     xmltype_clob clob;
     xmltype_obj xmltype;
     xmltype_str varchar2(1000);
    begin
     xmltype_clob := '<a>123</a>';
     xmltype_obj := xmltype(xmltype_clob);
     xmltype_str := xmltype_obj.getstringval();
     RAISE NOTICE 'xmltype_str is : %',xmltype_str;
    end;
    /
    NOTICE:  xmltype_str is : <a>123</a>
    

    The maximum size of the input parameter of the CLOB type is 1 GB minus 1 byte.

  • xmltype(blob, numeric [,varchar2 ,numeric ,numeric])

    Description: Creates the XMLType type from the BLOB type.

    Parameters: The first parameter is the BLOB to be converted to XMLType (mandatory column). The second parameter is the character set ID of the input XML data. The third parameter is the optional schema URL used to make the input comply with the specified schema (optional column, which is empty by default and does not take effect currently). The fourth parameter is the flag indicating whether the instance is valid according to the given XML schema (optional column, which is 0 by default and does not take effect currently). The fifth parameter is the flag indicating whether the instance is well-formed (optional column, which is 0 by default and does not take effect currently).

    Return type: XMLType

    Example:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    gaussdb=# declare
     xmltype_blob blob;
     xmltype_obj xmltype;
     xmltype_str varchar2(1000);
    begin
     xmltype_blob := getblobval(createxml('<a>123</a>'),7);
     xmltype_obj := xmltype(xmltype_blob,7);
     xmltype_str := xmltype_obj.getstringval();
     RAISE NOTICE 'xmltype_str is : %',xmltype_str;
    end;
    /
    NOTICE:  xmltype_str is : <?xml version="1.0" encoding="UTF8"?>
    <a>123</a>
    
    • The maximum size of input parameters of the BLOB type is 256 MB minus 1 byte.
    • The character set ID ranges from 1 to 41.

  • getstringval(xmltype)

    Description: Converts xmltype into a string.

    Parameter: XMLType to be converted.

    Return type: varchar2

    The getstringval function can be called in either of the following ways:

    Example 1:

    gaussdb=# SELECT getstringval('<a>123<b>456</b></a>');
         getstringval
    ----------------------
     <a>123<b>456</b></a>
    (1 row)

    Example 2: The calling mode is compatible with the ORA syntax.

    gaussdb=# SELECT xmltype('<a>123<b>456</b></a>').getstringval();
         xmltypefunc
    ----------------------
     <a>123<b>456</b></a>
    (1 row)

  • getrootelement(xmltype)

    Description: Gets the root element of the XMLType type.

    Parameter: XMLType whose root element is to be obtained.

    Return type: varchar2

    The getrootelement function can be called in either of the following ways:

    Example 1:

    gaussdb=# SELECT getrootelement('<a>123<b>456</b></a>');
     getrootelement
    ----------------
     a
    (1 row)

    Example 2: The calling mode is compatible with the ORA syntax.

    gaussdb=# SELECT xmltype('<a>123<b>456</b></a>').getrootelement();
     xmltypefunc
    -------------
     a
    (1 row)

  • getnamespace(xmltype)

    Description: Gets the namespace of the XMLType top-level element.

    Parameter: XMLType whose namespace is to be obtained.

    Return type: varchar2

    The getnamespace function can be called in either of the following ways:

    Example 1:

    gaussdb=# SELECT getnamespace('<c:a xmlns:c="asd">123<d:b xmlns:d="qwe">456</d:b></c:a>');
     getnamespace
    --------------
     asd
    (1 row)

    Example 2: The calling mode is compatible with the ORA syntax.

    gaussdb=# SELECT xmltype('<c:a xmlns:c="asd">123<d:b xmlns:d="qwe">456</d:b></c:a>').getnamespace();
     xmltypefunc
    -------------
     asd
    (1 row)

  • existsnode(xmltype, varchar2[, varchar2])

    Description: Determines whether the XML node exists in XMLType based on the XPath expression. If the XML node exists, 1 is returned. Otherwise, 0 is returned.

    Parameters: XMLType to be queried, path of the XPath node to be queried, and namespace of the XPath path (If the input parameter has a namespace, aliases must be defined for both the XPath and namespace, as shown in example 3.)

    Return type: numeric

    The existsnode function can be called in either of the following ways:

    Example 1:

    gaussdb=# SELECT existsnode('<a>123<b>456</b></a>','/a/b');
     existsnode
    ------------
              1
    (1 row)

    Example 2: The calling mode is compatible with the ORA syntax.

    gaussdb=# SELECT xmltype('<a>123<b>456</b></a>').existsnode('/a/b');
     xmltypefunc
    -------------
               1
    (1 row)

    Example 3

    gaussdb=# SELECT existsnode('<a:b xmlns:a="asd">123<c>456</c></a:b>','/a:b/c','xmlns:a="asd"');
     existsnode
    ------------
              1
    (1 row)

    Example 4: The calling mode is compatible with the ORA syntax.

    gaussdb=# SELECT xmltype('<a:b xmlns:a="asd">123<c>456</c></a:b>').existsnode('/a:b/c','xmlns:a="asd"');
     xmltypefunc
    -------------
               1
    (1 row)

  • extractxml(xmltype, varchar2[, varchar2])

    Description: Checks whether an XML node exists in the given XMLType based on the XPath expression. If yes, the XMLType containing the node is returned. If no, NULL is returned. The return value can be inserted into a table of the XMLType type.

    Parameters: XMLType to be queried, path of the XPath node to be queried, and namespace of the XPath path (If the input parameter has a namespace, aliases must be defined for both the XPath and namespace, as shown in example 3.)

    Return type: XMLType

    The extractxml function can be called in either of the following ways:

    Example 1:

    gaussdb=# SELECT extractxml('<a>123<b>456</b></a>','/a/b');
     extractxml
    ------------
     <b>456</b>
    (1 row)

    Example 2: The calling mode is compatible with the ORA syntax.

    gaussdb=# SELECT xmltype('<a>123<b>456</b></a>').extract('/a/b');
     xmltypefunc
    -------------
     <b>456</b>
    (1 row)
    
    gaussdb=# SELECT xmltype('<a>123<b>456</b></a>').extractxml('/a/b');
     xmltypefunc
    -------------
     <b>456</b>
    (1 row)

    Example 3

    gaussdb=# SELECT extractxml('<a:b xmlns:a="asd">123<c>456</c></a:b>','/a:b','xmlns:a="asd"');
                   extractxml
    ----------------------------------------
     <a:b xmlns:a="asd">123<c>456</c></a:b>
    (1 row)

    Example 4: The calling mode is compatible with the ORA syntax.

    gaussdb=# SELECT xmltype('<a:b xmlns:a="asd">123<c>456</c></a:b>').extract('/a:b','xmlns:a="asd"');
                  xmltypefunc
    ----------------------------------------
     <a:b xmlns:a="asd">123<c>456</c></a:b>
    (1 row)
    
    gaussdb=# SELECT xmltype('<a:b xmlns:a="asd">123<c>456</c></a:b>').extractxml('/a:b','xmlns:a="asd"');
                  xmltypefunc
    ----------------------------------------
     <a:b xmlns:a="asd">123<c>456</c></a:b>
    (1 row)