#xml

2 notes

Originally posted: 2021-05-08

A simple example of how to declare a namespace in XQuery so you can quickly and easily run XPath and XQuery on namespace elements

Namespace prefix has not been declared error

Namespaces can be a big pain. If you try to run an XQuery on something like an XSL stylesheet (where all the XSL elements are prefixed with the xsl: prefix), you might run into an error like this:

Namespace prefix 'xsl' has not been declared

Solution

The solution is as simple as declaring the namespace. Here’s an example:

declare namespace xsl = "http://www.w3.org/1999/XSL/Transform";
count(//xsl:template)
Permalink →

Flatten XML via JSON

2023-06-24 · 2 min read

Originally posted: 2021-01-02

Flatten an XML tree by turning it into JSON first (Python)

Going Directly from XML to JSON Using xmltodict:

It’s pretty straightforward to turn an XML file into a Python dictionary (which is essentially the same as a JSON file’s content).

The code snippet below can be copied and pasted into your terminal, and it will prompt you to select a file.

If you run into ModuleNotFoundErrors, simply type the following into your Python terminal:

pip or pip3 import [NAME OF MISSING MODULE HERE]

import xmltodict, json
xml_filepath = input('Drag and drop an XML file here:').strip()
with open(xml_filepath) as xml_file_input:
    xml_data_stream = xml_file_input.read()
    data_dict = xmltodict.parse(xml_data_stream)

Flatten JSON recursively with Python

I came across a nice, succinct, and effective piece of code to accomplish what I needed. Thanks Amir Ziai!

import json
 
def flatten_json(y): # or use `pip install flatten_json`
    out = {}
 
    def flatten(x, name=''):
        if type(x) is dict:
            for a in x:
                flatten(x[a], name + a + '_')
        elif type(x) is list:
            i = 0
            for a in x:
                flatten(a, name + str(i) + '_')
                i += 1
        else:
            out[name[:-1]] = x
 
    flatten(y)
    return out
 
json_filepath = input('Drag and drop a JSON file here:').strip()
with open(json_filepath) as json_file_input:
    string_data_stream = json_file_input.read()
    json_data_stream = json.loads(string_data_stream)
    flat_json = flatten_json(json_data_stream)
 

In case you run into an error about an extra character being at the end of your file, you can just leave off the final one (or two, etc.) characters by adding an index to the end of your string_data_stream, for example:

json.loads(string_data_stream[:-1])

Don’t Forget to Save Your Flattened JSON Data

with open('flattened_xml_via_json.json', 'a') as save_file:
    save_file.write(json.dumps(json_data_stream, indent=4, sort_keys=True))
Permalink →