XSLT Mapping – Know-how
Before writing this blog, I thought of coming up with a story line then dropped that plan, because most of the time we google for specific solutions. So, here I’m sharing some of my experiences dealing with different XSLT scenarios.
How to create XSLT Mapping using Altova MapForce:
- Get the source and target XML files
- In Altova MapForce create a new Mapping: File => New => Mapping
- Insert Source & Target XML: Insert => XML Schema/File
- This will prompt you to locate corresponding XSD file and if you don’t have that, it’ll create that for you
- Now map the nodes from source to target structure
- Now go to: Output => XSLT 1.0/2.0, you’ll get the corresponding XSLT mapping:
- Check the “Output” tab for sample output:
How to add CDATA tags into output XML using XSLT mapping:
CDATA sections are used to escape blocks of text containing characters which would normally be interpreted as markup. CDATA sections start with “<![CDATA[” and end with the “]]>”.
· With Altova MapForce
- Right click on the field where you want to add the CDATA and choose “Write Content as CDATA Section”
- This with add a “c” on the field denoting CDATA.
3. Corresponding XSLT will look like:
- And output will be as following:
· Without Altova MapForce
We need to know how to achieve CDATA mapping without the help of AMF because in some cases AMF doesn’t support CDATA mapping.
For example, CDATA is not support in any node other than a leaf one.
In this case add the following piece of code where you want to add the CDATA.
The Output will add CDATA before the <gridfield> tag.
How to add extra tags which are not there in the Input but required in the Output:
You need to simply mention the extra tag in the XSLT mapping.
In our previous example, we didn’t have the tag <gridfield> in the incoming XML but we needed that in the output XML, hence just added that corresponding opening & closing tags in the right places.
How to add additional identifiers in the Output XML tag:
Tags with special characters are not supported in the SAP PI Message Mapping. In those cases, we can use XSLT mapping to add additional identifies to an existing XML tag.
Let’s consider previous XML structures for example. In that we didn’t have any prefix in any of the output XML tags.
Now, if the requirement is to add “urn:” in every <name> and <value> tags in the output XML then you need to declare that in the beginning.
Input XML –
Corresponding XSLT Mapping:
If you don’t declare the prefix namespace in the beginning, then you’ll receive following error:
Hope this be useful for you. Happy learning and sharing 🙂