The next time you're about to open an XML file to look at structured data, try using XMLStarlet and see if you can query that data instead. If you're looking to take advantage of XML, then XMLStarlet could be a good entry point. XML can seem over-verbose and unwieldy at times, but the tools built to interact with it consistently surprise me. These two expressions, in the case of the sample XML, do the same thing: $ xmlstarlet select -template \ The -match and -value-of options can overlap, so it's up to you how you want to use them together. In this example, I use the position() function to select a specific element in the spin node: $ xmlstarlet select -template \ In this example, I use the last() function to retrieve just the last element in the spin node: $ xmlstarlet select -template \ match '/xml/os/linux/distribution/spin' \Īs with navigating the DOM, you can use XPath expressions to limit the scope of what data is returned. ) to represent your current location: $ xmlstarlet sel -template \ Once you're there, you can use -value-of to specify which element you want a value for. Suppose you know that the node contains several elements. To help you keep your destination and the data "inside" it separate, XMLStarlet differentiates between what you're trying to match with the -match option and the value of the data you want with a -value-of option. It's not really a directory, at least not in the filesystem sense, but it is a collection of data that you can query. Matching paths and getting valuesĪn advantage of viewing XML tags as nodes is that once you find the node, you can think of it as your current "directory" of data. You could view all codenames for those releases, too, by changing the -value-of path to /xml/os/linux/distribution/codename. value-of '/xml/os/linux/distribution/release' \ To view all release numbers associated with the old name "Fedora Core" (the project dropped "Core" from the name from release 7 onward): $ xmlstarlet sel -template \ Imagine for a moment that the sample XML file contains all Fedora releases beginning with 1. Here's a test for the value of the element, which returns the release number associated only with a specific match. You can use square brackets as a test function, comparing the contents of an element to some value. XPath features a wealth of functions, and it's documented in detail by W3C, but I find Mozilla's XPath documentation more concise. One of the most powerful tools for navigating and parsing XML is called XPath. It governs the syntax used in XML searches and invokes functions from XML libraries. XMLStarlet understands XPath expressions, so you can make your selection conditional with an XPath function. value-of /xml/os/linux/distribution/name \ Narrow your focus by descending further into the DOM tree: $ xmlstarlet select -template \ I've removed some of the excess space in the sample output. The -nl stands for "new line," and it inserts copious amounts of whitespace to ensure your terminal prompt gets a new line after your results are in. The earlier in the Document Object Model (DOM) tree you start to explore, the more information you see: $ xmlstarlet select -template \ If you know the path to the node, specify the full path with the -value-of option. When looking for data in an XML file, your first task is to focus on the node you want to explore. You can view the data in XML with the xmlstarlet select ( sel for short) command. T or -text - output is text (default is XML) Q or -quiet - do not write anything to standard output. You can get further help by appending -help to the end of any of these subcommands: $ xmlstarlet sel -help Val (or validate) - Validate XML document(s) (well-formed/DTD/XSD/RelaxNG)Įl (or elements) - Display element structure of XML document Tr (or transform) - Transform XML document(s) using XSLT Sel (or select) - Select data or query XML document(s) (XPATH, etc) You can see what it has to offer by running the command along with the -help option: $ xmlstarlet -helpĮd (or edit) - Edit/Update XML document(s) For spontaneous XML interactions, I use xmlstarlet, a classic "Swiss Army knife"-style application that does the most common XML tasks. Sometimes you don't need to process XML data, though you just need a convenient way to extract important data from, update, or just validate it. There are many tools designed to help parse and transform XML data, including software libraries that let you write your own parser and complex commands like fop and xsltproc. Should all else fail, you can install it manually from the source code on Sourceforge. Free online course: RHEL Technical Overview.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |