From d874f8ca57b7a1da69896188a74d3128ac6901fd Mon Sep 17 00:00:00 2001 From: Adrian Cochrane Date: Wed, 20 Nov 2024 11:04:49 +1300 Subject: [PATCH 1/2] Add an XSLT transformer. --- ogc/bblocks/transformers/xslt.py | 26 ++++++++++++++++++++++++++ requirements.txt | 3 ++- 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 ogc/bblocks/transformers/xslt.py diff --git a/ogc/bblocks/transformers/xslt.py b/ogc/bblocks/transformers/xslt.py new file mode 100644 index 0000000..f33fa80 --- /dev/null +++ b/ogc/bblocks/transformers/xslt.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 +import json +from typing import AnyStr + +from ogc.bblocks.models import TransformMetadata, Transformer +from lxml import etree + +transform_type = 'xslt' + +default_inputs = [ + 'text/xml', +] + +default_outputs = [ + 'text/xml', +] + +class XmlTransformer(Transformer): + + def __init__(self): + super().__init__(['xslt'], default_inputs, default_outputs) + + def do_transform(self, metadata: TransformMetadata) -> AnyStr | None: + transform = etree.XSLT(etree.XML(metadata.transform_content)) + result = transform(etree.XML(metadata.input_data)) + return etree.to_string(result, pretty=True) diff --git a/requirements.txt b/requirements.txt index 0c92a2a..1850656 100644 --- a/requirements.txt +++ b/requirements.txt @@ -13,6 +13,7 @@ networkx~=3.1 jq~=1.6 pathvalidate==3.2.0 jsonpointer~=2.4 +lxml~=5.0.0 # mkdocs mkdocs>=1.4.2 @@ -25,4 +26,4 @@ mkdocs-pymdownx-material-extras mkdocs-section-index mkdocstrings mkdocstrings-python -mkdocs-markdownextradata-plugin \ No newline at end of file +mkdocs-markdownextradata-plugin From 76d2fcc5c8c3d520c6286098e5a1cdc9083e4302 Mon Sep 17 00:00:00 2001 From: Alejandro Villar Date: Thu, 28 Nov 2024 20:50:28 +0100 Subject: [PATCH 2/2] Fix XSLT transforms, and add type hint to schema --- ogc/bblocks/known-mimetypes.yaml | 1 + ogc/bblocks/schemas/transforms.schema.yaml | 1 + ogc/bblocks/transformers/xslt.py | 10 +++++----- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/ogc/bblocks/known-mimetypes.yaml b/ogc/bblocks/known-mimetypes.yaml index 4a5ad15..b956a46 100644 --- a/ogc/bblocks/known-mimetypes.yaml +++ b/ogc/bblocks/known-mimetypes.yaml @@ -34,6 +34,7 @@ xml: mimeType: application/xml aliases: - application/xml + - text/xml extensions: - xml diff --git a/ogc/bblocks/schemas/transforms.schema.yaml b/ogc/bblocks/schemas/transforms.schema.yaml index 3929005..47354f4 100644 --- a/ogc/bblocks/schemas/transforms.schema.yaml +++ b/ogc/bblocks/schemas/transforms.schema.yaml @@ -69,6 +69,7 @@ $defs: - json-ld-frame - jq - semantic-uplift + - xslt - description: Other identifiers are also accepted $ref: '#/$defs/nonEmptyString' diff --git a/ogc/bblocks/transformers/xslt.py b/ogc/bblocks/transformers/xslt.py index f33fa80..da93bf6 100644 --- a/ogc/bblocks/transformers/xslt.py +++ b/ogc/bblocks/transformers/xslt.py @@ -8,11 +8,11 @@ transform_type = 'xslt' default_inputs = [ - 'text/xml', + 'application/xml', ] default_outputs = [ - 'text/xml', + 'application/xml', ] class XmlTransformer(Transformer): @@ -21,6 +21,6 @@ def __init__(self): super().__init__(['xslt'], default_inputs, default_outputs) def do_transform(self, metadata: TransformMetadata) -> AnyStr | None: - transform = etree.XSLT(etree.XML(metadata.transform_content)) - result = transform(etree.XML(metadata.input_data)) - return etree.to_string(result, pretty=True) + transform = etree.XSLT(etree.XML(metadata.transform_content.encode('utf-8'))) + result = transform(etree.XML(metadata.input_data.encode('utf-8'))) + return etree.tostring(result, encoding='utf-8', pretty_print=True, xml_declaration=True).decode('utf-8')