xmlsimple(3am)

πŸ“¦ version 1.1.2, in fedora 43, fedora 44, nixos 26.05, nixpkgs unstable

πŸ’– 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 to XMLDECLARATION.
  • SD β€” equivalent to XMLSTARTDOCT.
  • ED β€” equivalent to XMLENDDOCT.
  • PI β€” equivalent to XMLPROCINST.
  • SE β€” equivalent to XMLSTARTELEM.
  • EE β€” equivalent to XMLENDELEM.
  • TX β€” equivalent to XMLCHARDATA.
  • SC β€” equivalent to XMLSTARTCDATA.
  • EC β€” equivalent to XMLENDCDATA.
  • CM β€” equivalent to XMLCOMMENT.
  • UP β€” equivalent to XMLUNPARSED.
  • EOI β€” equivalent to XMLENDDOCUMENT.

πŸ“ 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 every XMLSTARTELEMENT or XMLENDELEMENT token. 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.

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 /name part. The path argument is optional. If it's not given, the XMLPATH is used.
  • XmlMatch(path) β€” tests whether the current XMLPATH matches the path expression argument, anchored at the end.
  • XmlMatchScope(path) β€” returns the XMLPATH prefix 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 a name attribute with the given value. The mode argument is optional. If it's non-null then the value is handled as a regular expression instead of a fixed value.

πŸ”₯ Grep-like facilities

  • XmlGrep() β€” if invoked at the XMLSTARTELEM event, 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