Search code examples
xmlcountcharxmlstarlet

count chars between <> tags in XML


i'm not experienced user of Linux and have a big problem. I need to count all chars in XML file exept tags.

for example

<node>
 <node1>Text</node>
</node>

My output should return 4 or Text. I try to figure out

xmlstarlet

programm but i cant use it even after read manual.

Thanks for any advice


Solution

  • xml sel -t -v 'string-length(/*)' file.xml
    

    should give you the total number of characters of text content within the root element (including whitespace, so that would be 7 in your example - newline, space, T, e, x, t, newline). This would only be characters in text nodes, so would not include comments or processing instructions, i.e.

    <foo>This is a <!-- simple --> example</foo>
    

    would give you a length of 18.