Jeff M
Jeff M

Reputation: 1

XSLT: Change part of a path string

I am an XSLT 1.0 newbie. In a string element in my XML, I want to change the directory parts of the path of a jar file (C:\abc\my.jar) to a static string and retain the jar file name ($PATH_VAR$\my.jar).

The original XML snippet looks like:

<object class="someclass">
   <property name="path">
      <string><![CDATA[C:\abc\my.jar]]></string>
   </property>
</object>

and I want the transformed XML to be:

<object class="someclass">
   <property name="path">
      <string><![CDATA[$PATH_VAR$\my.jar]]></string>
   </property>
</object>

Note that the original path can be any length (\\xyz\abc or Z:\abc\def\ghi), and the jar file can be named anything.

I'm not sure how to parse the original path string and change only the directories part of it. Please help!

-Jeff

Upvotes: 0

Views: 1372

Answers (2)

Rudolf Yurgenson
Rudolf Yurgenson

Reputation: 603

This is xslt 1.0. But looks like you are using xslt 2.0 so it's useless =)

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="xml" cdata-section-elements="string"/>

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

    <xsl:template match="string/text()">
        <xsl:choose>
            <xsl:when test="substring-after(., '\')">
                <xsl:call-template name="process">
                    <xsl:with-param name="left" select="."/>
                </xsl:call-template>
            </xsl:when>
            <!-- no slash == no $PATH_VAR$ -->
            <xsl:otherwise>
                <xsl:value-of select="."/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <xsl:template name="process">
        <xsl:param name="left"/>

        <xsl:choose>
            <!-- if we have at least one slash ahead - cut everything before first slash and call template recursively -->
            <xsl:when test="substring-after($left, '\')">
                <xsl:call-template name="process">
                    <xsl:with-param name="left" select="substring-after($left, '\')"/>
                </xsl:call-template>
            </xsl:when>
            <!-- if there are no slashes ahead then we have only file name left, that's all -->
            <xsl:otherwise>
                <xsl:value-of select="concat('$PATH_VAR$\', $left)"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>

Upvotes: 1

michael.hor257k
michael.hor257k

Reputation: 117102

Extracting only the file name - i.e. the part after the last \ separator - is easy in XSLT 2.0:

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

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

<xsl:template match="string/text()">
    <xsl:text>$PATH_VAR$\</xsl:text>
    <xsl:value-of select="tokenize(., '\\')[last()]"/>
</xsl:template>

</xsl:stylesheet>

Upvotes: 1

Related Questions