1

I need to transform this value:

    <MediaFile>F:\DEMO\TEST\HELLO.pm4</MediaFile>

to this:

<MediaFile>F:\\DEMO\\TEST\\HELLO.pm4</MediaFile>

I have tried a lot of thing, translate, concat, substring and replace but I didn't succeeded.

Here's precisely, what I've tried:

Source file:

        <?xml version="1.0" encoding="UTF-8" ?>
<Asset version="1.0">
    <Target>
    <Name>HELLO</Name>
    <MediaFile>F:\DEMO\TEST\HELLO.mp4</MediaFile>

</Target>
</Asset>

XSLT:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xsl:output method="text" indent="yes" encoding="UTF-8"/>
    <xsl:template match="/">

        <xsl:variable name="MediaFile">
            <xsl:value-of select="Asset/Target/MediaFile"/>
        </xsl:variable>

        <xsl:variable name="Media">
            <xsl:value-of select="replace($MediaFile,'\','\\')"/>
        </xsl:variable> 

            <xsl:element name="Media">
                <xsl:value-of select="$Media"/>
            </xsl:element>  
        </xsl:template>
</xsl:stylesheet>

I'm using xslt 2.0

Any idea? Thanks

Brice
  • 67
  • 2
  • 13
  • 2
    Which version of XSLT can you use? XSLT 2 or 3 offer all XPath 2 or 3 functions like `replace` https://www.w3.org/TR/xpath-functions/#func-replace – Martin Honnen Jun 01 '18 at 19:21
  • XSLT 2.0, I tried replace but didn't get anything good out of it, i'm sure i'm not doing it right.. – Brice Jun 01 '18 at 20:15
  • 1
    Can you edit your question to show what you have tried (in XSLT 2.0, with replace). You might not have been far off.... – Tim C Jun 01 '18 at 21:38
  • I just edit my question, yes I might have been closed to it at some point... – Brice Jun 01 '18 at 22:47
  • The thing is that I can replace anything else with no issue but the back slash seems to be the problem here... any solutions ? – Brice Jun 01 '18 at 23:31
  • 1
    Backslash is often used to change the behavior of the next character. Ex: \n represents a new line character. As such, to indicate a literal backslash it's often necessary to write '\\'. So, try replacing '\\' with '\\\\'. – Devon_C_Miller Jun 02 '18 at 03:30

1 Answers1

3

You would need to replace the below

<xsl:value-of select="replace($MediaFile,'\','\\')"/>

with

<xsl:value-of select="replace($MediaFile,'\\','\\\\')" />

In many programming languages, backslash is used to as an escape character to indicate the character following it needs special treatment. In this case, you would need to escape the character being replaced \ with \\ and also need to replace \\ with \\\\ (two backslashes).

Following question would help provide some detail explanation What does back slash "\" really mean?

Additionally, in the template that you have shared, the desired output is text, so there is no need to create an element named Media as it would come into picture if the desired output is XML. So the template code can be optimized as

<xsl:template match="/">
    <xsl:value-of select="replace(Asset/Target/MediaFile,'\\','\\\\')" />
</xsl:template>

Output

F:\\DEMO\\TEST\\HELLO.mp4
Aniket V
  • 3,183
  • 2
  • 15
  • 27