Home >Database >Mysql Tutorial >How to Extract Attribute Values from XML Data in SQL Using XQuery?

How to Extract Attribute Values from XML Data in SQL Using XQuery?

DDD
DDDOriginal
2024-12-25 13:49:14790browse

How to Extract Attribute Values from XML Data in SQL Using XQuery?

SQL: Accessing Attribute Values from XML Data

In SQL, accessing the value of an attribute in an XML datatype can be achieved through XQuery. This allows you to query and manipulate XML data efficiently.

To retrieve the value of the language attribute in the provided XML:

You can use the following XQuery expression:

This expression specifies:

  • (/email/account): Selects the account element within the root email element.
  • @language: Retrieves the value of the language attribute.
  • [1] (optional): Returns the first matching value, as there is only one language attribute.

To use this expression in your SQL statement, you can modify your query as follows:

This query will return the value of the language attribute as a string.

Example:

Output:

You can also use XQuery to process XML data from a table, as demonstrated in the following example:

Output:

The above is the detailed content of How to Extract Attribute Values from XML Data in SQL Using XQuery?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn