Mapper, create xml with both attribute and node value, or multiple attributes

I’m using a mapper to create values for an XML Formatter snap, but I can’t figure out how to handle when the XML requires both an attribute and a node value, or multiple attributes, like:

<address_type desc=“Work”>work</address_type>

How can I get the format I need?

Hi @wpenfold,

Perhaps you can find an answer here:

BR,
Dimitri

1 Like

Does Snaplogic use StAXON conventions? I still can’t figure out how to use the Mapper to include both an attribute and value on a single XML node

I think what you’re looking for is something like this in your Mapper:

image

Defining the root [’$’] attribute is the element value… using the at-sign (@) as the field prefix tells the XML Formatter that it is an attribute of the element.

See the XML Formatter documentation for more details:
https://docs-snaplogic.atlassian.net/wiki/spaces/SD/pages/1439279/XML+Formatter

I’ve put together a sample pipeline. In it I use a Mapper Snap to create the needed input for the XML Formatter Snap.

XML_with_Mapper_2020_06_29.slp (6.8 KB)

Hope it helps to clarify, or muddify :slightly_smiling_face: the matter further.

BR,
Dimitri

2 Likes

Thanks! Using the $ gave me the node value I needed.

Another question… If an email exists for the person, I need to create an xml node like this:

<email_address>XXXX@xxx.edu</email_address>
<email_types>
<email_type>work</email_type>
</email_types>
< / email >

But if we don’t have an email address, it needs to be just:

< / email >

Is there a way to do this in the mapper? I can handle one condition or the other, but not both. Do I have to use a router and 2 mappers?.. (I can’t get the end tags to show in this editor unless I add spaces)

Hi,

Assuming that in the input document of the Mapper (where you want to produce the JSON Object to be formatted to valid XML) you have this kind of structure:
{
“email”: {
“type”: “work”,
“address”: “one@two.com
}
}
then, if you put this into the Expression in the Mapper Snap:

($.hasPath("email") && $email != null) ? {}.extend( {email_address:$email.address, email_types:{email_type:$email.type } } ) : null

and assign it to the $email Target Path, that should produce the intended results.

Here’s a pipeline where the elaborated above is implemented:
XML_with_Mapper_2_2020_07_02.slp (6.5 KB)

BR,
Dimitri

1 Like