Search code examples
xmlxslt-2.0

XSLT dealing with network paths


I'm trying to extract the "Name" part from a network path such as:

\\Server01\Name\

XSLT 2.0 using Saxon 6.5.5

How can I get the Name part of the NetworkPath attribute:

Name

XML:

<?xml version="1.0" encoding="UTF-8"?>
<Example>
    <Network Name="test" NetworkPath="\\Server01\Name\"/>
</Example>


I've tried the following:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="text" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />

    <xsl:template match="/">
 
    
    <xsl:variable name="extractNetworkPathName">
        <xsl:variable name="input" select="/Example/Network/@NetworkPath"/>        
        <xsl:variable name="url_minus" select="substring-after($input,'\\')"/>

            
             <xsl:value-of select="substring-before($url_minus,'\')"/> 
    
    </xsl:variable>
      <xsl:value-of select="$extractNetworkPathName"/>
   
    </xsl:template>
    

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

This is currently getting me 'Server01' but I require the 'Name' instead.


Solution

  • So I've managed it with the following:

    <?xml version="1.0" encoding="UTF-8" ?>
    <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
        <xsl:output method="text" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
    
        <xsl:template match="/">
     
        
        <xsl:variable name="extractedNetworkPathName">
            <xsl:variable name="input" select="/Example/Network/@NetworkPath"/>        
            <xsl:variable name="url_minus" select="substring-after($input,'\\')"/>
            <xsl:variable name="url_after" select="substring-after($url_minus,'\')"/>      
            <xsl:variable name="networkPathName" select="substring-before($url_after,'\')"/>
            <xsl:value-of select="$networkPathName"/>
        </xsl:variable>
    
            <xsl:value-of select="$extractedNetworkPathName"/>
       
        </xsl:template>
        
    
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
    </xsl:transform>
    

    This gives an output of 'Name' as expected. Is there a cleaner way for this possibly with tokenize?