@xml.bzl//:xml.bzl
Public API for xml.bzl - XML parsing and building utilities for Starlark/Bazel
Functions & Macros
xml.cdataCreate a CDATA section node.
Example:
node = xml.cdata("<script>alert('hi')</script>") print(xml.to_string(node)) # <![CDATA[<script>alert('hi')</script>]]>
Parameters
*content | The CDATA content (not escaped). |
xml.commentCreate a comment node.
Example:
node = xml.comment("This is a comment") print(xml.to_string(node)) # <!--This is a comment-->
Parameters
*content | The comment content. |
xml.count_child_elementsCount the number of child element nodes.
Parameters
*node | An element or document node. |
xml.count_childrenCount the number of child nodes.
Parameters
*node | An element or document node. |
xml.documentCreate a document node.
Example:
doc = xml.document( xml_declaration = 'version="1.0" encoding="utf-8"', children = [ xml.element("root", children = [ xml.element("child", children = [xml.text("content")]), ]), ], ) print(xml.to_string(doc)) # <?xml version="1.0" encoding="utf-8"?> # <root> # <child>content</child> # </root>
Parameters
children | List of child nodes (typically one root element). Default: None |
xml_declaration | Optional XML declaration string (e.g., 'version="1.0" encoding="utf-8"'). Default: None |
xml.elementCreate an element node.
Example:
div = xml.element("div", {"class": "container"}, [ xml.element("p", children = [xml.text("Hello, world!")]), ]) print(xml.to_string(div)) # <div class="container"> # <p>Hello, world!</p> # </div>
Parameters
*tag_name | The tag name for the element. |
attributes | Optional dict of attribute names to values. Default: None |
children | Optional list of child nodes (elements, text, etc.). Default: None |
xml.find_element_by_idFind an element by its 'id' attribute.
Parameters
*node | The starting node (element or document). |
*id_value | The id value to search for. |
xml.find_element_by_tag_nameFind the first descendant element with a specific tag name.
Parameters
*node | The starting node (element or document). |
*tag_name | The tag name to search for. |
xml.find_elements_by_attributeFind all descendant elements with a specific attribute.
Parameters
*node | The starting node (element or document). |
*attr_name | The attribute name to search for. |
attr_value | Optional attribute value to match. If None, matches any value. Default: None |
xml.find_elements_by_tag_nameFind all descendant elements with a specific tag name.
Parameters
*node | The starting node (element or document). |
*tag_name | The tag name to search for. |
xml.get_attributeGet an attribute value from an element node.
Parameters
*node | An element node. |
*name | The attribute name to look up. |
default | Default value if attribute is not found. Default: None |
xml.get_attributesGet all attributes of an element node.
Parameters
*node | An element node. |
xml.get_child_elementsGet all child element nodes of a node (excludes text, comments, etc.).
Parameters
*node | An element or document node. |
xml.get_childrenGet all child nodes of a node.
Parameters
*node | An element or document node. |
xml.get_document_elementGet the root element of a document.
Parameters
*doc | A document node. |
xml.get_errorsGet the list of parsing errors from a document.
Parameters
*doc | A document node. |
xml.get_first_childGet the first child node.
Parameters
*node | An element or document node. |
xml.get_first_child_elementGet the first child element node.
Parameters
*node | An element or document node. |
xml.get_last_childGet the last child node.
Parameters
*node | An element or document node. |
xml.get_last_child_elementGet the last child element node.
Parameters
*node | An element or document node. |
xml.get_parentGet the parent node of a node.
Parameters
*node | The node whose parent to get. |
xml.get_tag_nameGet the tag name of an element node.
Parameters
*node | An element node. |
xml.get_textGet the text content of a node.
For text nodes, returns the text directly.
For element nodes, returns concatenated text of all descendant text nodes.
For CDATA nodes, returns the CDATA content.
Parameters
*node | Any node. |
xml.has_attributeCheck if an element has a specific attribute.
Parameters
*node | An element node. |
*name | The attribute name to check. |
xml.has_errorsCheck if the document has any parsing errors.
Parameters
*doc | A document node. |
xml.is_cdataCheck if a node is a CDATA node.
Parameters
*node | The node to check. |
xml.is_commentCheck if a node is a comment node.
Parameters
*node | The node to check. |
xml.is_documentCheck if a node is a document node.
Parameters
*node | The node to check. |
xml.is_elementCheck if a node is an element node.
Parameters
*node | The node to check. |
xml.is_processing_instructionCheck if a node is a processing instruction node.
Parameters
*node | The node to check. |
xml.is_textCheck if a node is a text node.
Parameters
*node | The node to check. |
xml.parseParse an XML string into a DOM document.
Parameters
*xml_string | The XML string to parse. |
strict | If True, fail on any parsing errors. If False (default), Default: False |
xml.processing_instructionCreate a processing instruction node.
Example:
pi = xml.processing_instruction("xml-stylesheet", 'type="text/xsl" href="style.xsl"') print(xml.to_string(pi)) # <?xml-stylesheet type="text/xsl" href="style.xsl"?>
Parameters
*target | The PI target (e.g., "xml-stylesheet"). |
content | The PI content/data. Default: "" |
xml.textCreate a text node.
Example:
p = xml.element("p", children = [xml.text("Hello <world>")]) print(xml.to_string(p)) # <p>Hello <world></p>
Parameters
*content | The text content. Special characters will be escaped |
xml.to_stringConvert a node back to an XML string.
Parameters
*node | The node to serialize. |
indent | Current indentation level (default 0). Only used when pretty=True. Default: 0 |
indent_str | String to use for each level of indentation (default " "). Default: " " |
pretty | If True (default), format output with indentation and newlines. Default: True |
xml.walkWalk the tree, calling callback for each node.
Note: In Starlark, this can't modify nodes since they are immutable.
Use this for collecting information from the tree.
Parameters
*node | The starting node. |
*callback | A function(node) to call for each node. |