Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use a result of template (last-token) as a value of specific attribute during copy using XSLT?

I am trying to copy a XML-File and to change the value of an (specific) attribute (FilePath). The new Value of the attribute should be the last string after the last backslash . So i used the last-token template and then i saved it in a variable "result".
I always get empty value, do I call the "last-token" twice?
i am a student and i am not familiar with XSLT and that is a university project please help!!
Thanks

My XML:

<Document>
    <Field Type="DD_FilePath" Value="C:\Users\uka\Desktop\00000002.jpg"/>
    <Field Type="aaa" Value= "3"/>
    
</Document>

How transform XML should be:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<Document>
    <Field Type="DD_FilePath" Value="00000002.jpg"/>
    <Field Type="aaa" Value= "3"/>
    
</Document>

My XSLT:

<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:template match="@*|node()">
           <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
        
    </xsl:template>
<xsl:template match="Field[@Type='DD_FilePath']/@Value">
    <xsl:variable name="result">
      
        <xsl:apply-templates select="last-token"/> 
      
        
      </xsl:variable>
         <xsl:value-of select="$result" />
          <xsl:attribute name="Value">
       
            <xsl:value-of select="$result"/>
        </xsl:attribute>
     </xsl:template> 



   <xsl:template name="last-token">
    <xsl:param name="text"/>
    <xsl:param name="delimiter" select="'\'"/>
    
    <xsl:choose>
        <xsl:when test="contains($text, $delimiter)">
            <!-- recursive call -->
            <xsl:call-template name="last-token">
                <xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$text"/>
        </xsl:otherwise>
    </xsl:choose>
    
</xsl:template>   

</xsl:stylesheet>

>Solution :

Named templates need to be called, not applied – typically with a parameter. Instead of:

<xsl:template match="Field[@Type='DD_FilePath']/@Value">
    <xsl:variable name="result">
      
        <xsl:apply-templates select="last-token"/> 
      
        
      </xsl:variable>
         <xsl:value-of select="$result" />
          <xsl:attribute name="Value">
       
            <xsl:value-of select="$result"/>
        </xsl:attribute>
     </xsl:template> 

do:

<xsl:template match="Field[@Type='DD_FilePath']/@Value">
    <xsl:variable name="result">
        <xsl:call-template name="last-token">
            <xsl:with-param name="text" select="."/>
        </xsl:call-template>
    </xsl:variable>
    <xsl:attribute name="Value">
        <xsl:value-of select="$result"/>
    </xsl:attribute>
</xsl:template> 

or simply:

<xsl:template match="Field[@Type='DD_FilePath']/@Value">
    <xsl:attribute name="Value">
        <xsl:call-template name="last-token">
            <xsl:with-param name="text" select="."/>
        </xsl:call-template>
    </xsl:attribute>
</xsl:template> 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading