The qml
file style file you get when exporting layer styles is different from the one you get in the "Style manager" when you export. The second format is the accepted one in "Create categorized renderer from style"
What is missing is a script to get "Style manager" compatible file from an existing layer style. I've made a script to make a "bridge" between both (limited as I've tested it only using Style Manager Fill symbols)
Script to bridge QML style and Style Manager XML
import xml.etree.ElementTree as ET
import copy
layer =iface.activeLayer()
outpath_layer_style = '/tmp/qml_style.qml'
out_style_manager = '/tmp/out_style_manager.xml'
# Export layer style to QML
layer.saveNamedStyle(outpath_layer_style)
# Read layer style QML
tree_qml = ET.parse(outpath_layer_style)
root_qml = tree_qml.getroot()
# Style Manager output style template
tree_template = ET.fromstring('''
<!DOCTYPE qgis_style>
<qgis_style version="2">
<symbols>
</symbols>
<colorramps/>
<textformats/>
<labelsettings/>
<legendpatchshapes/>
<symbols3d/>
</qgis_style>''')
# Need to fill name in each symbol with it categories name
symbols_root = tree_template.find('symbols')
symbols = root_qml.findall('.//symbols/symbol')
categories = { category.attrib['symbol']: category.attrib['value'] for category in root_qml.findall('.//categories/category')}
# Make update to symbol name and add symbol modified to template
for symbol in symbols:
s = copy.deepcopy(symbol)
s.attrib['name'] = categories[symbol.attrib['name']]
symbols_root.append(s)
with open(out_style_manager, 'w') as outfile:
outfile.write(ET.tostring(tree_template, encoding='unicode'))
How to export from Style Manager
To get the XML style manager file you need to provide to the "Create categorized renderer from style" you can use the following recipe.
You need to go to the menu "Settings" > "Style Manager"
At the bottom of the window, you can export your selected styles to the XML (see below screenshot)
Your selected styles names should match the categories in the layer (I used "List unique values" before in the Processing Toolbox to name them)
Then, you can apply the style
Standalone use case to completely try "Create categorized renderer from style"
To test, you can set a vector layer with source /vsicurl/https://d2ad6b4ur7yvpq.cloudfront.net/naturalearth-3.3.0/ne_110m_admin_0_countries.geojson
Then, you can use the Processing Toolbox "Create categorized renderer from style" using column region_un
and use the following style "un_region.xml" (download link, generated from the Style manager XML export)
See the following configuration
You will get the following output