DataPower

DataPower

Join this online group to communicate across IBM product users and experts by sharing advice and best practices with peers and staying up to date regarding product enhancements.

 View Only
Expand all | Collapse all

How to handle the special characters in xslt

  • 1.  How to handle the special characters in xslt

    Posted Fri November 06, 2020 12:26 PM

    Hi Team,

    I have a requirement like below . In my input xml I am getting different special characters. how do I remove the sepcial characters in xslt from the xml.

    Could you please help me . Below is the sample xml ?

    Input:

    <comment>Test values : &#34;Test&#34;"</comment>

    <comment>Test values2 : &~%Test*;"</comment>

    Expected output

    <comment>Test values Test</comment>

    <comment>Test values2 Test</comment>

    thank you

    SK



    #DataPower
    #Support
    #SupportMigration


  • 2.  RE: How to handle the special characters in xslt

    Posted Mon November 09, 2020 10:36 AM

    Hi,

    perhaps you can use some kind of replace function. for example EXSLT regexp:replace

    http://exslt.org/regexp/functions/replace/index.html

    --HP



    #DataPower
    #Support
    #SupportMigration


  • 3.  RE: How to handle the special characters in xslt

    Posted Tue November 10, 2020 11:06 AM

    Hi Team,


    Thank for the quick replay.


    But I need that to be achievd in xslt1.0 and the input is array block. Could you please help where exactly to make the changes to achieve different special characters.


    Input

    <comments>

     <comments>

    <comment>Test values : &#34;Test&#34;"</comment>

     </comments>

     <comments>

    <comment>Test values2 : &~%Test*;"</comment>

     </comments>

     <comments>

      


    Expected output


    <comments>

     <comments>

    <comment>Test values Test</comment>

     </comments>

     <comments>

    <comment>Test values2 Test</comment>

     </comments>

     <comments>



    Thank you

    SK




    #DataPower
    #Support
    #SupportMigration


  • 4.  RE: How to handle the special characters in xslt

    Posted Tue November 10, 2020 11:26 AM

    EXSLT regexp:replace is supported in XSLT 1.0 so you should be able to use it. By the way, how large is the list of special characters that you have to filter out?

    --HP



    #DataPower
    #Support
    #SupportMigration


  • 5.  RE: How to handle the special characters in xslt

    Posted Tue November 10, 2020 11:42 AM

    Hi hpernaa,


    The comment list is completeley depend on the input and it is any array as I mentioned in the below block.Example it input might receive morethan 20 comments at a time.


    Could you please help how to do in xslt 1.0 and will it support to Datapower ?


    <comments>

    <!-- This comments block will repeat --->

     <comments>

    <comment>Test values : &#34;Test&#34;"</comment>

     </comments>

     <comments>

    <comment>Test values2 : &~%Test*;"</comment>

     </comments>

    <!-- This comments block will repeat --->

     <comments>



    Thank you

    SK







    #DataPower
    #Support
    #SupportMigration


  • 6.  RE: How to handle the special characters in xslt

    Posted Tue November 10, 2020 11:48 AM

    Ok, I meant that what is your list of special characters?


    --HP



    #DataPower
    #Support
    #SupportMigration


  • 7.  RE: How to handle the special characters in xslt

    Posted Tue November 10, 2020 11:57 AM

    Here I will get the below list of sepcial charactes in the repeated comments block based on that I have to replace with values.


    &#10;&#13;---> Replace with (\r\n) 

      &#09;, ---> Replace with (\r\n) 

      &#92---> Replace with (\\)

      &#63 ---> Replace with (?)

    &#38; ---> Replace with (&)

      &#60; ---> Replace with (<)

      &#62; ---> Replace with (>)

      &#34; ---> Replace with (")

      &#39; ---> Replace with (\\)


    Thankk you

    SK



    #DataPower
    #Support
    #SupportMigration


  • 8.  RE: How to handle the special characters in xslt

    Posted Tue November 10, 2020 02:27 PM

    Most of the characters you listed are XML escape characters that cannot appear in their literal form inside XML. So the escaped form on the left is for a reason... If you'll try to replace the characters, you'll break your XML.

    https://www.w3.org/TR/REC-xml/#syntax


    You can try to set minimum output escaping rule but I'm not entirely sure what you are trying to achieve.

    --HP



    #DataPower
    #Support
    #SupportMigration


  • 9.  RE: How to handle the special characters in xslt

    Posted Tue November 10, 2020 04:59 PM

    Hi


    Thanks for the reply.


    My actual requirement is I am getting the mentioned comment block response from one of the backend, but I have to remove those escape characters from the comments block and need to send it to the same response to other backend without escape characters like below.

    Is there any way to achieve these in xslt1.0 ?


    Expected output


    <comments>

     <comments>

    <comment>Test values Test</comment>

     </comments>

     <comments>

    <comment>Test values2 Test</comment>

     </comments>

     <comments>





    #DataPower
    #Support
    #SupportMigration


  • 10.  RE: How to handle the special characters in xslt

    Posted Wed November 11, 2020 03:11 PM

    Here is an example with translate() that you can modify so that it fits into your use case:


    <xsl:stylesheet version="1.0"

    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">


    <xsl:output method="xml"/>

    <xsl:template match="*[local-name()='comment']/text()">


    <xsl:if test="translate(., '&quot;:~%*;', '')">

    <xsl:value-of select="translate(., '&quot;:~%*;', '')"/>

    </xsl:if>

    <xsl:apply-templates/>


    </xsl:template>

    <xsl:template match="*|node()">

        <xsl:copy>

          <xsl:apply-templates select="*|node()"/>

        </xsl:copy>

      </xsl:template>

    </xsl:stylesheet>


    --HP



    #DataPower
    #Support
    #SupportMigration