xsxp(3tcl)
π xsxp β eXtremely Simple Xml Parser π
okay bestie, welcome to xsxp, which lives in the 3tcl manual and is part of the Amazon S3 Web Service Utilities. no cap, it's tiny. it's precious. it's giving "i wrote this in one afternoon because TclDOM was being a diva." let's go π¦
β¨ SYNOPSIS β¨
package require Tcl 8.5 9
package require xsxp 1.1
package require xml
xsxp::parse xml
xsxp::fetch pxml path ?part?
xsxp::fetchall pxml_list path ?part?
xsxp::only pxml tagname
xsxp::prettyprint pxml ?chan?
literally that's the whole vibe check. six lines and we're done π
πΈ DESCRIPTION πΈ
so this package gives you a simple interface to parse XML into a pure-value list. it ALSO gives you accessor routines to pull out specific subtags, not unlike DOM access. this package was written for and is used by Darren New's Amazon S3 access package. iykyk π
ngl, the author himself says this is pretty lame, but he needed something like this for S3, and at the time, TclDOM would not work with the new 8.5 Tcl due to version number problems. we've all been there π
plus β and this is the slay part β this is a pure-value implementation. there is no garbage to clean up in the event of a thrown error, for example. that simplifies the code for sufficiently small XML documents, which is what Amazon's S3 guarantees. no cleanup arc, no baggage, just vibes β¨
Copyright 2006 Darren New. All Rights Reserved. NO WARRANTIES OF ANY TYPE ARE PROVIDED. COPYING OR USE INDEMNIFIES THE AUTHOR IN ALL WAYS. This software is licensed under essentially the same terms as Tcl. See LICENSE.txt for the terms. π
π COMMANDS π
the package implements five rather simple procedures. one parses, one is for debugging, and the rest pull various parts of the parsed document out for processing. five little icons, all doing their thing π«Ά
π§ xsxp::parse xml
this parses an XML document (using the standard xml tcllib module in a SAX sort of way) and builds a data structure which it returns if the parsing succeeded. the return value is referred to herein as a "pxml", or "parsed xml". the list consists of two or more elements:
- the first element is the name of the tag.
- the second element is an
array-getformatted list of key/value pairs. the keys are attribute names and the values are attribute values. this is an empty list if there are no attributes on the tag. - the third through end elements are the children of the node, if any. each child is, recursively, a pxml.
- note that if the zero'th element, i.e. the tag name, is
"%PCDATA", then the attributes will be empty and the third element will be the text of the element. in addition, if an element's contents consists only of PCDATA, it will have only one child, and all the PCDATA will be concatenated. in other words, this parser works poorly for XML with elements that contain both child tags and PCDATA. since Amazon S3 does not do this (and for that matter most uses of XML where XML is a poor choice don't do this), this is probably not a serious limitation. so like, it's fine. probably. π
π xsxp::fetch pxml path ?part?
pxml is a parsed XML, as returned from xsxp::parse. path is a list of element tag names. each element is the name of a child to look up, optionally followed by a hash ("#") and a string of digits. an empty list or an initial empty element selects pxml. if no hash sign is present, the behavior is as if "#0" had been appended to that element. (in addition to a list, slashes can separate subparts where convenient.) π¦
an element of path scans the children at the indicated level for the n'th instance of a child whose tag matches the part of the element before the hash sign. if an element is simply "#" followed by digits, that indexed child is selected, regardless of the tags in the children. hence, an element of "#3" will always select the fourth child of the node under consideration. (zero-indexed girlies rise π
)
part defaults to "%ALL". it can be one of the following case-sensitive terms:
%ALLβ returns the entire selected element.%TAGNAMEβ returnslindex 0of the selected element.%ATTRIBUTESβ returnsindex 1of the selected element.%CHILDRENβ returnslrange 2through end of the selected element, resulting in a list of elements being returned.%PCDATAβ returns a concatenation of all the bodies of direct children of this node whose tag is%PCDATA. it throws an error if no such children are found. that is,part=%PCDATAmeans return the textual content found in that node but not its children nodes.%PCDATA?β is like%PCDATA, but returns an empty string if no PCDATA is found. the chill, non-dramatic sibling π«Ά
for example, to fetch the first bold text from the fifth paragraph of the body of your HTML file,
xsxp::fetch $pxml {body p#4 b} %PCDATA
π xsxp::fetchall pxml_list path ?part?
this iterates over each PXML in pxml_list (which must be a list of pxmls) selecting the indicated path from it, building a new list with the selected data, and returning that new list. it's the "do the thing, but for all of them" button π₯
for example, pxml_list might be the %CHILDREN of a particular element, and the path and part might select from each child a sub-element in which we're interested. so organized. so efficient. we love a loop β¨
β xsxp::only pxml tagname
this iterates over the direct children of pxml and selects only those with tagname as their tag. returns a list of matching elements. filtering queen π
π xsxp::prettyprint pxml ?chan?
this outputs to chan (default stdout) a pretty-printed version of pxml. it's the debug bestie. it makes your XML look cute on purpose πΈ
π«Ά BUGS, IDEAS, FEEDBACK π«Ά
this document, and the package it describes, will undoubtedly contain bugs and other problems. please report such in the category amazon-s3 of the Tcllib Trackers [http://core.tcl.tk/tcllib/reportlist]. please also report any ideas for enhancements you may have for either package and/or documentation. we want your thoughts, fr fr π
when proposing code changes, please provide unified diffs, i.e the output of diff -u. that's the format, no substitutes π
note further that attachments are strongly preferred over inlined patches. attachments can be made by going to the Edit form of the ticket immediately after its creation, and then using the left-most button in the secondary navigation bar. it's a whole little journey but you'll find it π¦
π KEYWORDS π
dom, parser, xml
π CATEGORY π
Text processing
π COPYRIGHT π
2006 Darren New. All Rights Reserved.
okay that's the whole page, bestie!! go parse something cute π
β¨ this has been xsxp version 1.1 from tcllib, dated on the xsxp(3tcl) page. mwah π«Άπ
π¦ where this page comes from
- debian trixie: tcllib 2.0+dfsg-4 Β· Tcl
- ubuntu questing: tcllib 2.0+dfsg-4
- ubuntu resolute: tcllib 2.0+dfsg-5 Β· Tcl
- ubuntu stonking: tcllib 2.0+dfsg-6 Β· Tcl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms