How to parse out the value of several attributes on different elements with xmllint/xpath?

Claus Jørgensen

For a given xml file called configurations.xml I would like to extract the value of each conf element, and store it in a variable for later use.

<configurations>
  <conf name="bob"/>
  <conf name="alice"/>
  <conf name="ted"/>
  <conf name="carol"/>
</configurations>

The expected output is:

bob
ailce
ted
carol

I have xpath and xmllint available. A xpath of //conf/@name gets the nodes, but outputs as name="bob", which is what I'm trying to avoid.

gniourf_gniourf

I don't know how to achieve what you're trying to achieve with xmllint only.

Since you have xpath installed, you have Perl's XML::XPath too. So a little bit of Perl:

#!/usr/bin/perl

use XML::Path;

my $xp=XML::XPath->new(filename => 'configurations.xml');

my $nodeset=$xp->find('//conf/@name');
foreach my $node ($nodeset->get_nodelist) {
    print $node->getNodeValue,"\0";
}

will output what you want, separated with a nil character.

In a one-liner style:

perl -mXML::XPath -e 'foreach $n (XML::XPath->new(filename => "configurations.xml")->find("//conf/\@name")->get_nodelist) { print $n->getNodeValue,"\0"; }'

To retrieve them in, e.g., a Bash array:

#!/bin/bash

names=()
while IFS= read -r -d '' n; do
    names+=( "$n" )
done < <(
    perl -mXML::XPath -e 'foreach $n (XML::XPath->new(filename => "configurations.xml")->find("//conf/\@name")->get_nodelist) { print $n->getNodeValue,"\0" }'
)
# See what's in your array:
display -p names

Note that at this point you have the option of turning to Perl and drop Bash completely to solve your problem.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to declare several stylable attributes with the same name for different tags?

From Dev

How can I assign different data-attributes to cloned DOM elements with jQuery .clone()

From Dev

How to get data attributes value from child elements in angular controllers

From Dev

How to add attributes to custom elements?

From Dev

How to put several elements into one?

From Dev

How to add data-attributes with no value to elements

From Dev

Use Parsec to parse several different kinds of fields

From Dev

How to parse out the value of several attributes on different elements with xmllint/xpath?

From Dev

How to parse out data of a string

From Dev

Unsure how to web-scrape a specific value that could be in several different places

From Dev

Parse time elapsed / time span / duration with several different formats

From Dev

How to append an object to several elements?

From Dev

How to parse out {'value__avg': 46.26524716693248} 'value_avg' when using aggregate(Avg()) in django

From Dev

How to parse same named child elements based on attributes with XSLT

From Dev

Add different attributes to same elements jquery

From Dev

How to get data attributes value from child elements in angular controllers

From Dev

JSP tag attributes with different value types

From Dev

How to get Sibling Elements when siblings have same name but different attributes using Java and Xerces NodeIterator

From Dev

How to find entities with one of several attributes in Datomic

From Dev

Parse time elapsed / time span / duration with several different formats

From Dev

Regexp: find out if value that repeats several times

From Dev

get xml elements using attributes value

From Dev

Swift - Parse - How to loop into PFObject attributes

From Dev

How to extract the complete data from an R list with elements with different attributes when applying a mathematical function?

From Dev

Parse string with JS to pull out words between several special characters

From Dev

How to get value of XML tag with different attributes?

From Dev

XQuery/XPath: How to count occurrences of a specific value in different elements?

From Dev

Deserializing elements with both attributes and value

From Dev

Python - How to parse xml response and store a elements value in a variable?

Related Related

  1. 1

    How to declare several stylable attributes with the same name for different tags?

  2. 2

    How can I assign different data-attributes to cloned DOM elements with jQuery .clone()

  3. 3

    How to get data attributes value from child elements in angular controllers

  4. 4

    How to add attributes to custom elements?

  5. 5

    How to put several elements into one?

  6. 6

    How to add data-attributes with no value to elements

  7. 7

    Use Parsec to parse several different kinds of fields

  8. 8

    How to parse out the value of several attributes on different elements with xmllint/xpath?

  9. 9

    How to parse out data of a string

  10. 10

    Unsure how to web-scrape a specific value that could be in several different places

  11. 11

    Parse time elapsed / time span / duration with several different formats

  12. 12

    How to append an object to several elements?

  13. 13

    How to parse out {'value__avg': 46.26524716693248} 'value_avg' when using aggregate(Avg()) in django

  14. 14

    How to parse same named child elements based on attributes with XSLT

  15. 15

    Add different attributes to same elements jquery

  16. 16

    How to get data attributes value from child elements in angular controllers

  17. 17

    JSP tag attributes with different value types

  18. 18

    How to get Sibling Elements when siblings have same name but different attributes using Java and Xerces NodeIterator

  19. 19

    How to find entities with one of several attributes in Datomic

  20. 20

    Parse time elapsed / time span / duration with several different formats

  21. 21

    Regexp: find out if value that repeats several times

  22. 22

    get xml elements using attributes value

  23. 23

    Swift - Parse - How to loop into PFObject attributes

  24. 24

    How to extract the complete data from an R list with elements with different attributes when applying a mathematical function?

  25. 25

    Parse string with JS to pull out words between several special characters

  26. 26

    How to get value of XML tag with different attributes?

  27. 27

    XQuery/XPath: How to count occurrences of a specific value in different elements?

  28. 28

    Deserializing elements with both attributes and value

  29. 29

    Python - How to parse xml response and store a elements value in a variable?

HotTag

Archive