cancel
Showing results for 
Search instead for 
Did you mean: 

Escaping ' and " characters in XML message

Former Member
0 Kudos

Hello,

I have the following issue with XML character escaping. The following characters need to be escaped: < > & ' and ".

When I receive the message in the integration engine &lt and &gt are already escaped as expected. However ' and " are not.

I have implemented a Java Mapping to replace " and ' with it escape characters & quot; and & apos;

Problem however is that ALL " are escaped (including the XML prolog xml version = "1.0" )

The XML then results in a parsing error when executing a test for the mapping.

Do you have any idea how I can resolve this?`

Thank you very much!

Edited by: Florian Guppenberger on Jan 20, 2010 10:49 PM

Edited by: Florian Guppenberger on Jan 20, 2010 10:49 PM

Accepted Solutions (0)

Answers (3)

Answers (3)

stefan_grube
Active Contributor
0 Kudos

Usually there is no need for escaping ' and ", as the XML is valid anyway.

I think you should use a SAX parser. The SAX parser divides the XML in tags, namespaces and elements.

So you can do the String replace routines on element level only.

Regards

Stefan

Former Member
0 Kudos

Depending on how big your msgs are (or how often they arrive), if performance overhead is acceptable, then you could :

XML->1 String->apply reg ex to replace only " and ' within 2 tags ...

But this is only a dirty way of doing what a custom SAX parser would do (more elegantly and efficiently) ...

Chris

Former Member
0 Kudos

When the message arrives everything is escaped, however after executing the XSLT mapping &apos; and &qot; are de-escaped again.

I think both the SAX parster or the regex are a good approach to solve the problem.

Christophe, do you have any sample code available for the replacement with a regex? Guess that I have to find the relevanr substrings first, and then having a replace all run on that. But it would be very helpful to see a code sample there....

Thank you!

Edited by: Florian Guppenberger on Jan 21, 2010 2:20 PM

Edited by: Florian Guppenberger on Jan 21, 2010 4:02 PM

Former Member
0 Kudos

Florian,

Let me check my sources, I remember using something similar ...

Chris

former_member190389
Active Contributor
0 Kudos

Hi,

As Stefan said, there is no need of escaping ' and " characters .

Is it that are you receiving any error ?

Escaping De-escaping (if not required) will just add to the confusion.

Former Member
0 Kudos

Hello,

@Fariha: Unfortunately there is the need to escape ' and '" in my case. Problem is that I use a 2-step mapping (graphical / XSLT)

' and " are already escaped when calling the XSLT mapping. However the output of the XSLT contains ' and " again.

Our business partner however does not accept these two un-escaped characters

@Christophe: Would be great to see some sample code with regular patterns. This seems to be a little bit tricky as I have to apply a pattern to get only element values, then replace the characters and then somehow move it back to the original string......

At least this is what I was thinking of.

stefan_grube
Active Contributor
0 Kudos

> @Fariha: Unfortunately there is the need to escape ' and '" in my case. Problem is that I use a 2-step mapping (graphical / XSLT)

> ' and " are already escaped when calling the XSLT mapping. However the output of the XSLT contains ' and " again.

I wonder whether there is an option in XSLT to prevent de-escaping. Maybe you check an XSLT refernce first before you create a Java mapping.

Former Member
0 Kudos

Yes, this was my idea too, to check whether I can handle this in XSLT already.

One option might have been using character maps in XSLT 2.0 but obviously this cannot be handled in PI XSLT mapping.

stefan_grube
Active Contributor
0 Kudos

My idea was just setting a parameter for preventing the de-escaping in XSLT.

But I have not found anything. XSLT deescapes per default, I have no idea how to avoid this.

sunil_singh13
Active Contributor
0 Kudos

Hi Florian,

You can change the Unicode in Adapter engine from UTF8 to ISO... it supports &

the other solution is you can replace your & with some constant (like!!!!@!!!!!) using java mapping and at the receiver end you replace !!!!@!!!! with & to show the end user the exact string.

Thanks,

Sunil Singh

MichalKrawczyk
Active Contributor
0 Kudos

Hi Florian,

why reinvent the wheel ?:)

type : escaping java in google and take any of the many examples as your java mapping

escaping is totally not PI specific

Regards,

Michal Krawczyk

Former Member
0 Kudos

Hi,

yes, there are a couple of examples out there, however the problem is that I just want to have the " characters escaped that are part of the element value and not all others (such " characters that are part of the prolog or attributes)...

MichalKrawczyk
Active Contributor
0 Kudos

hi,

>>>yes, there are a couple of examples out there, however the problem is that I just want to have the " characters escaped that are part of the element value and not all others (such " characters that are part of the prolog or attributes)...

and do you think the prolog is PI/XI specific ?

Regards,

Michal Krawczyk

Former Member
0 Kudos

No, never said that,

However the java samples I have found replace all characters so I just wanted to know wether someone might have a good solution for this.

Former Member
0 Kudos

Are you able to bring the message with & in without any issues? I had problems bringing it in a project i was using.. I end up putting the dtd on the server. If need i can send you the path. That resolved my issue.