1

I'm trying to update the value of a field in a specific block but that value is in a wrapped attribute (not sure what the proper name should be), ie: <target_tag name="my_tag" default="value_to_update"/> rather than: <target_tag name="my_tag">value_to_update</target_tag>

I've been searching how to this and it seems to be something like:

xmllint --shell my_file.xml << EOF
cd //target_tag[@name="my_tag"]/default
set new_value
save
EOF

but this would work if the XML was of the second kind. How can I access/update the wrapped attribute? I can't seem to find the answer...

1

1 Answer 1

3

You were close!
The cd shell command must refer to the @default attribute

xmllint --shell tmp.xml << EOF
cd //target_tag[@name="my_tag"]/@default
cat .
set new_value
save
bye
EOF

cat tmp.xml

Console output

/ > cd //target_tag[@name="my_tag"]/@default
default > cat .
 -------
 default="value_to_update"
default > set new_value
default > save
default > bye
<?xml version="1.0" encoding="UTF-8"?>
<root>
    <target_tag name="my_tag" default="new_value"/>
</root>

xmllint version

xmllint --version
xmllint: using libxml version 20914
Sign up to request clarification or add additional context in comments.

3 Comments

Ah nice, thank you! That works with the dummy file I posted but not with my actual file. It looks like loading with the xpath works: xmllint --xpath "//target_tag[@name=\"my_tag\"]/@default" /tmp/tmp.xml but when I try the EOF approach, I get warning: failed to load external entity "tmp.xml"
Nevermind, I'd accidentally typo'd the filename, thanks again!
Follow-up question, if you have any ideas there: stackoverflow.com/questions/77065040/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.