xmlsimple(3am)
π xmlsimple(3am) π
okay so this is a page from the GNU Awk Extension Modules manual, bestie. Sit down, grab a boba, we're doing XML in gawk and it's gonna be so much easier than you think. π§
β¨ NAME β¨
xmlsimple β adds facilities for writing simple one-line scripts with the gawk-xml extension, and also simplifies writing more complex scripts. π
β¨ SYNOPSIS β¨
@include "xmlsimple"
parentpath = XmlParent(path)
test = XmlMatch(path)
scopepath = XmlMatchScope(path)
ancestorpath = XmlMatchAttr(path, name, value, mode)
XmlGrep()
β¨ DESCRIPTION β¨
So the xmlsimple library is here to make writing simple one-line scripts based on the gawk-xml extension actually doable. It also ships higher-level functions that make writing more complex scripts way less of a whole thing. It's an alternative to the xmllib library, and here's the key difference, no cap: $0 is not changed, so xmlsimple plays nice with awk code that relies on the gawk-xml core interface. π«Ά
π€ Short token variable names
To keep simple scripts short, xmlsimple gives you two-letter named variables that duplicate the predefined token-related core variables:
XDβ equivalent toXMLDECLARATION.SDβ equivalent toXMLSTARTDOCT.EDβ equivalent toXMLENDDOCT.PIβ equivalent toXMLPROCINST.SEβ equivalent toXMLSTARTELEM.EEβ equivalent toXMLENDELEM.TXβ equivalent toXMLCHARDATA.SCβ equivalent toXMLSTARTCDATA.ECβ equivalent toXMLENDCDATA.CMβ equivalent toXMLCOMMENT.UPβ equivalent toXMLUNPARSED.EOIβ equivalent toXMLENDDOCUMENT.
π Collecting character data
Character data items sitting between element tags get automatically collected into a single CHARDATA variable. This is iconic because it makes processing text data interspersed with comments, processing instructions or CDATA markup actually chill.
CHARDATAβ available at everyXMLSTARTELEMENTorXMLENDELEMENTtoken. Contains all the character data since the previous start- or end-element tag. π
πΈ Whitespace handling
The XMLTRIM mode variable controls whether whitespace in the CHARDATA variable gets automatically trimmed or not. Possible values are:
XMLTRIM = 0β keep all whitespace.XMLTRIM = 1(the default, bestie) β discard leading and trailing whitespace, and collapse contiguous whitespace characters into a single space char.XMLTRIM = -1β just collapse contiguous whitespace characters into a single space char. Keeps the collapsed leading or trailing whitespace.
π Record ancestors information
The ATTR array variable automatically keeps the attributes of every ancestor of the current element, and of the element itself. It's giving family tree but make it attributes. π³
ATTR[path@attribute]β contains the value of the specified attribute of the ancestor element at the given path.
π« Example
While processing a /books/book/title element, ATTR["/books/book@on-loan"] contains the name of the book loaner.
π¦ Path related functions
Okay so first, vocabulary, because you need this:
A fixed path is a slash delimited list of direct child elements (/name/name/...). A path expression also accepts an asterisk (*) to match any name, and a double slash (//) to represent a descendant at any level. An absolute path starts with a slash (that's a path from the root element). A relative path without a leading slash can start at any level (that's a path from some ancestor).
XmlParent(path)β returns the path of the parent element. I.e., the path argument without the last/namepart. The path argument is optional. If it's not given, theXMLPATHis used.XmlMatch(path)β tests whether the currentXMLPATHmatches the path expression argument, anchored at the end.XmlMatchScope(path)β returns theXMLPATHprefix not matched by the matching path expression argument. Returns a null value if there is no match.XmlMatchAttr(path, name, value, mode)β returns the path of the innermost ancestor that matches the path argument and also has anameattribute with the givenvalue. Themodeargument is optional. If it's non-null then thevalueis handled as a regular expression instead of a fixed value.
π₯ Grep-like facilities
XmlGrep()β if invoked at theXMLSTARTELEMevent, causes the whole element subtree to be copied to the output. Periodt. π
π NOTES π
The xmlsimple library includes both the xmlbase and xmlcopy libraries. Their functionality is implicitly available. Free real estate, basically. β¨
π BUGS π
The path related functions only operate on elements. Comments, processing instructions or CDATA sections are not taken into account.
XmlGrep() cannot be used to copy tokens outside the root element (XML prologue or epilogue).
π SEE ALSO π
XML Processing With gawk, xmlbase(3am), xmlcopy(3am), xmltree(3am), xmlwrite(3am).
π AUTHOR π
Manuel Collado, m-collado@users.sourceforge.net. π«Ά
π COPYING PERMISSIONS π
Copyright (C) 2017, Free Software Foundation, Inc.
Permission is granted to make and distribute verbatim copies of this manual page provided the copyright notice and this permission notice are preserved on all copies.
Permission is granted to copy and distribute modified versions of this manual page under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.
Permission is granted to copy and distribute translations of this manual page into another language, under the above conditions for modified versions, except that this permission notice may be stated in a translation approved by the Foundation.
and that's the whole page, bestie!! this one's from the GAWK Extension Library (the gawkex... source), dated January 2017, and it lives at xmlsimple(3am) in the manual. go forth and parse some XML, you absolute main character. π¦β¨π
π¦ where this page comes from
- fedora 43: gawk-xml 1.1.2-2.fc43 Β· GPL-1.0-or-later AND GPL-3.0-or-later
- fedora 44: gawk-xml 1.1.2-3.fc44 Β· GPL-1.0-or-later AND GPL-3.0-or-later
- nixos 26.05: gawkextlib-xml-unstable 2022-10-20
- nixpkgs unstable: gawkextlib-xml-unstable 2022-10-20
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms