Search code examples
xsltxslt-1.0xslt-grouping

XSLT mapping to remove double quotes only at starting and ending positions


Experts, i need to write XSLT 1.0 code to eliminate the double quotes at starting and ending of the field ( in short, starting and ending position double quote only), not supposed to remove any other double quote in the input field.

Input:

<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:iso:std:iso:20022:tech:xsd:pain.002.001.03">
    <CstmrPmtStsRpt>
        <GrpHdr>
            <MsgId>"88245"322"2608""</MsgId>
            <CreDtTm>"22219"</CreDtTm>
             <OrgnlNbOfTxs>12"3"41</OrgnlNbOfTxs>
        </GrpHdr>
       
    </CstmrPmtStsRpt>
</Document>

** Desired Output:**

<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:iso:std:iso:20022:tech:xsd:pain.002.001.03">
    <CstmrPmtStsRpt>
        <GrpHdr>
            <MsgId>88245"322"2608"</MsgId>
            <CreDtTm>22219</CreDtTm>
             <OrgnlNbOfTxs>12"3"41</OrgnlNbOfTxs>
        </GrpHdr>
       
    </CstmrPmtStsRpt>
</Document>

** XSLT I used is below:**

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>

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

    <xsl:template match="*/text()">
        <xsl:value-of select="translate(., '\&quot;', '')"/>
    </xsl:template>

</xsl:stylesheet>

This XSLT removing the all the double quotes from my input field, please assist here..


Solution

  • <xsl:template match="text()">
        <xsl:value-of 
           select="concat(
                     translate(substring(., 1, 1), '&quot;', ''), 
                     substring(., 2, string-length() - 2), 
                     translate(substring(., string-length()), '&quot;', '')
                   )"/>
    </xsl:template>