2. Studying Structure of XML Document

Before you do anything with your XML document you probably would like to know its structure at first. 'el' option could be used for this purpose.

Let's say you have the following XML document (table.xml)

<xml>
  <table>
    <rec id="1">
      <numField>123</numField>
      <stringField>String Value</stringField>
    </rec>
    <rec id="2">
      <numField>346</numField>
      <stringField>Text Value</stringField>
    </rec>
    <rec id="3">
      <numField>-23</numField>
      <stringField>stringValue</stringField>
    </rec>
  </table>
</xml>
xml el table.xml

would produce the following output.

xml
xml/table
xml/table/rec
xml/table/rec/numField
xml/table/rec/stringField
xml/table/rec
xml/table/rec/numField
xml/table/rec/stringField
xml/table/rec
xml/table/rec/numField
xml/table/rec/stringField

Every line in this output is an XPath expression which indicates a 'path' to elements in XML document. You would use these XPath expressions to navigate through your XML documents in other XmlStarlet options.

XML documents can be pretty large but with a very simple structure. (This is espesially true for data driven XML documents ex: XML formatted result of select from SQL table). If you just interested in structure but not order of the elements you can use -u switch combined with 'el' option.

EXAMPLE:

xml el -u table.xml

Output:

xml
xml/table
xml/table/rec
xml/table/rec/numField
xml/table/rec/stringField

If you are interested not just in elements of your XML document, but you want to see attributes as well you can use -a switch with 'el' option. And every line of the output will still be a valid XPath expression.

EXAMPLE:

xml el -a table.xml

Output:

xml
xml/table
xml/table/rec
xml/table/rec/@id
xml/table/rec/numField
xml/table/rec/stringField
xml/table/rec
xml/table/rec/@id
xml/table/rec/numField
xml/table/rec/stringField
xml/table/rec
xml/table/rec/@id
xml/table/rec/numField
xml/table/rec/stringField

If you are looking for attribute values as well use -v switch of 'el' option. And again - every line of output is a valid XPath expression.

EXAMPLE:

xml el -v table.xml

Output:

xml
xml/table
xml/table/rec[@id='1']
xml/table/rec/numField
xml/table/rec/stringField
xml/table/rec[@id='2']
xml/table/rec/numField
xml/table/rec/stringField
xml/table/rec[@id='3']
xml/table/rec/numField
xml/table/rec/stringField