-1

Experts, i need to write XSLT 1.0 code to eliminate the Pipe delimited symbol inside double quotes and also need to remove those double quotes..

Input:

<?xml version="1.0" encoding="utf-8"?>
<ns:MT_FILE>
    <LN>
        <LD>EXTRACT|"28|53"|1308026.7500|1176</LD>
    </LN>
    <LN>
        <LD>DETAIL|1176|"LOS LE|OS PARRILLA"|Y|R||||<LD>
    </LN>
    
</ns:MT_FILE>

** Desired Output:**

<?xml version="1.0" encoding="utf-8"?>
<ns:MT_FILE>
    <LN>
        <LD>EXTRACT|2853|1308026.7500|1176</LD>
    </LN>
    <LN>
        <LD>DETAIL|1176|LOS LE OS PARRILLA|Y|R||||<LD>
    </LN>
    
</ns:MT_FILE>

** 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 all the double quotes from my input field, please assist here..

Sree
  • 17
  • 7

2 Answers2

0

If it can be assumed that quotes will always come in pairs, you could do:

XSLT 1.0

<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>
michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
  • Thanks for the Code, I also need to remove double quotes from the input field, as of now we are considering only double quotes with Pipe delimited symbol. to the same code how can i append code to remove double quotes from string. – Sree Oct 14 '21 at 14:45
  • Please post a new question with your new problem. – michael.hor257k Oct 14 '21 at 15:06
  • https://stackoverflow.com/questions/69677816/xslt-to-remove-double-quotes-and-also-pipe-symbol-inside-the-double-quotes i posted here. – Sree Oct 22 '21 at 13:33
0

As you tagged as EXSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0"
  xmlns:regexp="http://exslt.org/regular-expressions"
  exclude-result-prefixes="regexp">


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

    <xsl:template match="LD/text()">
        <xsl:value-of select="regexp:replace(., '(&quot;)([^|]+)\|([^&quot;]+)(&quot;)', 'g', '$2$3')"/>
    </xsl:template>
    
</xsl:stylesheet>
Martin Honnen
  • 160,499
  • 6
  • 90
  • 110