简体   繁体   中英

Count occurrence of a tag

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>' xml_grep 'finstance' ../../recent/*.xml | xargs | grep -o '<para>' 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?

xmllint is probably what you are looking for.

ie

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM