CodeForFood
CodeForFood

Reputation: 39

Replace value in XML with XSLT

I'm working on a XML file that is like

<?xml version="1.0" encoding="UTF-8"?>
<Properties>
    <Property>
        <Name>Email</Name>
        <Value>[email protected]</Value>
    </Property>
    <Property>
        <Name>Resposta</Name>
        <Value>here i have ; to be replace by nothing</Value>
    </Property>
    <Property>
        <Name>NPS</Name>
        <Value>8</Value>
    </Property>
</Properties>

and my map XSLT is like:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">
  <xsl:output method="text" omit-xml-declaration="yes" indent="no"/>
  <xsl:strip-space elements="*"/>
    <xsl:template match="Properties">
       <xsl:variable name="Email" select="/Properties/Property[1]/Value/text()"/>
       <xsl:variable name="Resposta" select="/Properties/Property[2]/Value/text()"/>
       <xsl:variable name="NPS" select="/Properties/Property[3]/Value/text()"/>
       <xsl:value-of select="normalize-space($Email)"/>;<xsl:value-of select="normalize-space($Resposta)"/>;<xsl:value-of select="normalize-space($NPS)"/>
    </xsl:template>
</xsl:stylesheet>

How can I replace ";" by nothing using replace at my XSLT map?
For example: here I have ";" to be replaced by nothing.
And expected: here I have to be replaced by nothing.

Upvotes: 0

Views: 928

Answers (2)

Alejandro
Alejandro

Reputation: 1882

First, in XSLT 2.0 you have sequences and you can use separator attribute of value-of instruction. So, following Michael Kay comments about translate and changing just your value-of instruction to:

<xsl:value-of select="$Email, normalize-space(translate($Resposta,';','')), $NPS"
     separator=";" />

Output:

[email protected];here i have to be replace by nothing;8

Second, if you really want to use fn:replace:

<xsl:value-of select="$Email, normalize-space(replace($Resposta,';','')), $NPS"
     separator=";" />

Upvotes: 1

zx485
zx485

Reputation: 29022

You can achieve this easily with the XPath-1.0 function fn:translate.
So change your xsl:value-of to

<xsl:value-of select="normalize-space(translate($Email,';',''))"/>;<xsl:value-of select="normalize-space(translate($Resposta,';',''))"/>;<xsl:value-of select="normalize-space(translate($NPS,';',''))"/>

Upvotes: 1

Related Questions