<<<<<<< HEAD xmlattr | H2 | SQLDoc
Skip to main content

xmlattr

1. XMLATTR Function

======= xmlattr | H2 Function | SQLDoc

xmlattr

1. XMLATTR Function

>>>>>>> 9d19b1c8cb744c8e86d5c15b74d8b5f719ad62be

The XMLATTR function in H2 database is used to extract the value of an attribute from an XML element.

2. Syntax

The syntax for the XMLATTR function in H2 database is as follows:

XMLATTR(xml, attribute)

Arguments

  • xml: The XML element from which the attribute value needs to be extracted. It should be a valid XML string.
  • attribute: The name of the attribute whose value needs to be extracted. It should be a string.

Return

  • The XMLATTR function returns the value of the specified attribute as a string.

3. Notes

  • The XMLATTR function in H2 database works only with valid XML strings. If the input xml is not well-formed, the function will return NULL.
  • If the given attribute is not present in the XML element, the function will return NULL.
  • The XMLATTR function is case-sensitive when it comes to attribute names.

4. Examples

Here are a few examples demonstrating the usage of the XMLATTR function in H2 database:

Example 1 - Extracting the value of the "name" attribute from an XML element:

SELECT XMLATTR('<person name="John" age="30" />', 'name') AS attribute_value;

Output:

attribute_value
---------------
John

Example 2 - Extracting the value of an attribute from XML elements stored in a column:

CREATE TABLE xml_data (
id INT PRIMARY KEY,
xml_element XML
);

INSERT INTO xml_data VALUES (1, '<book title="The Great Gatsby" author="F. Scott Fitzgerald" />'),
(2, '<book title="To Kill a Mockingbird" author="Harper Lee" />');

SELECT id, XMLATTR(xml_element, 'title') AS title, XMLATTR(xml_element, 'author') AS author
FROM xml_data;

Output:

id |         title          |       author
---+------------------------+-------------------
1 | The Great Gatsby | F. Scott Fitzgerald
2 | To Kill a Mockingbird | Harper Lee
  • xmlnode - Extract XML element as a string
  • XMLEXISTS - Check if an XML element exists