Calendar

<<  June 2017  >>
MonTueWedThuFriSatSun
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789

View posts in large calendar

RecentComments

None

 
 
     
 

Another way to extend Xslt is to pass an extension object into the transform. 

function T-AddChecksum{
    param ($inxml)
    begin{
        . pslib:\xml\invoke-transform.ps1
    [xml]$xslt = @'
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    xmlns:cjb="cjb" version="1.0">
   
<xsl:template match="node()|@*">
       
<xsl:copy>
           
<xsl:apply-templates select="@*|node()" />
       
</xsl:copy>
   
</xsl:template>
   
<xsl:template match="*[local-name()='file']">
       
<xsl:variable name="fname">
           
<xsl:call-template name="get-path" />
       
</xsl:variable>
       
<xsl:copy>
           
<xsl:apply-templates select="@*|node()" />
           
<xsl:attribute name="Checksum">
               
<xsl:value-of select="cjb:GetChecksum(string($fname))" />
           
</xsl:attribute>
       
</xsl:copy>
   
</xsl:template>
   
<xsl:template name="get-path">
       
<xsl:for-each select="ancestor-or-self::*[not(@Root)]">
           
<xsl:value-of select="@Parent" />
           
<xsl:text>\</xsl:text>
           
<xsl:value-of select="@Name" />
       
</xsl:for-each>
   
</xsl:template>
</xsl:stylesheet>
'@

$code = @'
public class Checksum{
    public System.String GetChecksum(System.String file) {
        using (System.IO.FileStream stream = System.IO.File.OpenRead(file))
        {
            System.Security.Cryptography.SHA256Managed sha = new System.Security.Cryptography.SHA256Managed();
            byte[] checksum = sha.ComputeHash(stream);
            return System.BitConverter.ToString(checksum).Replace("-", System.String.Empty);
        }
   }
}
'@
        Add-Type -TypeDefinition $code
        $cs = new-object Checksum
    }
    process{
        if ($_ -is [xml]){
            [xml](invoke-transform -inxml $_ -inxsl $xslt -extensionobjects @{"cjb"=$cs})
        }
    }
    end{
        if ($inxml -is [xml]){
            [xml](invoke-transform -inxml $inxml -inxsl $xslt -extensionobjects @{"cjb"=$cs})
        }
    }
}

It can be used the same way as before

PS> . .\T-AddChecksum.ps1 #dot source translet
PS> . .\Get-DirAsXml.ps1 #dot source Get-DirAsXml
PS> Get-DirAsXml D:\powershell\test -props @{Length=""} | T-AddChecksum

Here is the code

T-AddChecksumCObject.zip (1.08 kb)

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Add comment