Wednesday 30 October 2013

XSLT - return filename of the input document

On occasion one needs to use the input filename of the document that is being transformed. This can be supplied as a parameter, but it can also be determined within the XSLT by using either the base-uri() or document-uri() functions. Use global variables to return the uri of the document and then using the forward slash to tokenize the variable. The last token will be the filename.



<xsl:variable name="base-uri" select="base-uri(.)"/>
<xsl:variable name="document-uri" select="document-uri(.)"/>
 
<xsl:variable name="filename" select="(tokenize($document-uri,'/'))[last()]"/>
 
<xsl:template match="/">
 <docs>
  <uri><xsl:value-of select="$document-uri"/></uri>
  <uri><xsl:value-of select="$base-uri"/></uri>
  <filename><xsl:value-of select="$filename"/></filename>
  <xsl:apply-templates/>
 </docs>
</xsl:template> 

No comments:

Post a Comment