0

In the documentation I'm looking at for a file upload, it needs this specific tag at the beginning of the xml file:

<oclcPersonas xmlns="http://worldcat.org/xmlschemas/IDMPersonas-2.2" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://worldcat.org/xmlschemas/IDMPersonas-2.2 IDMPersonas-2.2.xsd">

I'm trying to use the lxml.etree library to figure this out.

Many of the examples I've seen have a initial level of namespace for attributes that would cover the xmlns:xsi portion using this:

namespace_map = {
        None: persona_namespace,
        'xsi': "http://www.w3.org/2001/XMLSchema-instance"}

but 2 questions arise from the second portion xsi:schemaLocation

1) How would I accomplish a secondary level of namespace using lxml?

2) How do I allow the namespaces to contain a space without receiving an error (http://worldcat.org/xmlschemas/IDMPersonas-2.2 IDMPersonas-2.2.xsd)

1 Answer 1

1

In your sample XML, you have :

To build this element with lxml, you need to use the fully-qualified name using curly braces (James Clark notation).

First define a dictionary which store your namespace mapping:

# your namespaces
p_url = "http://worldcat.org/xmlschemas/IDMPersonas-2.2"
xsi_url = "http://www.w3.org/2001/XMLSchema-instance"
NS = {None: p_url, 'xsi': xsi_url}

In order to build fully-qualified namespace, you can define prefixes:

# tag prefixes
P = '{' + p_url + '}'
XSI = '{' + xsi_url + '}'

Then you can define the tag names and attributes:

# tag names and attributes
root_tag = P + 'oclcPersonas'
attrs = {
    XSI + 'schemaLocation':
    "http://worldcat.org/xmlschemas/IDMPersonas-2.2 IDMPersonas-2.2.xsd"
}

The root element can be created like below:

# element
root = etree.Element(root_tag, attrib=attrs, nsmap=NS)

You should have your element:

print(etree.tounicode(root))

To answer your question:

2) How do I allow the namespaces to contain a space without receiving an error (http://worldcat.org/xmlschemas/IDMPersonas-2.2 IDMPersonas-2.2.xsd)

This is not a namespace, this is the value of the schemaLocation attribute. A simple string.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! Worked Perfectly

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.