Skip to main content
9 votes

reading XML file and extract only node names and structure

The xmllint interactive shell command du appears to provide what you want: du PATH Show the structure of the subtree under the given path or the current node. If you want something non-...
steeldriver's user avatar
  • 83.8k
9 votes
Accepted

Change XML node's value, using sed?

You would use an XML parser to do this. For example xmlstarlet (a command line XML tool): $ xmlstarlet ed -u '//client-version' -v '1.2.9' file.xml <?xml version="1.0"?> <client-...
Kusalananda's user avatar
  • 356k
8 votes
Accepted

how to delete a div with a specific class from XHTML using xstarlet?

The correct way to do it with xmlstarlet is xmlstarlet ed --inplace -N xmlns="http://www.w3.org/1999/xhtml" \ --delete '//xmlns:div[@class="portlet solid author-note-portlet"]' ...
Kusalananda's user avatar
  • 356k
7 votes
Accepted

Edit XML using xmlstarlet only in one subnode

xmlstarlet solution: new_branch="DAVID" xmlstarlet ed -u "/configurations/rules/rule[name='APP1']/branch" -v "$new_branch" config.xml > final.xml The crucial <rule> node should look like: &...
RomanPerekhrest's user avatar
7 votes

reading XML file and extract only node names and structure

Since you're already using xmlstarlet you may as well continue using it. The xmlstarlet tool has an el (elements) sub-command which is used to "Display element structure of XML document". By ...
Kusalananda's user avatar
  • 356k
7 votes
Accepted

How to rename, in an XML file, specific tags that are embedded in some other specific tags

$ cat file <c>This is <b>an example</b>. <a>This is <b>a test;</b></a></c> $ xmlstarlet ed --rename '//a/b' -v 'd' file <?xml version="1.0&...
Kusalananda's user avatar
  • 356k
7 votes
Accepted

Extract value of element in XML with xmlstarlet

I have the file hash 5a43dc3524, i want to take the value of date, that is 2008-06-04 16:40. What's the easiest way to do this? We can get the date using the xpath expression //file[name/@hash="...
larsks's user avatar
  • 38.4k
6 votes

Deleting an XML tag in a huge file

I suggest you to give a try with xml_grep, it will be slow but memory efficient. It's a part of perl-XML-Twig (or xml-twig-tools) - Perl Module for Processing Huge Xml Documents in Tree Mode. You can ...
thanasisp's user avatar
  • 8,532
5 votes
Accepted

'xmlstarlet edit' using a variable as XPath

Use '$xp' to reference your variable: xmlstarlet edit \ --var xp '//elt/following::comment()' \ -a '$xp' -t elem -n p -v 'some new text' \ -a '$xp' -t elem -n p -v 'some other text' \ /tmp/...
Freddy's user avatar
  • 26.3k
4 votes
Accepted

Use xmlstarlet to remove an entire element that matches an attribute value?

Use the -L / --inplace option (see xmlstarlet edit --help) and the exact match from the linked answer: xmlstarlet ed -L -d "//configuration/folder[@id=\"$foldername\"]" config.xml
Freddy's user avatar
  • 26.3k
4 votes

Creating new elements with xmlstarlet

There’s a typo in your xmlstarlet invocation (lelvelb): xmlstarlet ed -a /levela/lelvelb -t elem -n levelc -v "" \ -i //levelc -t elem -n "element1" -v "value1" \ file.xml To add a subnode, ...
Stephen Kitt's user avatar
4 votes

How can I use xmlstarlet to query a tag for two attribute values?

the_id='cover' the_mediatype_prefix='image/' xmlstarlet sel -t \ --var queryid="'$the_id'" \ --var typeprefix="'$the_mediatype_prefix'" \ -v '//item[@id = $queryid and ...
Kusalananda's user avatar
  • 356k
4 votes

Deleting an XML tag in a huge file

The following XSLT 3.0 stylesheet will do the job: <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0"> <xsl:mode streamable="yes" ...
Michael Kay's user avatar
2 votes
Accepted

Rename XML elements to a sequence

My experience with XML command-line tools has left me with the feeling that there's a lot to be desired. Almost without fail I find myself giving up and just using Python. That said, I think I have an ...
igal's user avatar
  • 10.2k
2 votes
Accepted

Set bash variables from XML tags

Assuming the values never contain spaces you can do something like this: xmlstarlet sel -t -m '//KeyValuePair' -v 'concat(BASE-URL," ", Key, " ", Value, " ")' -nl file.xml | while read base_url key ...
Bodo's user avatar
  • 6,441
2 votes
Accepted

Parsing 12,000+ XML files

Both awk and sed are generally unsuitable for parsing XML (and other similar formats such as JSON and YAML etc.). For this XML example, for example, we don't know what order the nodes in InvoiceLines ...
Kusalananda's user avatar
  • 356k
2 votes

How to extract selected value in XML

Assuming you want to get the value of the playlist_url nodes for each channel node that has a protected node with value True: $ xmlstarlet sel -t -v '//channel[protected = "True"]/playlist_url' -nl ...
Kusalananda's user avatar
  • 356k
2 votes

Can I convert fields in XML to tags using xmlstarlet?

I'm aware that the question says "using xmlstarlet or sed", but either of those tools would require an awkward amount of typing (and using sed to modify any structured document format is not ...
Kusalananda's user avatar
  • 356k
2 votes
Accepted

insert a line into xml using xmlstarlet

Use two steps (I fold line for readability) xmlstarlet ed -s "//schema" -t elem -n "similarity" \ -i "//similarity" -t attr -n "class" -v "solr.BM25SimilarityFactory" \ > final....
Archemar's user avatar
  • 32.3k
2 votes
Accepted

how to parse the xml that have multiple properties and names

Using XMLStarlet to get the value of the prop with name "one thing": $ xmlstarlet sel -t -v '//prop[name = "one thing"]/value' -nl file.xml 2 This applies an XPATH query to the XML which selects the ...
Kusalananda's user avatar
  • 356k
2 votes

I want to add a namespace prefix to the tags of the default name space of an XML document from the shell

It's been a while since you asked. Nevertheless ... xmlstarlet edit's -r / --rename action requires a literal value for the new name so XPath functions are out. However, xmlstarlet select can be used ...
urznow's user avatar
  • 221
2 votes
Accepted

Update XML parameter, with namespace, using xmlstarlet

Your last try is almost correct, but you forgot to add the namespace to the name and value nodes: xmlstarlet ed \ -N s=http://bla.com/engine/management/engineConfiguration \ -u '/s:...
Kusalananda's user avatar
  • 356k
2 votes
Accepted

Convert this regex to be friendly within sed

This part of the regexp is the source of the error: [\s-.] If you want - to be treated as just another character in a bracket expression, then put it at the beginning or the end of the expression. ...
cas's user avatar
  • 83.9k
2 votes

How can I use xmlstarlet to query a tag for two attribute values?

You can add multiple conditions: xmlstarlet sel -t -m "//item[@id='cover'][starts-with(@media-type,'image/')]" -v '@href' -nl file.xml
Chris Davies's user avatar
2 votes

How to download latest MediaWiki from command line?

If you have an XML parser such as xmlstarlet that understands HTML you can use something like this: curl --silent https://www.mediawiki.org/wiki/Download | xmlstarlet format -H 2>/dev/null | ...
Chris Davies's user avatar
2 votes

Remove all namespace attributes from xml using xmlstarlet

I have found a couple of XSLT solutions to do this, both of which can conveniently be processed with xmlstarlet: How to remove the namespace and its prefixes in an XML file using XSLT? - IBM XSLT: ...
Chris Davies's user avatar
1 vote
Accepted

xmlstarlet command for batch processing .xhtml files

The code below is an only slightly modified variant from the end of my answer to your previous question on the same topic, utilizing identical ways of doing the actual batch processing. If the files ...
Kusalananda's user avatar
  • 356k
1 vote
Accepted

How to extract specific node values without travesing via xmlstarlet

You can print the values of the host XPATHs that do not have a hostname attribute: $ xmlstarlet sel -t -v '//host[not(hostname)]' -n file.xml cdn.cutshort.io www.cutshort.io blogdata.cutshort.io cdn....
steeldriver's user avatar
  • 83.8k
1 vote
Accepted

How to Search XML Block and Delete the same

Using xmlstarlet this should do the trick with properly formed xml xmlstarlet ed -d "//configProperty[.//value[contains(string(), 'oaea_server')]]" file1 Output has Server48 removed <?xml version=...
bu5hman's user avatar
  • 4,851
1 vote
Accepted

xmlstarlet how to select tag immediately before selected tag

Not sure it's the best solution, but here is how i managed to do it : xmlstarlet sel -t -c "SCRIBUSUTF8NEW/DOCUMENT/PAGEOBJECT/StoryText/trail[@PARENT='SearchedStyle']/preceding-sibling::ITEXT[1]...
JLuc's user avatar
  • 111

Only top scored, non community-wiki answers of a minimum length are eligible