How to handle special character in XML document

Hi All,

We are storing the following data in a text type process variable and mapping it to generate .xml document:-

<Root>
<name>ABC</name>
<description> <Test Data> "Hello Everyone!"
The test isn't done & need further analysis. Refer the file path below:
File Path: \\abc\bqpv\¿01. Client File / 729
</description>
</Root>

We would like to know if there is any out of box feature to handle the characters like & \b <> " ' which is present in the above example inside the tag <description>

  Discussion posts and replies are publicly visible

Parents
  • Hi, 
    you can escape the characters

    " to  &quot;
    ' to  &apos;
    < to  &lt;
    > to  &gt;
    & to  &amp;
    or you can wrap attribute values in double quotes ("), e.g.
    <MyTag attr="If a&lt;b &amp; b&lt;c then a&lt;c, it's obvious"/> will resolve into
    If a<b & b<c then a<c, it's obvious as it can be found easy with google.
    
  • Thanks for the response. We have created a common rule to find the character and replace it with the required character individually. Hence, the same rule will check the characters individually. However, is there a way to check and all special characters at once and replace with the respective required characters. 

  • 0
    Certified Lead Developer
    in reply to anushrid0001

    You just need a single rule where you can pass in a string and it passes out the "sanitized" version.  I'm unclear whether that's what you already have, but this is about as good as you're going to get in Appian.

  • Thanks for the solution Mike. I have probably done that only. PFB my approach:

    Rule "ia_findAndReplaceCharacter" Code is as below:

    if(
    find(
    ri!search_text,
    ri!input
    ) = 0,
    ri!input,
    substitute(
    ri!input,
    ri!search_text,
    ri!replace_text
    )
    )

    Rule "ia_findAndReplaceAllCharacters" Code is as below:

    a!localVariables(
    local!lessThanChar: rule!ia_findAndReplaceCharacter(
    input: ri!input,
    search_text: "<",
    replace_text: "|"
    ),
    local!greaterThanChar: rule!ia_findAndReplaceCharacter(
    input: local!lessThanChar,
    search_text: ">",
    replace_text: "|"
    ),
    local!apostropheChar: rule!ia_findAndReplaceCharacter(
    input: local!greaterThanChar,
    search_text: "'",
    replace_text: "|"
    ),
    local!backspaceChar: rule!ia_findAndReplaceCharacter(
    input: local!apostropheChar,
    search_text: "\b",
    replace_text: "\B"
    ),
    local!ampersandChar: rule!ia_findAndReplaceCharacter(
    input: local!backspaceChar,
    search_text: "&",
    replace_text: "and"
    ),
    local!finalOutput: local!ampersandChar,
    local!finalOutput
    )

    Here, we have used rule  "ia_findAndReplaceCharacter" for each character. 

    Is there a way or any function which can find and replace any such special character instead of the above approach. 

  • This code replaces all XML special characters in a given XML text.

    http://xml.silmaril.ie/specials.html

    " to  &quot;
    ' to  &apos;
    < to  &lt;
    > to  &gt;
    & to  &amp;

    reduce(
      substitute(_,_,_),
      ri!xml,
      merge(
        {"&", "<", ">", """", "'"},
        {"&", "<", ">", """, "'"} /* Change this to the escape characters listed above.*/
      )
    )

  • Thanks Stefan for the solution. However, I would like to know if we can encode special characters in Appian in the similar manner which can be done in other programming language with functions like encode()

Reply Children