how to insert elements into xml with xmlstarlet?
I want to insert additional elements into some variants of XML input. This script tries to demonstrate the input, and my attempt to insert code into the existing XML. As it can be seen, modifying a.xml
gives the expected output. But in b.xml
and c.xml
, the result is bogus. In b.xml
, the existing <c/>
is changed and another block of <b/>
is created. In c.xml
, the result is that d=""
is assigned twice.
There should be only a single <a><b>
, with several <c>
.
Any idea how to achieve that?
#!/bin/bash
# insert <a><b><c d="2"/>
set -e
td=`mktemp --directory --tmpdir=/dev/shm XXX`
trap "rm -rf '$td'" EXIT
xmlstarlet --version
pushd "$td"
cat > a.xml <<_EOX_
<a>
</a>
_EOX_
cat > b.xml <<_EOX_
<a>
<b>
<c/>
</b>
</a>
_EOX_
cat > c.xml <<_EOX_
<a>
<b>
<c d="1"/>
</b>
</a>
_EOX_
for i in *.xml
do
echo "$i"
cat "$i" |
xmlstarlet ed -O
-s 'a' -t elem -n b
-s 'a/b' -t elem -n c
-i 'a/b/c' -t attr -n d -v '2' |
xmlstarlet fo -o || echo "$?"
done
This produces the following output:
1.6.1
compiled against libxml2 2.9.7, linked with 20907
compiled against libxslt 1.1.32, linked with 10132
/dev/shm/tpX ~/work
a.xml
<a>
<b>
<c d="2"/>
</b>
</a>
37
b.xml
<a>
<b>
<c d="2"/>
<c d="2"/>
</b>
<b>
<c d="2"/>
</b>
</a>
80
c.xml
-:3.19: Attribute d redefined
<c d="1" d="2"/>
^
2
xml shell xpath xmlstarlet
add a comment |
I want to insert additional elements into some variants of XML input. This script tries to demonstrate the input, and my attempt to insert code into the existing XML. As it can be seen, modifying a.xml
gives the expected output. But in b.xml
and c.xml
, the result is bogus. In b.xml
, the existing <c/>
is changed and another block of <b/>
is created. In c.xml
, the result is that d=""
is assigned twice.
There should be only a single <a><b>
, with several <c>
.
Any idea how to achieve that?
#!/bin/bash
# insert <a><b><c d="2"/>
set -e
td=`mktemp --directory --tmpdir=/dev/shm XXX`
trap "rm -rf '$td'" EXIT
xmlstarlet --version
pushd "$td"
cat > a.xml <<_EOX_
<a>
</a>
_EOX_
cat > b.xml <<_EOX_
<a>
<b>
<c/>
</b>
</a>
_EOX_
cat > c.xml <<_EOX_
<a>
<b>
<c d="1"/>
</b>
</a>
_EOX_
for i in *.xml
do
echo "$i"
cat "$i" |
xmlstarlet ed -O
-s 'a' -t elem -n b
-s 'a/b' -t elem -n c
-i 'a/b/c' -t attr -n d -v '2' |
xmlstarlet fo -o || echo "$?"
done
This produces the following output:
1.6.1
compiled against libxml2 2.9.7, linked with 20907
compiled against libxslt 1.1.32, linked with 10132
/dev/shm/tpX ~/work
a.xml
<a>
<b>
<c d="2"/>
</b>
</a>
37
b.xml
<a>
<b>
<c d="2"/>
<c d="2"/>
</b>
<b>
<c d="2"/>
</b>
</a>
80
c.xml
-:3.19: Attribute d redefined
<c d="1" d="2"/>
^
2
xml shell xpath xmlstarlet
could you please inform us how the output should look like for the 3 cases?
– kvantour
Nov 16 '18 at 9:37
@kvantourThere should be only a single <a><b>, with several <c>.
– olh
Nov 16 '18 at 13:50
updated the answer
– kvantour
Nov 16 '18 at 13:58
add a comment |
I want to insert additional elements into some variants of XML input. This script tries to demonstrate the input, and my attempt to insert code into the existing XML. As it can be seen, modifying a.xml
gives the expected output. But in b.xml
and c.xml
, the result is bogus. In b.xml
, the existing <c/>
is changed and another block of <b/>
is created. In c.xml
, the result is that d=""
is assigned twice.
There should be only a single <a><b>
, with several <c>
.
Any idea how to achieve that?
#!/bin/bash
# insert <a><b><c d="2"/>
set -e
td=`mktemp --directory --tmpdir=/dev/shm XXX`
trap "rm -rf '$td'" EXIT
xmlstarlet --version
pushd "$td"
cat > a.xml <<_EOX_
<a>
</a>
_EOX_
cat > b.xml <<_EOX_
<a>
<b>
<c/>
</b>
</a>
_EOX_
cat > c.xml <<_EOX_
<a>
<b>
<c d="1"/>
</b>
</a>
_EOX_
for i in *.xml
do
echo "$i"
cat "$i" |
xmlstarlet ed -O
-s 'a' -t elem -n b
-s 'a/b' -t elem -n c
-i 'a/b/c' -t attr -n d -v '2' |
xmlstarlet fo -o || echo "$?"
done
This produces the following output:
1.6.1
compiled against libxml2 2.9.7, linked with 20907
compiled against libxslt 1.1.32, linked with 10132
/dev/shm/tpX ~/work
a.xml
<a>
<b>
<c d="2"/>
</b>
</a>
37
b.xml
<a>
<b>
<c d="2"/>
<c d="2"/>
</b>
<b>
<c d="2"/>
</b>
</a>
80
c.xml
-:3.19: Attribute d redefined
<c d="1" d="2"/>
^
2
xml shell xpath xmlstarlet
I want to insert additional elements into some variants of XML input. This script tries to demonstrate the input, and my attempt to insert code into the existing XML. As it can be seen, modifying a.xml
gives the expected output. But in b.xml
and c.xml
, the result is bogus. In b.xml
, the existing <c/>
is changed and another block of <b/>
is created. In c.xml
, the result is that d=""
is assigned twice.
There should be only a single <a><b>
, with several <c>
.
Any idea how to achieve that?
#!/bin/bash
# insert <a><b><c d="2"/>
set -e
td=`mktemp --directory --tmpdir=/dev/shm XXX`
trap "rm -rf '$td'" EXIT
xmlstarlet --version
pushd "$td"
cat > a.xml <<_EOX_
<a>
</a>
_EOX_
cat > b.xml <<_EOX_
<a>
<b>
<c/>
</b>
</a>
_EOX_
cat > c.xml <<_EOX_
<a>
<b>
<c d="1"/>
</b>
</a>
_EOX_
for i in *.xml
do
echo "$i"
cat "$i" |
xmlstarlet ed -O
-s 'a' -t elem -n b
-s 'a/b' -t elem -n c
-i 'a/b/c' -t attr -n d -v '2' |
xmlstarlet fo -o || echo "$?"
done
This produces the following output:
1.6.1
compiled against libxml2 2.9.7, linked with 20907
compiled against libxslt 1.1.32, linked with 10132
/dev/shm/tpX ~/work
a.xml
<a>
<b>
<c d="2"/>
</b>
</a>
37
b.xml
<a>
<b>
<c d="2"/>
<c d="2"/>
</b>
<b>
<c d="2"/>
</b>
</a>
80
c.xml
-:3.19: Attribute d redefined
<c d="1" d="2"/>
^
2
xml shell xpath xmlstarlet
xml shell xpath xmlstarlet
edited Nov 14 '18 at 16:50
kvantour
9,44431731
9,44431731
asked Nov 14 '18 at 15:38
olholh
333
333
could you please inform us how the output should look like for the 3 cases?
– kvantour
Nov 16 '18 at 9:37
@kvantourThere should be only a single <a><b>, with several <c>.
– olh
Nov 16 '18 at 13:50
updated the answer
– kvantour
Nov 16 '18 at 13:58
add a comment |
could you please inform us how the output should look like for the 3 cases?
– kvantour
Nov 16 '18 at 9:37
@kvantourThere should be only a single <a><b>, with several <c>.
– olh
Nov 16 '18 at 13:50
updated the answer
– kvantour
Nov 16 '18 at 13:58
could you please inform us how the output should look like for the 3 cases?
– kvantour
Nov 16 '18 at 9:37
could you please inform us how the output should look like for the 3 cases?
– kvantour
Nov 16 '18 at 9:37
@kvantour
There should be only a single <a><b>, with several <c>.
– olh
Nov 16 '18 at 13:50
@kvantour
There should be only a single <a><b>, with several <c>.
– olh
Nov 16 '18 at 13:50
updated the answer
– kvantour
Nov 16 '18 at 13:58
updated the answer
– kvantour
Nov 16 '18 at 13:58
add a comment |
1 Answer
1
active
oldest
votes
The problem is that you need to tell which node you want to change.
Add child
b
only ifb
does not exist ina
:-s "/a[not(b)]" -t elem -n "b"
Add a new child
c
ina/b
:-s "/a/b[not(c)]" -t elem -n "c"
Add attribute
d
to the newly added nodec
which is in the last place:-s "/a/b/c[last()]" -t attr -n "d" -v "2"
So the complete commands reads now:
xmlstarlet ed -O
-s "/a[not(b)]" -t elem -n "b"
-s "/a/b" -t elem -n "c"
-s "/a/b/c[last()]" -t attr -n "d" -v "2"
Here we made use of the not()
function to indicate that we just want to select the nodes that do not contain that particular item.
Useful links:
- A very handy XPath tutorial
- XPath 1.0 specification
Thanks. But the last one gives just a single <c/> inside <b/>. I was expecting two of them,
– olh
Nov 14 '18 at 21:45
@olh I thought this was your question. All XML inputs should give the same result.
– kvantour
Nov 14 '18 at 22:36
Thanks so much.
– olh
Nov 16 '18 at 20:03
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53303796%2fhow-to-insert-elements-into-xml-with-xmlstarlet%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The problem is that you need to tell which node you want to change.
Add child
b
only ifb
does not exist ina
:-s "/a[not(b)]" -t elem -n "b"
Add a new child
c
ina/b
:-s "/a/b[not(c)]" -t elem -n "c"
Add attribute
d
to the newly added nodec
which is in the last place:-s "/a/b/c[last()]" -t attr -n "d" -v "2"
So the complete commands reads now:
xmlstarlet ed -O
-s "/a[not(b)]" -t elem -n "b"
-s "/a/b" -t elem -n "c"
-s "/a/b/c[last()]" -t attr -n "d" -v "2"
Here we made use of the not()
function to indicate that we just want to select the nodes that do not contain that particular item.
Useful links:
- A very handy XPath tutorial
- XPath 1.0 specification
Thanks. But the last one gives just a single <c/> inside <b/>. I was expecting two of them,
– olh
Nov 14 '18 at 21:45
@olh I thought this was your question. All XML inputs should give the same result.
– kvantour
Nov 14 '18 at 22:36
Thanks so much.
– olh
Nov 16 '18 at 20:03
add a comment |
The problem is that you need to tell which node you want to change.
Add child
b
only ifb
does not exist ina
:-s "/a[not(b)]" -t elem -n "b"
Add a new child
c
ina/b
:-s "/a/b[not(c)]" -t elem -n "c"
Add attribute
d
to the newly added nodec
which is in the last place:-s "/a/b/c[last()]" -t attr -n "d" -v "2"
So the complete commands reads now:
xmlstarlet ed -O
-s "/a[not(b)]" -t elem -n "b"
-s "/a/b" -t elem -n "c"
-s "/a/b/c[last()]" -t attr -n "d" -v "2"
Here we made use of the not()
function to indicate that we just want to select the nodes that do not contain that particular item.
Useful links:
- A very handy XPath tutorial
- XPath 1.0 specification
Thanks. But the last one gives just a single <c/> inside <b/>. I was expecting two of them,
– olh
Nov 14 '18 at 21:45
@olh I thought this was your question. All XML inputs should give the same result.
– kvantour
Nov 14 '18 at 22:36
Thanks so much.
– olh
Nov 16 '18 at 20:03
add a comment |
The problem is that you need to tell which node you want to change.
Add child
b
only ifb
does not exist ina
:-s "/a[not(b)]" -t elem -n "b"
Add a new child
c
ina/b
:-s "/a/b[not(c)]" -t elem -n "c"
Add attribute
d
to the newly added nodec
which is in the last place:-s "/a/b/c[last()]" -t attr -n "d" -v "2"
So the complete commands reads now:
xmlstarlet ed -O
-s "/a[not(b)]" -t elem -n "b"
-s "/a/b" -t elem -n "c"
-s "/a/b/c[last()]" -t attr -n "d" -v "2"
Here we made use of the not()
function to indicate that we just want to select the nodes that do not contain that particular item.
Useful links:
- A very handy XPath tutorial
- XPath 1.0 specification
The problem is that you need to tell which node you want to change.
Add child
b
only ifb
does not exist ina
:-s "/a[not(b)]" -t elem -n "b"
Add a new child
c
ina/b
:-s "/a/b[not(c)]" -t elem -n "c"
Add attribute
d
to the newly added nodec
which is in the last place:-s "/a/b/c[last()]" -t attr -n "d" -v "2"
So the complete commands reads now:
xmlstarlet ed -O
-s "/a[not(b)]" -t elem -n "b"
-s "/a/b" -t elem -n "c"
-s "/a/b/c[last()]" -t attr -n "d" -v "2"
Here we made use of the not()
function to indicate that we just want to select the nodes that do not contain that particular item.
Useful links:
- A very handy XPath tutorial
- XPath 1.0 specification
edited Nov 16 '18 at 13:58
answered Nov 14 '18 at 16:28
kvantourkvantour
9,44431731
9,44431731
Thanks. But the last one gives just a single <c/> inside <b/>. I was expecting two of them,
– olh
Nov 14 '18 at 21:45
@olh I thought this was your question. All XML inputs should give the same result.
– kvantour
Nov 14 '18 at 22:36
Thanks so much.
– olh
Nov 16 '18 at 20:03
add a comment |
Thanks. But the last one gives just a single <c/> inside <b/>. I was expecting two of them,
– olh
Nov 14 '18 at 21:45
@olh I thought this was your question. All XML inputs should give the same result.
– kvantour
Nov 14 '18 at 22:36
Thanks so much.
– olh
Nov 16 '18 at 20:03
Thanks. But the last one gives just a single <c/> inside <b/>. I was expecting two of them,
– olh
Nov 14 '18 at 21:45
Thanks. But the last one gives just a single <c/> inside <b/>. I was expecting two of them,
– olh
Nov 14 '18 at 21:45
@olh I thought this was your question. All XML inputs should give the same result.
– kvantour
Nov 14 '18 at 22:36
@olh I thought this was your question. All XML inputs should give the same result.
– kvantour
Nov 14 '18 at 22:36
Thanks so much.
– olh
Nov 16 '18 at 20:03
Thanks so much.
– olh
Nov 16 '18 at 20:03
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53303796%2fhow-to-insert-elements-into-xml-with-xmlstarlet%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
could you please inform us how the output should look like for the 3 cases?
– kvantour
Nov 16 '18 at 9:37
@kvantour
There should be only a single <a><b>, with several <c>.
– olh
Nov 16 '18 at 13:50
updated the answer
– kvantour
Nov 16 '18 at 13:58