parse_xml_attrs
Coming soon
Parses an XML string into a Liquid object, including element attributes. Works exactly like parse_xml but keeps attributes instead of stripping them.
{% assign data = xml_string | parse_xml_attrs %}
{% log data %}Syntax
{{ xml_string | parse_xml_attrs }}xml_string
A string containing valid XML data
Return Value
Returns a Liquid object representing the XML structure. Each attribute becomes a plain key on its element (e.g. node.currency). If an element has both attributes and text content, the text is available under the #text key.
Examples
Read attributes from an API response:
{% assign xml = '<rate currency="USD" code="STD">9.99</rate>' %}
{% assign data = xml | parse_xml_attrs %}
{% log data.rate.currency %} {# USD #}
{% log data.rate.code %} {# STD #}
{% log data.rate['#text'] %} {# 9.99 #}Iterate elements that carry attributes:
{% assign data = response.body | parse_xml_attrs %}
{% for line in data.order.line %}
{% log line.sku | append: ": " | append: line['#text'] %}
{% endfor %}Notes
Attributes are exposed as plain keys, so they're dot-accessible like any element
Text content of an element that also has attributes lives under
#textIf you don't need attributes, use
parse_xmlinsteadSee also:
parse_xml,parse_json,parse_csv
Last updated