Skip to content

Instantly share code, notes, and snippets.

@hoehrmann
Created April 21, 2024 23:14
Show Gist options
  • Save hoehrmann/11001e4606d473f5cef169db4799a4a3 to your computer and use it in GitHub Desktop.
Save hoehrmann/11001e4606d473f5cef169db4799a4a3 to your computer and use it in GitHub Desktop.
XML to JSON mapping rule definitions
#
# In a JSON document, there is a unique normalized JSON Path for everything,
# so you can write out a JSON document as a combination of paths and values:
#
# $.meta.id = "ID123"
#
# Given some JSON schema document, you can write out all possible paths. To
# handle infinite nesting, part of the path could be described using a regular
# expression.
#
# Ignoring mixed content, you can do the same for some XML schema. Combined,
# if there is a reasonably obvious mapping between the XML representation and
# the JSON representation of some format, that leads to something like this:
#
/m:Structures/s:DataStructure[n]/@agencyID:
$.data.dataStructures[n].agencyID
/m:Structures/s:DataStructure[n]/@serviceURL:
$.data.dataStructures[n].serviceURL
/m:Structures/s:DataStructure[n]/c:Annotations/c:Annotation[o]/c:AnnotationTitle:
$.data.dataStructures[n].annotations[o].AnnotationTitle
/m:Structures/s:MetadataStructure[n]/s:MetadataAttributeList(/s:MetadataAttribute[o]){p}/s:ConceptIdentity:
$.data.metadataStructures[n].metadataAttributeList(.metadataAttributes[o]){p}.ConceptIdentity
# This is XPath and JSON Path with the addition of of the `(...){n}` construct,
# which refers to the literal path enclosed in parens repeated n times, and
# using placeholders for node set / array indices.
#
# The logic would be, in an input XML document, if some node matches one
# of the rules for some combination of integer values for the placeholders,
# then the value of the node selected by the JSON path template, after
# expanding repretitions and filling in array indices with the corresponding
# integer values, in the output JSON document must match.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment