简体   繁体   中英

XSLT mapping to remove double quotes which has PIPE delimited symbol inside

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

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>

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>

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