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

Time:07-26

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:

<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>

CodePudding user response:

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> 
  • Related