简体   繁体   中英

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..

<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>

Another way you could look at it:

<xsl:template match="text()">
    <xsl:variable name="len" select="string-length()" />
    <xsl:variable name="s" select="starts-with(., '&quot;')" />
    <xsl:variable name="e" select="substring(., $len) = '&quot;'" />
    <xsl:value-of select="substring(., 1 + $s, $len - $s - $e)" />
</xsl:template> 

Of course, if you know that some elements will always have leading and trailing quotes, you could do simply:

<xsl:template match="ns0:MsgId | ns0:CreDtTm">
    <xsl:copy>
        <xsl:value-of select="substring(., 2, string-length() - 2)" />
    </xsl:copy>
</xsl:template> 

provided you include:

xmlns:ns0="urn:iso:std:iso:20022:tech:xsd:pain.002.001.03"
exclude-result-prefixes="ns0"

in the xsl:stylesheet start-tag.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM