Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unescape xml file using xmlstarlet

I have two xml files A and B. I want to insert certain element nodes in A to B. It went fine but the imported elements were not "unescape" in B:

XML file A:

<?xml version="1.0"?>
<article>
  <publication>
     <authors>
      <author>
        <givenname locale="en_US">Admin</givenname>
        <givenname locale="nb_NO">Admin</givenname>
        <familyname locale="en_US">Septentrio</familyname>
        <email>[email protected]</email>
      </author>
      <author include_in_browse="true" user_group_ref="Forfatter" seq="0" id="13075">
        <givenname locale="nb_NO">Peter</givenname>
        <familyname locale="nb_NO">Nilsen</familyname>
        <affiliation locale="nb_NO">NTL University</affiliation>
        <country>NO</country>
        <email>[email protected]</email>
      </author>
    </authors>
  </publication>
</article>

XML file B:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE article PUBLIC "-//NLM//DTD JATS (Z39.96) Journal Archiving and Interchange DTD v1.2 20190208//EN" "JATS-archivearticle1.dtd">
<article xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:xlink="http://www.w3.org/1999/xlink" dtd-version="1.2" article-type="other">
  <front>
  </front>
</article>

When I run:

xmlstarlet ed -L -s "//article/front" -t elem -n "newchild"  -v "$(xmlstarlet  sel  --omit-decl  -t -c "/article/publication/authors/author" a.xml)" b.xml

it gives me the following in B file:

<!DOCTYPE article PUBLIC "-//NLM//DTD JATS (Z39.96) Journal Archiving and Interchange DTD v1.2 20190208//EN" "JATS-archivearticle1.dtd">
<article xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:xlink="http://www.w3.org/1999/xlink" dtd-version="1.2" article-type="other">
  <front>
  <newchild>&lt;author&gt;
        &lt;givenname locale="en_US"&gt;Admin&lt;/givenname&gt;
        &lt;givenname locale="nb_NO"&gt;Admin&lt;/givenname&gt;
        &lt;familyname locale="en_US"&gt;Septentrio&lt;/familyname&gt;
        &lt;email&gt;[email protected]&lt;/email&gt;
        &lt;/author&gt;&lt;author include_in_browse="true" user_group_ref="Forfatter" seq="0" id="13075"&gt;
        &lt;givenname locale="nb_NO"&gt;Peter&lt;/givenname&gt;
        &lt;familyname locale="nb_NO"&gt;Nilsen&lt;/familyname&gt;
        &lt;affiliation locale="nb_NO"&gt;NTL University&lt;/affiliation&gt;
        &lt;country&gt;NO&lt;/country&gt;
        &lt;email&gt;[email protected]&lt;/email&gt;
      &lt;/author&gt;</newchild></front>
</article>

How can I do unesc on the result.

Thanks.

Ofuuzo

like image 260
Ofuuzo Avatar asked Feb 01 '26 06:02

Ofuuzo


1 Answers

Like this:

$ xmlstarlet unesc < file.xml
<!DOCTYPE article PUBLIC "-//NLM//DTD JATS (Z39.96) Journal Archiving and Interchange DTD v1.2 20190208//EN" "JATS-archivearticle1.dtd">
<article xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:xlink="http://www.w3.org/1999/xlink" dtd-version="1.2" article-type="other">
  <front>
  <newchild><author>
        <givenname locale="en_US">Admin</givenname>
        <givenname locale="nb_NO">Admin</givenname>
        <familyname locale="en_US">Septentrio</familyname>
        <email>[email protected]</email>
        </author><author include_in_browse="true" user_group_ref="Forfatter" seq="0" id="13075">
        <givenname locale="nb_NO">Peter</givenname>
        <familyname locale="nb_NO">Nilsen</familyname>
        <affiliation locale="nb_NO">NTL University</affiliation>
        <country>NO</country>
        <email>[email protected]</email>
      </author></newchild></front>
</article>

to edit in place:

$ xmlstarlet unesc < file.xml | sponge file
like image 188
Gilles Quenot Avatar answered Feb 03 '26 23:02

Gilles Quenot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!