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

Subject: Re: [xsl] How to declaratively describe a mapping that involves breaking a string apart and reassembling the parts with an additional symbol?
From: "Dimitre Novatchev dnovatchev@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Wed, 13 Mar 2024 21:09:17 -0000
Hi Roger,

>         <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'"/>

Just have this XPath expression:

let $m := map{'E' : 'East', 'W': 'West'},
 return
    $m(substring($magVar,1,1))


>       <xsl:value-of select="
>             concat(substring($magVar,2,3),'.',substring($magVar,5,1))"/>

And for the number:

   format-number(substring($magVar, 2), '999.9')

In case this code doesn't strike you as "declarative enough", then place
the above expressions in suitably-named functions:

   my:MagDirection($input as xs:string) as xs:string

and

   my:MagValue($input as xs:string) as XS:string

Or even have these combined within a single function:

my:MagData($input as xs:string)  as map(xs:string, xs:string)

and use:

   my:MagData($magVar)?direction
and
   my:MagData($magVar)?value


Thanks,
Dimitre

On Tue, Mar 12, 2024 at 6:20b/AM Roger L Costello costello@xxxxxxxxx <
xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:

> 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),'.',substring($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