Calendar

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

View posts in large calendar

RecentComments

None

 
 
     
 

Here are ways to write translets that will do more than the just an Xslt transform. You might want to add an MD5 or SHA256 checksum to all the files within a tree. Xslt doesn't do this natively so you need to extend it.

Here is one way it uses C# as the Xslt scripting language

function T-AddChecksum{
    param ($inxml)
    begin{
        . .\invoke-transform.ps1
       [xml]$xslt = @'
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:ucjb="urn:cjb" version="1.0">
   
<msxsl:script language="C#" implements-prefix="ucjb">
   public string GetChecksum(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);
        }
   }
    </msxsl:script>

   
<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="ucjb: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>
'@
    }
    process{
        if ($_ -is [xml]){
            [xml](invoke-transform -inxml $_ -inxsl $xslt)
        }
    }
    end{
        if ($inxml -is [xml]){
            [xml](invoke-transform -inxml $inxml -inxsl $xslt)
        }
    }
}

It can be used like this

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

or
PS> T-AddChecksum [xml](gc .\tmp.xml )

and might produce

<root Name="root" Root="True" Date="2009/11/03 05:45:37">
   
<folder Name="test" Base="D:\powershell\test">
       
<file Name="test.txt" Length="836" Checksum="0D7439F5894B4E8EFEC8FB409635D0D8EA7A450E902F6B30B335907B5867DF16" />
       
<file Name="test.ps1" Length="3330" Checksum="C47313D06C6AADA288AF6D61E03EFD7FA7C52DD73AB097E9D556535D330798D3" />
       
<file Name="test.zip" Length="1290" Checksum="7F2CCA02F17FF0E9458C0777C659D6D00B80F1C9D2921AEC971AE9A82D296AA5" />
       
<file Name="tmp.xml" Length="4383" Checksum="1351245F9834D0406C42DD5AF622FCA691A9A36F440A7C88F389927800292303" />
   
</folder>
</root>

Here is the code

T-AddChecksumCScript.zip (1.03 kb)

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Add comment