[xsl] How to declaratively describe a mapping that involves breaking a string apart and reassembling the parts with an additional symbol?

Subject: [xsl] How to declaratively describe a mapping that involves breaking a string apart and reassembling the parts with an additional symbol?
From: "Roger L Costello costello@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 12 Mar 2024 13:20:45 -0000
Hi Folks,

I am mapping an old XML format to a new XML format.

To carry out the mapping, I want to write as little code as possible; instead,
I want to declaratively describe the mapping in an XML document and then have
a tiny piece of generic code which, with little or no knowledge of the old and
new formats, carries out the mappings described in the XML document.

I am having a hard time with some descriptions. Here's an illustration:

I want to map this old XML:

<Magnetic_Variation>W0150</Magnetic_Variation>

to this new XML:

<magneticVariation>
    <magneticVariationEWT>West</magneticVariationEWT>
    <magneticVariationValue>015.0</magneticVariationValue>
</magneticVariation>

Here is one way to perform the mapping:

<magneticVariation>
    <magneticVariationEWT>
        <xsl:value-of select="
            if (substring($magVar,1,1) eq 'E') then 'East'
            else if (substring($magVar,1,1) eq 'W') then 'West'
            else 'True'"/>
    </magneticVariationEWT>
    <magneticVariationValue>
        <xsl:value-of select="
            concat(substring($magVar,2,3),'.',substring($magVar,5,1))"/>
    </magneticVariationValue>
</magneticVariation>

That is a fine way to perform the mapping. However, it is not the way that I
want to do it because the mapping is expressed procedurally, not
declaratively. I want the mapping expressed declaratively.

I can declaratively express part of the mapping -- map the first character E
to East, W to West, T to True -- using this description:

<Magnetic_Variation>
    <mapping>
        <old>
            <Magnetic_Variation col="1" length="1">E</Magnetic_Variation>
        </old>
        <new>
            <magneticVariationEWT>East</magneticVariationEWT>
        </new>
    </mapping>
    <mapping>
        <old>
            <Magnetic_Variation col="1" length="1">W</Magnetic_Variation>
        </old>
        <new>
            <magneticVariationEWT>West</magneticVariationEWT>
        </new>
    </mapping>
    <mapping>
        <old>
            <Magnetic_Variation col="1" length="1">T</Magnetic_Variation>
        </old>
        <new>
            <magneticVariationEWT>True</magneticVariationEWT>
        </new>
    </mapping>
</Magnetic_Variation>

I do not know how to declaratively describe the other part of the mapping --
map dddd to ddd.d (where d = digit). I could do this:

    <mapping>
        <old>
            <Magnetic_Variation col="2" length="4"></Magnetic_Variation>
        </old>
        <new>
            <magneticVariationValue>concat(substring($magVar,2,3),'.',substri
ng($magVar,5,1))</magneticVariationValue>
        </new>
    </mapping>

But that is unacceptable (to me) because the description contains code.

How do I declaratively describe this mapping?

Bonus points if you can also answer this question:

Computer Science Theory Question: If it is impossible to declaratively express
the above mapping, does that mean there is a limit to declarative
descriptions? Is the set of declarative descriptions smaller than the set of
procedural descriptions?

/Roger

Current Thread