Sree
Sree

Reputation: 19

XSLT to handle quotes and Pipe Delimited symbol

Experts, i need to write XSLT 1.0 code to remove the quotes for multiple conditions.

CASE1: Remove the double quotes CASE2: Remove the double quotes + delete the PIPE symbol inside that double quotes (IF exist) CASE3: Remove Single quote " from the input field.

Input:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ns0:Accounting xmlns:ns0="http://sample.com">
        <Record>
            <DRCR>"DR"</DRCR>
            <GLREFERENCE>"TEST|CASE"</GLREFERENCE>
            <GLVALUEDATE>EXAM"PLE</GLVALUEDATE>
            <GLACCOUNTNUMBER>"1160</GLACCOUNTNUMBER>
            <GLEXAMPLE>123</GLEXAMPLE>
            <GLEXAMPLE1>EXTRACT|2021-06-16|2853|1308026.7500|1176</GLEXAMPLE1>
        </Record>       
</ns0:Accounting>

** Desired Output:**

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ns0:Accounting xmlns:ns0="http://sample.com">
        <Record>
            <DRCR>DR</DRCR>
            <GLREFERENCE>TEST CASE</GLREFERENCE>
            <GLVALUEDATE>EXAMPLE</GLVALUEDATE>
            <GLACCOUNTNUMBER>1160</GLACCOUNTNUMBER>
            <GLEXAMPLE>123</GLEXAMPLE>
<GLEXAMPLE1>EXTRACT|2021-06-16|2853|1308026.7500|1176</GLEXAMPLE1>
        </Record>       
</ns0:Accounting>

** XSLT I tried:**

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="text()">
    <xsl:call-template name="process">
        <xsl:with-param name="text" select="."/>
    </xsl:call-template>
</xsl:template>

<xsl:template name="process">
    <xsl:param name="text"/>
    <xsl:choose>
        <xsl:when test="contains($text, '&quot;')">
            <xsl:value-of select="substring-before($text, '&quot;')"/>
            <xsl:value-of select="translate(substring-before(substring-after($text, '&quot;'), '&quot;'), '|', '')"/>
            <xsl:call-template name="process">
                <xsl:with-param name="text" select="substring-after(substring-after($text, '&quot;'), '&quot;')"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$text"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

</xsl:stylesheet>

This XSLT not handling case 3, which has single quote in the input field. Please assist here..

Upvotes: 0

Views: 694

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117102

Maybe something like this could work for you:

XSLT 1.0 (+ EXSLT node-set function)

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="text()">
    <xsl:choose>
        <xsl:when test="contains(., '&quot;')">
            <xsl:variable name="tokens">
                <xsl:call-template name="tokenize">
                    <xsl:with-param name="text" select="."/>
                </xsl:call-template>
            </xsl:variable>
            <xsl:for-each select="exsl:node-set($tokens)/token">
                <xsl:choose>
                    <xsl:when test="(position()=1 or position()=last()) and last() > 1">
                        <xsl:value-of select="."/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="translate(., '|', '')"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:for-each>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="."/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

<xsl:template name="tokenize">
    <xsl:param name="text"/>
    <xsl:param name="delimiter" select="'&quot;'"/>
        <xsl:variable name="token" select="substring-before(concat($text, $delimiter), $delimiter)" />
        <xsl:if test="$token">
            <token>
                <xsl:value-of select="$token"/>
            </token>
        </xsl:if>
        <xsl:if test="contains($text, $delimiter)">
            <!-- recursive call -->
            <xsl:call-template name="tokenize">
                <xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
            </xsl:call-template>
        </xsl:if>
</xsl:template>

</xsl:stylesheet>

Note that this does not check the parity of the quotation marks. Any vertical bar character that is both preceded and followed by a quotation mark will be removed. For example, an input of:

<EXAMPLE>abc|123"def|456"ghi|789"jkl|012</EXAMPLE>

will be transformed to:

<EXAMPLE>abc|123def456ghi789jkl|012</EXAMPLE>

Upvotes: 1

Related Questions