0

I have a few XML files and would like to count how many times the tag <para> occurs.

       <values>
          <fnstance>
            <value>00</value>
            <description>
              <para>Some text.</para>
            </description>
          </finstance>
          <finstance>
            <value>01</value>
            <description>
              <para>When this text</para>
            </description>
            <description>
              <para>Some other text for same value</para>
            </description>
          </finstance>
        </values>

I would like to find the tag <para> and count them for a each <value> tag. Is there a way to do this in Unix?

I tried xml_grep 'finstance' ../../recent/*.xml | xargs | grep -o '<para>' which gives me the list of <para> elements in a give file. But I don't know how to do that for each value tag. How to do this?

user4157124
  • 2,809
  • 13
  • 27
  • 42
user1958532
  • 113
  • 1
  • 13

1 Answers1

0

xmllint is probably what you are looking for.

i.e.

xmllint --xpath '//value' yourfile.xml | sed s/\>\</\>\\n\</g

will return

<value>00</value>
<value>01</value>

This Question will give you some guidance on querying your xml with the --xpath option to xmllint.

Niall Cosgrove
  • 1,273
  • 1
  • 15
  • 24