XML: elements, attributes and content
Loaded XML uses Snowflake’s object representation. XMLGET selects a child element; GET reads its content or attributes.
Understand the idea
Use the first lesson’s schema and stage. Save the one-document fixture and upload it to xml/. XMLGET operates on parsed XML, not an arbitrary VARCHAR. For inline exploration, PARSE_XML converts a string first.
| Concept | What it means |
|---|---|
| XMLGET(doc, name) | Returns a child element object. |
| GET(element, '$') | Reads the element’s content. |
| GET(element, '@id') | Reads an id attribute. |
| Repeated elements | XMLGET uses a zero-based instance argument. |
A concrete example
<event id="101"><amount>35</amount></event> has an id attribute and an amount child.
Remember: An XML element is not the same as its scalar text content.
Explain it step by step · full theory
How to apply it
Use the first lesson’s schema and stage. Save the one-document fixture and upload it to xml/. XMLGET operates on parsed XML, not an arbitrary VARCHAR. For inline exploration, PARSE_XML converts a string first.
The document object already represents the outer event element; do not call XMLGET(doc, event) to select that root again. Namespace handling, repeated children and mixed content need inspection before scalar casting.
Words used in this lesson
- XMLGET(doc, name)
- Returns a child element object.
- GET(element, '$')
- Reads the element’s content.
- GET(element, '@id')
- Reads an id attribute.
- Repeated elements
- XMLGET uses a zero-based instance argument.
See how it works
Choose a step to explore the idea at your own pace.
Open the interactive explanation
XML: elements, attributes and content
Read the shape
Loaded XML uses Snowflake’s object representation. XMLGET selects a child element; GET reads its content or attributes.
Automatic playback is off for reduced motion. Select any step or use Previous and Next. Illustrations explain behavior; they are not live measurements.
Read the complete diagram explanation
- Read the shape
Loaded XML uses Snowflake’s object representation. XMLGET selects a child element; GET reads its content or attributes.
- Follow the example
<event id="101"><amount>35</amount></event> has an id attribute and an amount child.
- Verify the result
An XML element is not the same as its scalar text content.
Try the SQL or command
Read → predict → run in your learning environment → compare. You can also learn by reading without an account.
New to SQL? Where to run it and how to read it
A query is a question written in SQL. Snowflake runs it and returns a result table. This website shows examples; it does not connect to your account.
- No account yet? Read and predict the output first. Follow the account and Snowsight lesson when you are ready.
- For SQL practice, use a learning account in Snowsight and select an authorized role and warehouse. A role determines permissions; a warehouse supplies processing power. Follow the example’s database/schema setup when it refers to stored objects.
- Read the entire example, including comments beginning with
--. Run the selected statement or labeled block in the stated order. Examples labeled Python, shell or HTTP belong in that client, not in a SQL editor. - Compare values and row counts with the explained result. If an example deliberately causes an error, follow its rollback and cleanup steps before continuing.
- SELECT / FROM
- Choose the values to return / identify their input.
- VALUES / COLUMN1
- VALUES supplies tiny example rows. COLUMN1 means their first column, COLUMN2 their second.
- AS / WITH
- AS gives a result a readable name. WITH names a temporary query step within one statement.
- NULL / ::
- NULL means missing or unknown. The :: notation converts a value to a named data type.
- Session
- One connection and its settings. Keep related transaction steps in the same session.
- Database.schema.table
- A full address for stored data. It is separate from the warehouse that processes queries.
Need the L2_OBJECTS_LAB setup? Open the first database-object lesson →
Help with common practice errors
- No active warehouse: select a warehouse your role can use. Do not assume you need to create a new one.
- Object missing or not authorized: check the full name, required setup and active role. Ask the account owner for access if needed.
- Object already exists: setup may already have run. Read the current state or choose a fresh lab name consistently; do not add OR REPLACE blindly.
- Different result: check whether an earlier INSERT or UPDATE changed the data. VALUES examples provide their own fresh input for each statement.
Before running this lesson
Manual learning examples; nothing runs on this website. Standalone WITH/SELECT examples supply their own input. Table and file exercises require the first lesson’s L2_OBJECTS_LAB.M7_LAB setup and an authorized warehouse. Select that schema in every session. The final lab requires the supplied events.json to be loaded first.
Save event.xml and upload to xml/
<event id="101"><amount>35</amount><tag>sale</tag><tag>gift</tag></event>What to expect and why
Upload using Snowsight or the lesson 6 PUT pattern with event.xml and xml/.
Load the document and extract values
CREATE TABLE M7_XML_RAW (DOC OBJECT);
COPY INTO M7_XML_RAW FROM @M7_FILES/xml/
FILES=('event.xml') FILE_FORMAT=(TYPE=XML);
SELECT GET(DOC, '@id')::NUMBER AS EVENT_ID,
GET(XMLGET(DOC, 'amount'), '$')::NUMBER AS AMOUNT,
GET(XMLGET(DOC, 'tag', 1), '$')::VARCHAR AS SECOND_TAG
FROM M7_XML_RAW;What to expect and why
Expected one row: 101, 35, gift. The instance argument 1 selects the second tag.
Check your understanding
Choose an answer, then check your reasoning. This is practice, not a scored test.
Try it yourself
Why does selecting the outer event with XMLGET fail to find the root?
Compare your reasoning
XMLGET searches contained elements; DOC already is the event element object.