Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Simple Transformation: Problems with encoding conversion (angle brackets)

patricksteffens
Participant
0 Kudos
1,802

I use ST to create an XML file from ABAP. The transformation works finde except for one tiny, yet crucial part.

Here's what the XML should look like in the end:

<?xml version="1.0" encoding="utf-8"?>
<Paket>
    <Metadaten>
        <DOKNR>N1-QCPRT-000231</DOKNR><DOKAR>DOC</DOKAR><DOKVR>-</DOKVR><DOKTL>000</DOKTL><WFRelevant>J</WFRelevant>
    </Metadaten>
</Paket>

The tags in line 4 have dynamic names, depending on the metadata parameters read by the program. Since ST is not built for handling such dynamic stuff, I create this line manually in ABAP by writing it into a string variable (PAKET.METADATEN) and use it in my transformation:

<?sap.transform simple?>
<tt:transform xmlns:tt="http://www.sap.com/transformation-templates" xmlns:ddic="http://www.sap.com/abapxml/types/dictionary" xmlns:def="http://www.sap.com/abapxml/types/defined">
  <tt:root name="ROOT" type="?"/>
  <tt:root name="PAKET" type="ddic:ZPD_S_XML_PAKET_ROOT"/>
  <tt:template>
    <Paket>
      <tt:serialize>
        <Metadaten><tt:value ref=".PAKET.METADATEN"/></Metadaten>
      </tt:serialize>
    </Paket>
  </tt:template>
</tt:transform>

I transform it writing the output into an xstring variable (to get an UTF-8 XML). The transformation now interprets the angle brackets in the string as '& lt;' for '<' and '& gt;' for '>' (ignore the whitespace ;-)) which leads to the following output XML (in bytes, since this editor here is able to interpret it correctly:

3c3f786d6c2076657273696f6e3d22312e302220656e636f64696e673d227574662d38223f3e0a3c50616b65743e0a093c4d657461646174656e3e266c743b444f4b4e522667743b4e312d51435052542d303030323331266c743b2f444f4b4e522667743b266c743b444f4b41522667743b444f43266c743b2f444f4b41522667743b266c743b444f4b56522667743b2d266c743b2f444f4b56522667743b266c743b444f4b544c2667743b303030266c743b2f444f4b544c2667743b3c2f4d657461646174656e3e0a3c2f50616b65743e<br>

I'm working on a unicode system and thus the string value is represented in unicode. The output must be a UTF-8 xml file.

So the basic question is: How can I either realize such dynamic tag names without filling a string variable or have ST interpret the string properly, so that the angle brackets are shown in the output?

1 ACCEPTED SOLUTION

Tomas_Buryanek
Active Contributor
965

Check out XML Fragments.

Demo program for ST = DEMO_ST_XSDANY - method prepare_fragment is creating XML string which is later inserted in transformation to result XML.

By the way XML Fragments works in XSL Transformation too (not only in Simple Transformations).

-- Tomas --
7 REPLIES 7

Tomas_Buryanek
Active Contributor
966

Check out XML Fragments.

Demo program for ST = DEMO_ST_XSDANY - method prepare_fragment is creating XML string which is later inserted in transformation to result XML.

By the way XML Fragments works in XSL Transformation too (not only in Simple Transformations).

-- Tomas --

0 Kudos
965

Hi Tomas,

thanks a lot for this hint. This solved the initial problem of broken tags 🙂 However, < and > within such nodes (e.g. arrow symbols in description texts) are still translated into & lt ; and & gt ; (without whitespace) . Do you know why this happens?

Best
Patrick

965

patricksteffens It looks like you need to escape the values in your XML string (fragment) before transformation.

For example you have a value "two > one" in tag <value>. After escaping the complete string would look like - EDIT: without spaces after ampresand, sap.com forum is unescaping escaped value > 😞

<value>two & gt ; one</value>
-- Tomas --

0 Kudos
965

I replaced all < by & lt ; and all > by & gt ; (without whitespace) before the id transformation.

In the output XML, they are not translated back into < and >.Even worse: The & is interpreted as & amp ; which makes somewhat sense to me.

Or did I get you wrong?

0 Kudos
965

Patrick Steffens Why is it a problem that a text with character > is seritalized into & gt; ? (by the way it's not related to the question which was about including an XML fragment in an XML element) That's how XML works: the deserialization will interpret & gt; as the character >. Same thing for & which is serialized into & amp;. Do you have an example to describe your problem? Or is it just because you don't "like" it?

0 Kudos
965

sandra.rossi : We encountered problems where description texts are not translated consistently. E.g. the text "ä<" would be translated into "ä& lt ;" (without whitespace) which is a problem for our software parsing the XML (it fails reading the 'ä' symbol). Thus I would like to have either all or no symbol transformed. So either I would expect all symbols to be translated or none.

Is there an explanation for this observation?

0 Kudos
965

By the way, I tried to escape the tag containing these symbols via CDATA but this did not have an effect.