Unix & Linux Stack Exchange
Q&A for users of Linux, FreeBSD and other Unix-like operating systems
Latest Questions
0
votes
1
answers
57
views
Unable to extract some properties from Maven settings.xml using either xmllint or xmlstarlet
I have a Maven settings file at ~/.m2/settings.xml. I need to write some scripting that extracts the username and password from one of the server entries. I've tried using both "xmllint" and "xmlstarlet" using what I believe are the correct (different) command-line parameters for both of those. The...
I have a Maven settings file at ~/.m2/settings.xml. I need to write some scripting that extracts the username and password from one of the server entries.
I've tried using both "xmllint" and "xmlstarlet" using what I believe are the correct (different) command-line parameters for both of those. The result for both is just an empty string. There is no error, it just prints nothing.
This is an excerpt of ~/.m2/settings.xml:
central
...
...
...
This is working fine with Maven.
For xmllint, I'm trying the following:
xmllint --xpath 'string(//server[id="central"]/username/text())' ~/.m2/settings.xml
For xmlstarlet, it is this:
xmlstarlet sel -T -R -D -t -v "//settings/servers/server[id='central']/username/text()" ~/.m2/settings.xml
Those are slightly semantically different, but I've tried several variations. All of these just print an empty string.
David M. Karr
(1173 rep)
Sep 26, 2024, 08:58 PM
• Last activity: Sep 26, 2024, 09:09 PM
2
votes
2
answers
5218
views
How can I use xmlstarlet to query a tag for two attribute values?
Example: ```xml ``` I'd like to select `@id="cover"` and `media-type="image/*"` and retrieve `@href`. My current "solution" seems to be... ```bash xmlstarlet sel -t -m "//_:item[@id='$opf_cover_name']" -v @href -o '|' -v @media-type -n file.xml ``` ...followed by some extremely messy splitting aroun...
Example:
I'd like to select @id="cover"
and media-type="image/*"
and retrieve @href
.
My current "solution" seems to be...
xmlstarlet sel -t -m "//_:item[@id='$opf_cover_name']" -v @href -o '|' -v @media-type -n file.xml
...followed by some extremely messy splitting around the '|' character of the output.
Does xmlstarlet have a substring function?
$ xmlstarlet --version
Version = 1.6.1
compiled against libxml2 2.9.4, linked with 20904
compiled against libxslt 1.1.29, linked with 10132
Jeremy Boden
(1344 rep)
Nov 28, 2021, 11:01 PM
• Last activity: Sep 24, 2024, 05:57 PM
6
votes
1
answers
4507
views
Extract value of element in XML with xmlstarlet
My situation is very specific, and I am a beginner with `xmlstarlet`. I have an XML file like the one below, which contains hundreds of entries. ```xml 2008-06-04 16:40 Daniel New name of - Just an example.pdf Just an example.pdf ``` Where **root** = netbooks tag. **QUESTIONS** 1. I have the file ha...
My situation is very specific, and I am a beginner with
xmlstarlet
.
I have an XML file like the one below, which contains hundreds of entries.
2008-06-04 16:40
Daniel
New name of - Just an example.pdf
Just an example.pdf
Where **root** = netbooks tag.
**QUESTIONS**
1. 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?
2. Also, how to get the **attribute** id
(that is 2
) from **file** element? Again, I have the hash, just it.
Daniel
(432 rep)
Jul 4, 2023, 01:22 PM
• Last activity: Aug 6, 2024, 01:13 PM
1
votes
1
answers
92
views
How to assign matching node values from xmlstarlet to an array in bash?
The loop below finds all of the nodes where the value equals "DOWNLOADING" and grabs the value of the corresponding "bytesdownloaded" node. I was expecting `adlBytesDL1[0]` to equal `147333120` (from the first matched node); instead, it equals `147333120147329024` (node1 and node2 back to back). I a...
The loop below finds all of the nodes where the value equals "DOWNLOADING" and grabs the value of the corresponding "bytesdownloaded" node. I was expecting
adlBytesDL1
to equal 147333120
(from the first matched node); instead, it equals 147333120147329024
(node1 and node2 back to back). I assume I am putting my index in the wrong place, but I cannot figure out where it belongs. Any help is greatly appreciated. I have to use XMLSTARLET with Xpath 1.0.
for ((i=0; i
2284879
InProgress
true
Sun, 30 Jun 2024 14:48:39 +0000
2284879
1
cefbabced653b2837ccd4bd906841390b1f87227
1869565324
http://servername.com/A13_13.26.9U3.zip
DOWNLOADING
Sun, 30 Jun 2024 14:48:39 +0000
0
false
147333120
1869565324
5
2284879
2
7e90e741c6ffd3476bdbae0262ff5a6aad49b925
1877973677
http://servername.com/A13_13.29.10U0.zip
DOWNLOADING
Sun, 30 Jun 2024 14:48:39 +0000
0
false
147329024
1877973677
5
```
webbytr
(13 rep)
Aug 5, 2024, 08:54 PM
• Last activity: Aug 6, 2024, 05:47 AM
0
votes
1
answers
902
views
Insert a tag with a unique identifier into a XML
I'm able to add a new node with a custom fixed value to my XML file in this way, with [xmlstarlet][1]: xmlstarlet ed --subnode "/legge190/data/lotto" --type elem -n newsubnode \ -v "myvalue" But how to add a unique identifier? If I try to add a unique identifier using `generate-id(.)` xmlstarlet ed...
I'm able to add a new node with a custom fixed value to my XML file in this way, with xmlstarlet :
xmlstarlet ed --subnode "/legge190/data/lotto" --type elem -n newsubnode \
-v "myvalue"
But how to add a unique identifier?
If I try to add a unique identifier using
generate-id(.)
xmlstarlet ed --subnode "/legge190/data/lotto" --type elem -n newsubnode \
-v "generate-id(.)"
I do not have an id value, but the generate-id(.)
string.
Thank you
aborruso
(2995 rep)
Sep 24, 2017, 03:42 PM
• Last activity: Jul 23, 2024, 10:45 AM
0
votes
1
answers
392
views
XMLStarlet with using a variable in XPath expression
I have this command: ``` find . -iname '*.xml' | xargs xmlstarlet sel -N z="http://abc.com/article/1.0/" \\ --var zgtag="SuperTag" -t -m "/z:profile-extension" -i "//z:tag='$zgtag'" \\ -n -f -o "," -v "count(//z:zgContextsAnonymTotal[z:tag='$zgtag'])" -o "," \\ -v "//z:zgContextsAnonymTotal/z:tag[.=...
I have this command:
find . -iname '*.xml' | xargs xmlstarlet sel -N z="http://abc.com/article/1.0/ " \\
--var zgtag="SuperTag" -t -m "/z:profile-extension" -i "//z:tag='$zgtag'" \\
-n -f -o "," -v "count(//z:zgContextsAnonymTotal[z:tag='$zgtag'])" -o "," \\
-v "//z:zgContextsAnonymTotal/z:tag[.='$zgtag']/@z:timestamp" -o "," \\
-v "count(//z:zgContextsREGTotal[z:tag='$zgtag'])" -o "," \\
-v "//z:zgContextsREGTotal/z:tag[.='$zgtag']/@z:timestamp" -o "," \\
-v "count(//z:zgContextsPremiumTotal[z:tag='$zgtag'])" -o "," \\
-v "//z:zgContextsPremiumTotal/z:tag[.='$zgtag']/@z:timestamp" | grep -v ^$
It does not work because the command version without variable (same value repeated instead of using $zgtag
show results. What is wrong?
**My investigation so far pointed out:**
- It has to be --var zgtag="'SuperTag'"
because without single quotes the compiled XSLT by xmlstarlet would produce the following variable declaration: `` which is obviously wrong, the single quotes are missing!
- In the XSLT compiled by xmlstarlet I can see that the variable is NOT inserted correctely, e.g. I see this:
Then I used this version: --var zgtag="'SuperTag'" -m "/z:profile-extension" -i '//z:tag=$zgtag'
, now I get a correctly compiled XSLT debugging output of xmlstarlet:
However the output still is empty, the same script without the variable (SuperTag
directly inserted into the script multiple times) works just fine. So there must be still a problem with this var-styled command.
So any help welcome how to insert and use the defined variable correctly. The definition is not the problem, it's the usage of the variable in the xmlstarlet command.
basZero
(153 rep)
Feb 7, 2024, 09:56 AM
• Last activity: Feb 8, 2024, 08:21 AM
3
votes
1
answers
428
views
'xmlstarlet edit' using a variable as XPath
When you follow the doc: xmlstarlet edit --help you can read that `--var` can be used to declare a XPath expression as a variable. Generating moc file: cat /tmp/file.xml x y EOF This one works, without variables: xmlstarlet edit \ --var xp '//elt/following::comment()' \ -a '//elt/following::comment(...
When you follow the doc:
xmlstarlet edit --help
you can read that
--var
can be used to declare a XPath expression as a variable.
Generating moc file:
cat /tmp/file.xml
x
y
EOF
This one works, without variables:
xmlstarlet edit \
--var xp '//elt/following::comment()' \
-a '//elt/following::comment()' -t elem -n p -v 'some new text' \
-a '//elt/following::comment()' -t elem -n p -v 'some other text' \
/tmp/file.xml
This one doesn't edit with variables:
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/file.xml
What does I miss to use variable?
Mévatlavé Kraspek
(541 rep)
Feb 28, 2023, 08:33 AM
• Last activity: Feb 28, 2023, 09:00 AM
4
votes
2
answers
3878
views
Use xmlstarlet to remove an entire element that matches an attribute value?
My question is similar to [sed - Delete XML node containing certain element - Unix & Linux Stack Exchange](https://unix.stackexchange.com/questions/72157/delete-xml-node-containing-certain-element). Trying to implement the suggestions there has kept me busy all day, but I haven't managed to get...
My question is similar to [sed - Delete XML node containing certain element - Unix & Linux Stack Exchange](https://unix.stackexchange.com/questions/72157/delete-xml-node-containing-certain-element) . Trying to implement the suggestions there has kept me busy all day, but I haven't managed to get anything to work, so I am posting a question.
Within a bash script, I need to remove an entire `
element when the
id attribute matches a given value. I'm actually using user-groups to do part of this. Say a user is **not** in the group
folder_a; then the entire
element with attribute
id=".Folder_A"` should be deleted from config.xml. (I also delete the folder from disk.)
My bash script looks like this:
#!/bin/bash
grouplist=$(groups $theuser);
for foldername in '.Folder_A' '.Folder_B'; do
grpnm="${foldername,,}"|sed -e 's/^.//'
if ! [[ $grouplist =~ ${grpnm} ]]; then
perl -0777 -pe "s|()|$1=~ /id=\"$foldername\"/?"":$1|gse" config.xml > config.xml
rm -fr "$foldername"
else
echo "permitting access to ${foldername}"
fi
done
The perl command is **not** working. It is just one of many variants I have tried. I also tried sed. I would prefer to use xmlstarlet, but I had even more trouble with it.
**Edit** - I just found this answer: https://unix.stackexchange.com/a/339089/393289
It helped me come up with this:
xml ed -d '//configuration/folder[contains(@id,".Folder_A")]' config.xml
I feel like I am closer now. (I tried to upvote that answer but I don't have enough rep yet.) However, I can't translate the attribute name into a bash variable yet due to the quote marks or something else.
BTW, how do I make that perform an in-place edit (similar to the sed -i
command) once I get it working?
Here's an example config.xml file:
basic
0
0
0
false
false
false
25
.stfolder
basic
0
0
0
false
false
false
25
.stfolder
false
true
0
0
0
false
false
true
0
0
0
false
127.0.0.1:8384
98qewr0qe9r
default
0
0
60
false
10
false
-1
3
false
1800
true
false
24
false
5
false
false
10
0
~/Sync/
0
false
auto
0
false
false
This is a config.xml example for syncthing.
J-S
(43 rep)
Jan 1, 2021, 02:37 AM
• Last activity: Feb 27, 2023, 09:04 AM
4
votes
1
answers
334
views
Sum values in xmlstarlet from two siblings?
I am using `xmlstarlet` to extract text from elements with a specific preceding sibling event. An example from the XML file: ```xml 11.134 0.2 CUER 11.367 1.26 TARGETR 379 1 YOU WIN! 12.651 2.65 1 YOU WIN! ``` From the example, I need to do the following: 1. print the `onset` of ` $DIR/output.stf ``...
I am using
xmlstarlet
to extract text from elements with a specific preceding sibling event. An example from the XML file:
11.134
0.2
CUER
11.367
1.26
TARGETR
379
1
YOU WIN!
12.651
2.65
1
YOU WIN!
From the example, I need to do the following:
1. print the onset
of ` $DIR/output.stf
```
Though as written, the following code only displays the duration from the matched element rather than summing the duration of the two adjacent events. **Is the latter possible using xmlstarlet?**
Thank you for your help!
XMLnewbie
(43 rep)
Feb 14, 2023, 10:07 PM
• Last activity: Feb 16, 2023, 05:13 PM
3
votes
2
answers
1451
views
Remove all namespace attributes from xml using xmlstarlet
I want to remove all the namespace attributes from the following XML. I have tried to remove `xmlns` using following command which does not change the input in anyway. xmlstarlet ed \ -N n="urn:oasis:names:tc:SAML:2.0:metadata" \ -d '/md:EntityDescriptor/@*[n:name()="xmlns"]' \ ' \ > ttt.xml Expecte...
I want to remove all the namespace attributes from the following XML.
I have tried to remove
xmlns
using following command which does not change the input in anyway.
xmlstarlet ed \
-N n="urn:oasis:names:tc:SAML:2.0:metadata" \
-d '/md:EntityDescriptor/@*[n:name()="xmlns"]' \
' \
> ttt.xml
Expected output:
s1n7ax
(437 rep)
Oct 12, 2022, 08:23 AM
• Last activity: Oct 16, 2022, 11:13 PM
1
votes
1
answers
146
views
How to download latest MediaWiki from command line?
One can download MediaWiki via a web browser GUI here: https://www.mediawiki.org/wiki/Download The download link there isn't version agnostic (it changes any time a new version comes out), that makes it impossible to download by the same link because the link always changes. How could I still downlo...
One can download MediaWiki via a web browser GUI here:
https://www.mediawiki.org/wiki/Download
The download link there isn't version agnostic (it changes any time a new version comes out), that makes it impossible to download by the same link because the link always changes.
How could I still download the latest MediaWiki in a version agnostic way? How to "target" the dynamic download link from command line? Possibly some regex is needed but possibly not.
automation
(57 rep)
Oct 7, 2022, 10:40 AM
• Last activity: Oct 8, 2022, 12:04 PM
5
votes
2
answers
579
views
how to delete a div with a specific class from XHTML using xstarlet?
I have several hundred .xhtml files in a sub-directory(*) and I want to delete all DIVs with a specific class (and the entire contents of those DIVs - including other divs, spans, image and paragraph elements) from them. The DIV may appear zero, one, or more times at any arbitrary depth within each...
I have several hundred .xhtml files in a sub-directory(*) and I want to delete all DIVs with a specific class (and the entire contents of those DIVs - including other divs, spans, image and paragraph elements) from them. The DIV may appear zero, one, or more times at any arbitrary depth within each .xhtml file.
The specific DIVs I want to delete are:
Using the xml_grep
utility from the perl [XML::Twig](https://metacpan.org/release/XML-Twig) module, I can run xml_grep -v 'div[@class="portlet solid author-note-portlet"]' file*.xhtml
and it will remove all instances of that div from the .xhtml files and display the result on stdout. Exactly what I want, except for "display on stdout".
If xml_grep
had some kind of in-place edit option, that would be fine, I'd just use that....but it doesn't, so I'd have to write a wrapper script that used a temporary file or sponge
and run xml_grep against each .xhtml file individually, which would be slow and tedious. Or I could hack a copy of xml_grep so that it could edit its input file(s).
But I don't want to do either of these things, I want to use the existing tool which can already do this, I want to use xmlstarlet
- it'll be faster, has in-place edit, and I won't have to run it once per filename.
The trouble is that no matter what I try (and I have tried dozens of variations), I cannot figure out the correct xpath specification to delete a div with this class. e.g. I have tried:
xmlstarlet ed -d "div[@class='portlet solid author-note-portlet']" file.xhtml
and (with different quoting)
xmlstarlet ed -d 'div[@class="portlet solid author-note-portlet"]' file.xhtml
and
xmlstarlet ed -d '//html/body/div/div/div[@class="portlet solid author-note-portlet"]'
and dozens of other variations. None of them have resulted in any change to the xhtml output. This is the point at which I usually give up on xmlstarlet and write a perl script, but this time I'm determined to do it with xmlstarlet.
So, what's the correct way to specify this div class for xmlstarlet?
BTW, for one example .xhtml file (with two instances of this div, which happen to be at the same depth...which is fairly typical but not universal), xmlstarlet el -v
says:
$ xmlstarlet el -v OEBPS/file0007.xhtml | grep author-note-portlet
html/body/div/div[@class='portlet solid author-note-portlet']
html/body/div/div[@class='portlet solid author-note-portlet']
---
(*) Not that it matters, but these .xhtml files are inside a .epub file(**) generated by the [FanFicFare](https://github.com/JimmXinu/FanFicFare) plugin for [Calibre](https://calibre-ebook.com) - which downloads all chapters from books on various fiction web sites and turns them into an epub file (which is basically a zip archive containing XHTML and CSS files and maybe jpeg or gif files, along with a bunch of metadata files).
cas
(81942 rep)
Sep 15, 2022, 12:55 AM
• Last activity: Sep 15, 2022, 01:39 PM
5
votes
2
answers
1608
views
Deleting an XML tag in a huge file
I have difficulty parsing a huge XML file (about 100GB with large nodes). I am trying to reduce the node sizes by deleting unnecessary tags. For example, any ` ` tags. If I use native XML parsers such as `xmlstarlet` xmlstarlet ed -P -d '//text' file.xml I face the same problem of being out of memor...
I have difficulty parsing a huge XML file (about 100GB with large nodes). I am trying to reduce the node sizes by deleting unnecessary tags. For example, any `` tags.
If I use native XML parsers such as
xmlstarlet
xmlstarlet ed -P -d '//text' file.xml
I face the same problem of being out of memory.
Is there a safe way (with little memory footprint) to remove all `` pairs without breaking the XML structure?
Googlebot
(2009 rep)
Jun 4, 2022, 01:07 AM
• Last activity: Jun 4, 2022, 04:18 PM
2
votes
1
answers
144
views
How to untag, in an XML file, a chain of tagged characters that are embedded within specified tags
What is the xmlstarlet command to untag, in an XML file, a chain of tagged characters that are embedded within specified tags? Example: searching every occurrence of the tag ` `, that needs to be deleted only if it is within the ` ... ` tag: * Input example: ```xml This is an example . This is a tes...
What is the xmlstarlet command to untag, in an XML file, a chain of tagged characters that are embedded within specified tags?
Example: searching every occurrence of the tag
, that needs to be deleted only if it is within the ...
tag:
* Input example:
This is an example. This is a test;
* Desired output:
This is an example. This is a test;
For the sake of generalization, I am keeping the second sentence in the example, i.e.
This is a test;
but if it is an issue, it can be ignored.
Matt5
(87 rep)
May 7, 2022, 10:19 AM
• Last activity: May 7, 2022, 02:29 PM
4
votes
1
answers
915
views
How to rename, in an XML file, specific tags that are embedded in some other specific tags
What is the xmlstarlet command to replace a tag value by another one, only for specified tags embedded in other specified tags? Example: searching every occurance of the tag `b`, that needs to be replaced by `d` only if it is within the ` ... ` tag (inside other tags): * Input example: ```xml This i...
What is the xmlstarlet command to replace a tag value by another one, only for specified tags embedded in other specified tags?
Example: searching every occurance of the tag
b
, that needs to be replaced by d
only if it is within the ...
tag (inside other tags):
* Input example:
This is an example. This is a test;
* Desired output:
This is an example. This is a test;
This post is related to https://unix.stackexchange.com/q/700179/523366 in the solution I am trying to find to format xml files.
Matt5
(87 rep)
May 6, 2022, 12:05 PM
• Last activity: May 6, 2022, 12:20 PM
-1
votes
1
answers
56
views
Change the version number from 97 to 98 in an XML file
I want to change the version number with respect to the ip. For example for this ip `10.228.134.226`, it should change the version number from `97` to `98` ```xml ``` Here I want to change the value of `defaultVersion` so how do we do that ?
I want to change the version number with respect to the ip. For example for this ip
10.228.134.226
, it should change the version number from 97
to 98
Here I want to change the value of defaultVersion
so how do we do that ?
raghav
(11 rep)
Mar 17, 2022, 04:23 AM
• Last activity: Mar 17, 2022, 01:17 PM
1
votes
2
answers
915
views
I want to add a namespace prefix to the tags of the default name space of an XML document from the shell
I have a SVG file, which is an XML document: I want to add the svg: prefix to all the tags corresponding to the default namespace in order to get next output: I am sure this is possible in one or few command lines from the shell using **xmllint** and/or **xmlstarlet** but I can't manage it.
I have a SVG file, which is an XML document:
I want to add the svg: prefix to all the tags corresponding to the default namespace in order to get next output:
I am sure this is possible in one or few command lines from the shell using **xmllint** and/or **xmlstarlet** but I can't manage it.
Pierre François
(143 rep)
Mar 20, 2020, 10:38 AM
• Last activity: Jan 26, 2022, 10:00 AM
5
votes
4
answers
904
views
Can I convert fields in XML to tags using xmlstarlet?
I want to convert fields in Tags to tags within that tag for example ```xml ``` to ```xml 250 350 ``` I want to perform this operation in Linux command line using `xmlstarlet` or `sed` .
I want to convert fields in Tags to tags within that tag for example
to
250
350
I want to perform this operation in Linux command line using xmlstarlet
or sed
.
krishna
(529 rep)
Aug 13, 2015, 09:34 AM
• Last activity: Jan 24, 2022, 10:54 AM
0
votes
1
answers
478
views
xmlstarlet command for batch processing .xhtml files
I've tried xmlstarlet for processing an individual .xhtml files. xmlstarlet fo --omit-decl --recover --html file.xhtml This command process an individual files and prints output on terminal only. How to batch process multiple files and save results on `stdout` as a files? (with same name, by adding...
I've tried xmlstarlet for processing an individual .xhtml files.
xmlstarlet fo --omit-decl --recover --html file.xhtml
This command process an individual files and prints output on terminal only.
How to batch process multiple files and save results on
stdout
as a files? (with same name, by adding prefix or suffix for files)
minto
(575 rep)
Oct 3, 2021, 11:27 AM
• Last activity: Oct 3, 2021, 01:25 PM
1
votes
0
answers
232
views
XMLstarlet to fix image tags and replace path for images
I have multiple .XHTML files in the folder. The top declaration part is as follows: First, I don't want alter top head part. I want process files in batch and fix two things, 1) terminate image end tags properly `'/>'`, same for ` ` and ` ` tags. 2) replace path in all images (preserving name), i.e....
I have multiple .XHTML files in the folder. The top declaration part is as follows:
First, I don't want alter top head part.
I want process files in batch and fix two things,
1) terminate image end tags properly
` tags. 2) replace path in all images (preserving name), i.e. from
'/>'
, same for ` and
` tags. 2) replace path in all images (preserving name), i.e. from

to

Tried xmlstarlet (v1.6.1), xmlstarlet fo --recover --html file.xhtml
but it alters top declaration part, adding extra stuff at the top:
Also warns about invalid tag
file.xhtml:8.54: Tag section invalid
^
what is correct commands? First I need 'dry run' to see changes, if OK then apply in place.
minto
(575 rep)
Sep 27, 2021, 09:19 PM
• Last activity: Sep 27, 2021, 10:07 PM
Showing page 1 of 20 total questions