0

I have to create this node following this model https://gitlab.com/crossref/schema/-/blob/master/examples/journal_article_4.4.2.xml:

<jats:abstract>The abstract
</jats:abstract>

I already set the namespaces:

MY_NAMESPACES={'xsi': 'http://www.w3.org/2001/XMLSchema-instance', None: 'http://www.crossref.org/schema/4.4.2', 'jats': 'http://www.ncbi.nlm.nih.gov/JATS1', 'fr': 'http://www.crossref.org/fundref.xsd'}
attr_xsi = etree.QName("http://www.w3.org/2001/XMLSchema-instance", "schemaLocation")
root = etree.Element('doi_batch', {attr_xsi: "http://www.crossref.org/schema/4.4.2 https://www.crossref.org/schemas/crossref4.4.2.xsd"}, nsmap=MY_NAMESPACES, version="4.4.2")

But I don't know how to call the jats namespace from that. Following this example Create XML files which have tags with prefix (python and lxml) I created this:

jats = "http://jats.nlm.nih.gov" #https://jats.nlm.nih.gov/archiving/tag-library/1.3d1/chapter/app-notes-namespace.html
jats_namespace = {"jats": jats}
abstract = etree.SubElement(article, "{" + jats + "}" + "abstract").text = "the abstract"

But what I get is

<ns0:abstract xmlns:ns0="http://jats.nlm.nih.gov">the abstract</ns0:abstract>

I know this is wrong, but how can I call the jats namespace set before?

Lara M.
  • 855
  • 2
  • 10
  • 23
  • 1
    If you want "jats" instead of "ns0", you need to use `register_namespace`: https://stackoverflow.com/a/58678592/407651 – mzjn Jun 09 '22 at 10:18
  • Thank you very much, that worked! `jats_namespace = "http://www.ncbi.nlm.nih.gov/JATS1" etree.register_namespace("jats", jats_namespace) abstract = etree.SubElement(article, etree.QName(jats_namespace, "abstract")).text = "the abstract"` – Lara M. Jun 09 '22 at 12:30
  • 1
    Does this answer your question? [Extracting Child XML using ElementTree ignoring Namespace](https://stackoverflow.com/questions/49257216/extracting-child-xml-using-elementtree-ignoring-namespace) – mzjn Jun 09 '22 at 12:55

0 Answers0