-2

I want to copy from an xml file, two element nodes to another node (parent) in an existing xml file using xmlstarlet.

input example:

<?xml version="1.0" encoding="utf-8"?>
<article>
   <publication>
    <title locale="en_US">Ludo-Narrative Dissonance Revisited<title>
    <abstract locale="en_US"> which we see</abstract>     <licenseUrl>https://creativecommons.org/licenses/by/4.0</licenseUrl>
    <copyrightHolder>Pawe;zyk, Bo Kampmann Walther</copyrightHolder>
    <copyrightYear>2023</copyrightYear>
    <keywords locale="en_US">
      <keyword>Ludo-narrative dissonance</keyword>
      <keyword>ludology</keyword>
    </keywords>
    <authors xmlns:xsi="http://XMLSchema-instance">
      <author include_in_browse="true">
        <givenname locale="en_US">Pawe</givenname>
        <familyname locale="en_US">Grabarczyk</familyname>
        <affiliation locale="en_US">IT University</affiliation>
      </author>
      <author include_in_browse="true">
        <givenname locale="en_US">Bo</givenname>
        <familyname locale="en_US">Kampmann Walther</familyname>
        <affiliation locale="en_US">University</affiliation>
       </author>
      </authors>
  </publication>
</article>

I would like to copy the elements, and as child elements to an existing second xml file where <from_old_xml> serves as a parent. will only contain and

output.xml will look like this:

<?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>
    <journal-meta>
      <journal-id/>
      <journal-title-group/>
      <issn/>
      <journal-title-group/>
      <publisher>
        <publisher-name/>
      </publisher>
    </journal-meta>
    <article-meta>
      <article-id pub-id-type="doi">10.7557/23.6506</article-id>
      <title-group>
        <article-title>Game of Twisted Shouting</article-title>
        <subtitle>Ludo-Narrative Dissonance Revisited</subtitle>
      </title-group>
      <contrib-group/>
    </article-meta>
    <from_old_xml> 
      <keywords>
        <keyword>Ludo-narrative dissonance</keyword>
        <keyword>ludology</keyword>
        <keyword>narrativity</keyword>
        <keyword>gameplay</keyword>
        <keyword>games and violence</keyword>
      </keywords>
      <authors>
        <author>
          <givenname>Bo</givenname>
          <familyname>Kampmann Walther</familyname>
        </author>
        <author>
          <givenname>Bo</givenname>
          <familyname>Kampmann Walther</familyname>
        </author>
      </authors>
    <from_old_xml> 
  </front>
</article>

xmlstarlet ed -L \
     -s "//article/front" -t test -n "from_old_xml" -v "$(xmlstarlet select --omit-decl -t --copy-of '//article/publication/keywords' input.xml)" output.xml

It does not work and I need help.

Thanks in advance

  • Ofuuzo

1 Answer 1

0

Here we go ^^

$ xmlstarlet edit -r '/dataTemplate' -v 'sec' xml1
<?xml version="1.0" encoding="windows-1252"?>
<sec>
  <test>John</test>
  <test>Peter</test>
  <test>Paul</test>
</sec>

So

 $ xmlstarlet edit -r '/dataTemplate' -v 'sec' xml1 | tee xml2
Sign up to request clarification or add additional context in comments.

1 Comment

So give a better sample. My answer fulfill your requirements

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.