Calendar

<<  October 2017  >>
MonTueWedThuFriSatSun
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345

View posts in large calendar

RecentComments

None

 
 
     
 
There is no reason why a Translet shouldn't use XQuery as opposed to Xslt. So here is one that will transform a DAXml into a Windows Play List (WPL) using XQuery. It is quite simple function T-DAXmlToWpl{     param ($inxml)     begin{         Add-Type -path "PSLib:\xml\xquery\querymachine\QueryMachine.XQuery.dll"         $xq = @' (:<?wpl version="1.0"?>:) <smil>     <head>         <meta name="Generator" content="Powershell Translet T-Dax2Wpl"/>         <author>Chris Bayes</author>         <title>Playlist</title>     </head>     <body>         <seq>         { for $f in //file return              <media src="{$f/@FullName}" />         }         </seq>     </body> </smil> '@     }     process{         if ($_ -is [xml]){             $xqresult = [DataEngine.XQuery.XPathFactory]::QuerySingleNode($_, $xq)             $xqresult.OuterXml         }     }     end{         if ($inxml -is [xml]){             $xqresult = [DataEngine.XQuery.XPathFactory]::QuerySingleNode($inxml, $xq)             $xqresult.OuterXml         }     } } I am using OuterXml here because the result of the query is a single XmlElement (<smil>). The result of this translet would usually be written to a file with the Set-Content commandlet so it is ok to pass a string down the pipeline. If an [xml] object is required then [xml]($xqresult.OuterXml) can be used instead. I will have to do some timings to see if importing $xqresult into a new emty [xml] object is quicker than casting to an [xml] object. It can be used like this PS> . .\T-DAXmlToWpl.ps1 PS> Get-DirAsXml ..\test -props=@{FullName=""} | T-DAXmlToWpl | sc playlist.wpl After a bit of testing I changed the XQuery to     $xq = @' (:<?wpl version="1.0"?>:) document{ <smil>     <head>         <meta name="Generator" content="Powershell Translet T-Dax2Wpl"/>         <author>Chris Bayes</author>         <title>Playlist</title>     </head>     <body>         <seq>         { for $f in //file return             <media src="{$f/@FullName}" />         }         </seq>     </body> </smil> } '@ and QuerySingleNode returns an XmlDocument :-) however it has a <?xml version="1.0" encoding="utf-8"?> xml declaration which is not part of the SMIL spec but Windows Media Player doesn't seem to mind. I then tried function T-DAXmlToWpl{     param ($inxml)     begin{         Add-Type -path "PSLib:\xml\xquery\querymachine\QueryMachine.XQuery.dll"         $xq = @' document{     <?wpl version="1.0"?>,<smil>         <head>             <meta name="Generator" content="Powershell Translet T-DAXmlToWpl"/>             <author>Chris Bayes</author>             <title>Playlist</title>         </head>         <body>             <seq>             {                 for $f in //file[@Extension=".mp3"]                 return                   <media src="{$f/@FullName}" />             }             </seq>         </body>     </smil> } '@     }     process{         if ($_ -is [xml]){             [DataEngine.XQuery.XPathFactory]::QuerySingleNode($_, $xq)         }     }     end{         if ($inxml -is [xml]){             [DataEngine.XQuery.XPathFactory]::QuerySingleNode($inxml, $xq)         }     } } which produces something like this <?xml version="1.0" encoding="utf-8"?> <?wpl version="1.0"?> <smil>     <head>         <meta name="Generator" content="Powershell Translet T-DAXmlToWpl" />         <author>Chris Bayes</author>         <title>Playlist</title>     </head>     <body>         <seq>             <media src="D:\music\Culture - Two Sevens Clash (30th Anniversary Ed.) TQMP\04_Culture - Two Sevens Clash.mp3" />             ...         </seq>     </body> </smil> which has the xml declaration and the wpl processing instruction which Windows Media Player also plays so I will leave it like that. Because we are producing an XmlDocument we need to use the Format-Xml commandlet before we can use the Set-Content commandlet, something like this PS> Get-DirAsXml D:\music -props @{FullName="";Extension=""} | T-DAXmlToWpl | Format-Xml | Set-Content allplaylist.wpl
I recently spotted Standalone XQuery Implementation in .NET on Codeplex and decided to integrate it with DAXml explorer. I'm going to start with a previous post and add a few things. A ToolBar to switch to XQuery search and run the XQuery <ToolBarTray Background="White" Grid.Row="0">     <ToolBar Band="1" BandIndex="0">         <Button Name="SEARCHTOOL">             <TextBlock>Search</TextBlock>         </Button>         <Button Name="RUNTOOL">             <TextBlock>Run</TextBlock>         </Button>     </ToolBar>     <ToolBar Band="1" BandIndex="1" Visibility="{Binding ElementName=TABCONTROL, Path=Visibility}">         <TextBlock FontSize="20"> A </TextBlock>         <Slider Name="TEXTSIZE" Maximum="50" Width="100" Value="12" Orientation="Horizontal" HorizontalAlignment="Left" AutoToolTipPlacement="BottomRight" AutoToolTipPrecision="2" />     </ToolBar> </ToolBarTray> and a TabControl to switch between the XQuery and the results <TabControl Name="TABCONTROL" Grid.Column="2" Grid.Row="1" Visibility="Hidden">     <TabItem Name="TABXQUERY" Header="XQuery">         <TabItem.Content>             <TextBox Name="TABQUERYSOURCE" AcceptsReturn="True" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" FontSize="{Binding ElementName=TEXTSIZE, Path=Value}" />         </TabItem.Content>     </TabItem>     <TabItem Name="XQUERYRESULT" Header="Result">         <TabItem.Content>             <ListView Name="LISTVIEW2" Grid.Column="2" Grid.Row="1" HorizontalAlignment="Stretch" Visibility="Visible">                 <ListView.View>                     <GridView AllowsColumnReorder="true" ColumnHeaderContainerStyle="{DynamicResource CustomHeaderStyle}">                         <GridViewColumn Header="Name" CellTemplate="{StaticResource ItemTemplate}">                         </GridViewColumn>                     </GridView>                 </ListView.View>             </ListView>         </TabItem.Content>     </TabItem> </TabControl> We need a couple of events wired up $SEARCHTOOL.add_Click({     if ($TABCONTROL.Visibility -eq [System.Windows.Visibility]::Visible){         $TABCONTROL.Visibility = [System.Windows.Visibility]::Hidden         $LISTVIEW.Visibility = [System.Windows.Visibility]::Visible     }else{         $TABCONTROL.Visibility = [System.Windows.Visibility]::Visible         $LISTVIEW.Visibility = [System.Windows.Visibility]::Hidden     } }) that will switch from normal to search view. And an event to do the XQuery on the DAXml $RUNTOOL.add_Click({     Add-Type -path "D:\QueryMachine.XQuery.1.3\QueryMachine.XQuery.1.3\QueryMachine.XQuery.dll"     $xqresult = [DataEngine.XQuery.XPathFactory]::QueryNodes($xml, $TABQUERYSOURCE.Text)     $LISTVIEW2.ItemsSource = $xqresult     $TABXQUERYRESULT.IsSelected = $true }) This might be displayed like this Normal explorer view. XQuery search view. XQuery result view. It can be used like this Xaml-DisplayDirExp (get-dirasxml ..\test -props @{Length="";LastWriteTime="";Extension="";FullName=""} ) or like this Xaml-DisplayDirExp (get-dirasxml ..\test -CustomProps {     param ($element, $directory, $file, $prefix, $namespace)     try{         [xml]$xmp = (pslib:\xml\ReadingXMP.exe $file.FullName)         $xmpr = $xmp.SelectSingleNode("/*")         $inode = $element.OwnerDocument.ImportNode($xmpr, $true)         [void]$element.AppendChild($inode)     }catch{} } -props @{Length="";LastWriteTime="";Extension="";FullName=""} ) which will use the ReadingXMP from this previous post to stuff XMP RDF from JPG files into the DAXml. Depending on which method you use will depend on the XQuery you use. With DAXml stuffing you can use an XQuery like this declare namespace MPReg="http://ns.microsoft.com/photo/1.2/t/Region#"; for $a in //file[@Extension=".jpg"][//MPReg:PersonDisplayName = "Chris Bayes"] order by $a/@Length return $a and if you use sidecar files then you can use XQuery like this declare namespace MPReg="http://ns.microsoft.com/photo/1.2/t/Region#"; for $a in //file[@Extension=".jpg"] where doc(concat(@FullName, ".xmp"))//MPReg:PersonDisplayName = "Chris Bayes" order by $a/@Length return $a either way the XQuery will return all JPG files in all your folders where 'Chris Bayes' is in the MPReg:PersonDisplayName of the XMP RDF ordered by the file size. If you used order by $a//exif:GPSLatitude you would get all JPG files in all your folders where 'Chris Bayes' is in the MPReg:PersonDisplayName of the XMP RDF ordered by the e/w distance from the Greenwich Meridian. Here is the code Xaml-DisplayDirExpXQuerySearch.zip
I wanted to get the raw XMP RDF data from some JPEG files. I looked at Microsoft Windows Imaging Component and although it will get individual metadata properties I couldn't find a way to get the whole XMP RDF packet. I saw some posts about extracting it using a substring aproach by getting all the data between <?xpacket begin='´╗┐' id='some uid'?> and <?xpacket end='w'?> but there can be several RDF packets in a file and it also seems that it can be zipped so it doesn't work so well. I then spotted the Adobe XMP Toolkit and this has handlers for about 24 different file types. It is written in C++ and is only provided as source code. However it does have Visual Studio Solution files and can be built with Visual Studio Express C++ free edition. So download that. There are a couple of third-party things it needs (expat, zlib, QT) so download those too and put them in the third-party folder as per the ReadMe.txt files. There are some walkthroughs showing how to use the toolkit and the ReadingXMP project is a good place to start. It shows how to read individual properties which is what we don't want so remove all of that properties code and replace it with this if(ok) { // Create the xmp object and get the xmp data SXMPMeta meta; string rawrdf; myFile.GetXMP(&meta, &rawrdf, 0); cout << rawrdf << endl; // Close the SXMPFile. The resource file is already closed if it was // opened as read only but this call must still be made. myFile.CloseFile(); } else { cout << "Unable to open " << filename << endl; } In case you don't know C++ here is the file. Just replace the ReadingXMP.cpp file in the source folder. ReadingXMP.cpp.zip Build the solution and you will get a ReadingXMP.exe file in the targets folder. In case you don't want to go to all that trouble here is the executable. ReadingXMP.exe.zip You can use it from the DOS command line > ReadingXMP.exe somefile.jpg You can extract the data into a 'sidecar file' like this > ReadingXMP.exe somefile.jpg > somefile.jpg.xmp You can use it in Powershell like this PS> [xml]$xmp = (.\ReadingXMP.exe somefile.jpg) Which reads the XMP RDF directly into the $xmp object. To keep in the theme of my previous posts a useful -CustomProps param for Get-DirAsXml can go something like this if you only want one or two properties (or just use WIC). get-dirasxml ..\test -CustomProps {     param ($element, $directory, $file, $prefix, $namespace)     try{         [xml]$xmp = (.\ReadingXMP.exe $file.FullName)         $long = $xmp.SelectSingleNode("//*[local-name() = 'GPSLongitude']/text()").Value         $lati = $xmp.SelectSingleNode("//*[local-name() = 'GPSLatitude']/text()").Value         [void]$element.SetAttribute("GPSLongitude", $namespace, $long)         [void]$element.SetAttribute("GPSLatitude", $namespace, $lati)     }catch{} } | Format-Xml and might produce <root Name="root" Root="True" Date="2010/01/22 01:24:45">     <folder Name="test" Base="D:\powershell\blog\test" Parent="D:\powershell\blog">         <folder Name="test2">             <file Name="test.ps1" />             <file Name="test.txt" />             <file Name="test.zip" />             <file Name="tmp.xml" />         </folder>         <file Name="test.jpg" GPSLongitude="0,0E" GPSLatitude="51,28.927873651518N" />         <file Name="test.ps1" />         <file Name="test.txt" />         <file Name="test.zip" />         <file Name="tmp.xml" />     </folder> </root> If you want the whole XMP RDF packet you can do something like this get-dirasxml ..\test -CustomProps {     param ($element, $directory, $file, $prefix, $namespace)     try{         [xml]$xmp = (.\ReadingXMP.exe $file.FullName)         $xmpr = $xmp.SelectSingleNode("/*")         $inode = $element.OwnerDocument.ImportNode($xmpr, $true)         [void]$element.AppendChild($inode)     }catch{} } | Format-Xml Which might produce something like this <root Name="root" Root="True" Date="2010/01/22 02:00:07">     <folder Name="test" Base="D:\powershell\blog\test" Parent="D:\powershell\blog">         <folder Name="test2">             <file Name="test.ps1" />             <file Name="test.txt" />             <file Name="test.zip" />             <file Name="tmp.xml" />         </folder>         <file Name="test.jpg">             <x:xmpmeta xmlns:x="adobe:ns:meta/">                 <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">                     <rdf:Description xmlns:dc="http://purl.org/dc/elements/1.1/" rdf:about="uuid:faf5bdd5-ba3d-11da-ad31-d33d75182f1b">                         <dc:subject>                             <rdf:Bag>                                 <rdf:li>Chris</rdf:li>                             </rdf:Bag>                         </dc:subject>                     </rdf:Description>                     <rdf:Description xmlns:MicrosoftPhoto="http://ns.microsoft.com/photo/1.0" rdf:about="uuid:faf5bdd5-ba3d-11da-ad31-d33d75182f1b">                         <MicrosoftPhoto:LastKeywordXMP>                             <rdf:Bag>                                 <rdf:li>Chris</rdf:li>                             </rdf:Bag>                         </MicrosoftPhoto:LastKeywordXMP>                     </rdf:Description>                     <rdf:Description xmlns:MP="http://ns.microsoft.com/photo/1.2/">                         <MP:RegionInfo>                             <rdf:Description>                                 <MPRI:Regions xmlns:MPRI="http://ns.microsoft.com/photo/1.2/t/RegionInfo#">                                     <rdf:Bag>                                         <rdf:li>                                             <rdf:Description>                                                 <MPReg:Rectangle xmlns:MPReg="http://ns.microsoft.com/photo/1.2/t/Region#">                                                     0.200521, 0.257813, 0.072917, 0.054688                                                 </MPReg:Rectangle>                                                 <MPReg:PersonDisplayName xmlns:MPReg="http://ns.microsoft.com/photo/1.2/t/Region#">                                                     Chris Bayes</MPReg:PersonDisplayName>                                                     <MPReg:PersonEmailDigest xmlns:MPReg="http://ns.microsoft.com/photo/1.2/t/Region#">                                                         267675C2A3CDXXXXXXXXXX799647B6CA144                                                     </MPReg:PersonEmailDigest>                                                     <MPReg:PersonLiveIdCID xmlns:MPReg="http://ns.microsoft.com/photo/1.2/t/Region#">                                                         -46641XXXXXXX70720                                                     </MPReg:PersonLiveIdCID>                                                 </rdf:Description>                                         </rdf:li>                                     </rdf:Bag>                                 </MPRI:Regions>                             </rdf:Description>                         </MP:RegionInfo>                     </rdf:Description>                     <rdf:Description xmlns:xmp="http://ns.adobe.com/xap/1.0/">                         <xmp:CreatorTool>Microsoft Windows Live Photo Gallery14.0.8081.709</xmp:CreatorTool>                         <xmp:creatortool>Microsoft Pro Photo Tools</xmp:creatortool>                     </rdf:Description>                     <rdf:Description xmlns:tiff="http://ns.adobe.com/tiff/1.0/">                         <tiff:Software>Microsoft Windows Live Photo Gallery14.0.8081.709</tiff:Software>                         <tiff:software>Microsoft Pro Photo Tools</tiff:software>                         <tiff:Orientation>1</tiff:Orientation>                     </rdf:Description>                     <rdf:Description xmlns:exif="http://ns.adobe.com/exif/1.0/">                         <exif:GPSLatitude>51,28.927873651518N</exif:GPSLatitude>                         <exif:GPSLongitude>0,0E</exif:GPSLongitude>                     </rdf:Description>                 </rdf:RDF>             </x:xmpmeta>         </file>         <file Name="test.ps1" />         <file Name="test.txt" />         <file Name="test.zip" />         <file Name="tmp.xml" />     </folder> </root> and is quite large. Whether to leave the XMP RDF in the file, in sidecar files, or in DAXml, depends on your  'application/ps1 easy script', and how many files you have in your own folders. I guess the point is that you have easy access to your own data. P.S. I should have looked harder. ExifTool by Phil Harvey does all of this and more. I assumed from the name that it works on EXIF metadata but it can output all metadata as XMP i.e. PS> .\exiftool test.jpg -s -o .\%d%f.xmp will create sidecar files and doing something like this (less than obvious) [xml]$xmp=(.\exiftool -xmp -b -m $file.FullName) will produce the equivalent to the xml in the sidecar file. -X though will produce "RDF/Xml" which is less useful.
A different way to display the output from Get-DirAsXml is with Xaml. I will probably occasionally refer back to the previous posts on how to do it using Windows.Forms. So here is step one, displaying the output as a simple TreeView function Xaml-DisplayDir{    param([xml]$xml)    $xaml = @" <Window     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     Title="Xaml-DisplayDir"     Height="300" Width="600"     ResizeMode="CanResizeWithGrip"     WindowStyle="SingleBorderWindow">    <Window.Resources>        <XmlDataProvider x:Key="DirectoryData" XPath="/DAXContainer/*">            <x:XData>                <DAXContainer xmlns="">                    $($xml.SelectSingleNode("/*").OuterXml)                </DAXContainer>            </x:XData>        </XmlDataProvider>        <HierarchicalDataTemplate x:Key="FolderTemplate" ItemsSource="{Binding XPath=folder}">            <StackPanel Orientation="Horizontal">                <TextBlock Text="{Binding XPath=@Name}" Margin="2,0,0,0" />            </StackPanel>        </HierarchicalDataTemplate>        <DataTemplate x:Key="FileTemplate">            <StackPanel Orientation="Horizontal">                <TextBlock Text="{Binding XPath=@Name}" Margin="0,0,0,0" />            </StackPanel>        </DataTemplate>    </Window.Resources>    <Grid>        <TreeView Margin="0, 0, 0, 0" Name="TREEVIEW" HorizontalAlignment="Stretch" DataContext="{Binding Source={StaticResource DirectoryData}, XPath=/DAXContainer/*}" ItemTemplate="{StaticResource FolderTemplate}" ItemsSource="{Binding}" />    </Grid></Window>"@    $RESULT = ""    $WINDOW = [Windows.Markup.XamlReader]::Load((New-Object System.Xml.XmlNodeReader([xml]$xaml)))    $TREEVIEW = $WINDOW.FindName("TREEVIEW")        $TREEVIEW.add_SelectedItemChanged({            $RESULT = $this.Selecteditem.Name            write-host "SelectedItemChanged"    })    [void]$WINDOW.ShowDialog()    $WINDOW.Close()    $WINDOW.Dispose()    $RESULT} There are a few things going on here. The whole thing is a Powershell Mashup script. It declares a here-string that contains the Xaml markup. It inserts the Xml into the middle of the Xaml markup. (highlighted) It creates a Window object from the resulting markup. It adds a simple event handler to the TREEVIEW object. Displays the result. I like to think of it like this. Get-DirAsXml is used to get some Xml, the Xml is docorated or transformed (step 3 above. The Xml is wrapped in Xaml), the resulting Xml is displayed in a window. It can be used like this PS> . .\Xaml-DisplayDir.ps1 # dot source the scriptPS> Xaml-DisplayDir (Get-DirAsXml test) It might be displayed like this Here is the code Xaml-DisplayDir.zip (1.09 kb) So onto the next step. Add a ListView to display the files. This code will do it <ListView Name="LISTVIEW" Grid.Column="2" HorizontalAlignment="Stretch" ItemsSource="{Binding Path=SelectedItem, ElementName=TREEVIEW, Mode=OneWay}">    <ListView.View>        <GridView AllowsColumnReorder="true" ColumnHeaderContainerStyle="{DynamicResource CustomHeaderStyle}">            <GridViewColumn Header="Name" CellTemplate="{StaticResource ItemTemplate}" />            <GridViewColumn DisplayMemberBinding="{Binding XPath=@Length}" Header="Length" />            <GridViewColumn DisplayMemberBinding="{Binding XPath=@LastWriteTime}" Header="Date" />        </GridView>    </ListView.View></ListView> This adds a ListView with 3 columns Name, Length and Date. The columns will display the data specified in the XPath binding i.e. @Name, @Length, @LastWriteTime. We don't need to do anything like add an event handler to populate and display the items in the list because the ListView.ItemSource attribute is 'bound' to the TREEVIEW.SelectedItem. We also want to add icons for the folders and files. Rather than using an ImageList and indexing into it like Forms, Xaml is a bit more like html and uses markup to display an image and the text. Like this StackPanel <DataTemplate xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Key="ItemTemplate">    <StackPanel Orientation="Horizontal">        <Image Width="16" Height="16" Stretch="Fill" Source="ico2.png" />        <TextBlock Text="{Binding XPath=@Name}" Margin="0,0,0,0" />    </StackPanel></DataTemplate> It might be displayed like this You will notice that the folder test2 in the listview has the wrong icon. This can be fixed with a Trigger. <Style TargetType="Image">    <Style.Triggers>        <DataTrigger Binding="{Binding Path=Name}" Value="folder">            <Setter Property="Source" Value="{StaticResource FolderImage}" />        </DataTrigger>        <DataTrigger Binding="{Binding Path=Name}" Value="file">            <Setter Property="Source" Value="{StaticResource FileImage}" />        </DataTrigger>    </Style.Triggers></Style> I would like to bind the DataTrigger to XPath=local-name() but XPath functions are not supported DOH! To keep things self contained I have set the Image.Source property to rather badly drawn Xaml icons. It can be used like this PS> . .\Xaml-DisplayDirExp.ps1 # dot source the scriptPS> Xaml-DisplayDirExp (Get-DirAsXml test -props @{Length="";LastWriteTime=""} ) Here is the code Xaml-DisplayDirExp.zip (2.22 kb) Because Xaml is Xml we can process it with Xslt. We can add a little identity transform to our script that has an aditional template to hande the DAXContainer [xml]$xslt = @' <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">    <xsl:param name="DAX" />    <xsl:template match="node()|@*">        <xsl:copy>            <xsl:apply-templates select="@*|node()" />        </xsl:copy>    </xsl:template>    <xsl:template match="DAXContainer">        <DAXContainer>            <xsl:copy-of select="$DAX" />        </DAXContainer>    </xsl:template></xsl:stylesheet>'@[xml]$resultxaml = (Invoke-Transform -inxml $xaml -inxsl $xslt -arguments @{'DAX'=$xml}) This is a simple T-DaxDecorate Translet. (Notice that the single quote Here-String from Powershell is used for $xslt. This is to avoid Powershell's symbol substitution that would have translated  <xsl:copy-of select="$DAX" /> into invalid Xslt.) The Invoke-Transform is taking the Xaml as the input Xml and the motherload xml is passed as a parameter and copied directly to the output. Something like this It can be used like this PS> . .\Xaml-DisplayDirExpXslt.ps1 # dot source the scriptPS> Xaml-DisplayDirExp (Get-DirAsXml test -props @{Length="";LastWriteTime=""} ) Here is the code  Xaml-DisplayDirExpXslt.zip (1.99 kb) As in the Forms version we will want to specify the columns that are displayed. So far the Name, Length and LastWriteTime are hard coded in the Xaml Form but we can pass another Xml into the mix that specifies the columns we want. Something like this PS> Xaml-DisplayDirExp (Get-DirAsXml test -props @{Length=""; LastWriteTime=""} -ExtendedProps @{Title=""; Subject=""; Author=""; Category=""; Keywords=""; Comments=""}) -columns ([xml]"<columns><c n='Length' b='@Length' /><c n='Date' b='@LastWriteTime' /></columns>") The nice thing about this is that the column name and the columns binding path can be specified. [xml]$xslt = @' <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xaml="http://schemas.microsoft.com/winfx/2006/xaml/presentation" version="1.0">    <xsl:param name="COLUMNS" />    <xsl:template match="node()|@*">        <xsl:copy>            <xsl:apply-templates select="@*|node()" />        </xsl:copy>    </xsl:template>    <xsl:template match="xaml:ListView[@Name='LISTVIEW']/xaml:ListView.View/xaml:GridView">        <xsl:copy>            <xsl:apply-templates select="@*|node()" />            <xsl:apply-templates select="$COLUMNS/columns/c" />        </xsl:copy>    </xsl:template>    <xsl:template match="columns/c">        <xaml:GridViewColumn DisplayMemberBinding="{{Binding XPath={@b}}}" Header="{@n}" />    </xsl:template></xsl:stylesheet>'@[xml]$resultxaml = (Invoke-Transform -inxml $xaml -inxsl $xslt -arguments @{COLUMNS=$columns}) This is a simple T-DaxExplorerColumn Translet (Notice that the curly braces in the DisplayMemberBinding are escaped to avoid Xslt AVT substitution.) We can expand it to specify that we want column justification PS> Xaml-DisplayDirExp (Get-DirAsXml test -props @{Length=""; LastWriteTime=""} -ExtendedProps @{Title=""; Subject=""; Author=""; Category=""; Keywords=""; Comments=""}) -columns ([xml]"<columns><c n='Length' b='@Length' j='Right' /><c n='Date' b='@LastWriteTime' j='Right' /></columns>") [xml]$xslt = @' <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xaml="http://schemas.microsoft.com/winfx/2006/xaml/presentation" version="1.0">    <xsl:param name="COLUMNS" />    <xsl:template match="node()|@*">        <xsl:copy>            <xsl:apply-templates select="@*|node()" />        </xsl:copy>    </xsl:template>    <xsl:template match="xaml:ListView[@Name='LISTVIEW']/xaml:ListView.View/xaml:GridView">        <xsl:copy>            <xsl:apply-templates select="@*|node()" />            <xsl:apply-templates select="$COLUMNS/columns/c" />        </xsl:copy>    </xsl:template>    <xsl:template match="columns/c">        <xaml:GridViewColumn Header="{@n}">            <xaml:GridViewColumn.CellTemplate>                <xaml:DataTemplate>                    <xaml:TextBlock Text="{{Binding XPath={@b}}}"                             TextAlignment="{@j}" />                </xaml:DataTemplate>            </xaml:GridViewColumn.CellTemplate>        </xaml:GridViewColumn>    </xsl:template></xsl:stylesheet>'@[xml]$resultxaml = (Invoke-Transform -inxml $xaml -inxsl $xslt -arguments @{COLUMNS=$columns}) This is a simple T-DaxExplorerColumnWithJustify Translet For this to work properly we need to add a Style to the ListViewItem that will contain the TextBlock. It needs to stretch to fill the grid cell. This style can go into a Resources section that is in scope. I put it into the Window.Resources section. <Style TargetType="{x:Type ListViewItem}">    <Setter Property="HorizontalContentAlignment" Value="Stretch" /></Style> It might be displayed like this. Here is the code  Xaml-DisplayDirExpXsltColumns.zip (2.21 kb) Adding a ToolTip to items in the ListView is easy. We just have to add a ToolTip property to the ItemTemplate StackPanel and bind it to the InfoTip attribute. <StackPanel.ToolTip>    <TextBlock Text="{Binding XPath=@InfoTip}" /></StackPanel.ToolTip> Adding XPath searching seems simple at first. Add a Button and a TextBox to the last Row of the Grid <Grid Grid.Row="2" Grid.ColumnSpan="3" Background="Aquamarine">    <Grid.ColumnDefinitions>        <ColumnDefinition Width="100" />        <ColumnDefinition Width="*" />    </Grid.ColumnDefinitions>    <Button Name="SEARCHBUTTON" Content="Search" Width="100" />    <TextBox Name="SEARCHTEXT" Grid.Column="2" HorizontalAlignment="Stretch" Width="Auto" /></Grid> Then add a Click event to the Button and set the result of the XPath expression to the ItemsSource propert of the ListView. This works but it wrecks the binding of the ListView and further clicking on the TreeView doesn't do anything obviously. I tried a few things but in the end I decided on a 2 ListView approach. One is visible when the search button is clicked and the other when the treeview is clicked. It isn't perfect but it works until I can find a way of recreating the binding. It might display like this Here is the code  Xaml-DisplayDirExpXsltSearch.zip (2.75 kb) Now that we have another control on the form it is worth applying some styling. We can add say a gradient to the button. <Style TargetType="{x:Type Button}">    <Setter Property="Background">        <Setter.Value>            <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">                <GradientStop Color="#FF0000FF" Offset="0" />                <GradientStop Color="#FFFFFFFF" Offset="0.338" />                <GradientStop Color="#FFFFFFFF" Offset="0.716" />                <GradientStop Color="#FF0000FF" Offset="1" />            </LinearGradientBrush>        </Setter.Value>    </Setter></Style> Styles can get very big very quickly and a way of keeping them seperate is to include them like the columns and DAXml by doing a merge in Xslt   [xml]$xslt = @' <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xaml="http://schemas.microsoft.com/winfx/2006/xaml/presentation" version="1.0">    <xsl:param name="STYLE" />    <xsl:template match="node()|@*">        <xsl:copy>            <xsl:apply-templates select="@*|node()" />        </xsl:copy>    </xsl:template>    <xsl:template match="xaml:Window.Resources">        <xsl:copy>            <xsl:apply-templates select="@*|node()" />            <xsl:apply-templates select="$STYLE/*/*" />        </xsl:copy>    </xsl:template></xsl:stylesheet>'@[xml]$resultxaml = (Invoke-Transform -inxml $xaml -inxsl $xslt -arguments @{STYLE=$style}) This is a simple T-DaxExplorerStyler Translet It might be displayed like this or    Here is the code Xaml-DisplayDirExpXsltStyle.zip (2.81 kb)  Adding Drag'n'Drop is very similar to the Forms version except there is no DragStart event and the object model is slightly different but the code is very similar. I added a third pane like before that contains a TreeView. The drag rule is simple, items can be dragged from the ListView to the right-hand TreeView. The Template for the TreeView is modified to display folders and files. The resulting Xml is put into the pipeline when the form closes. It might be displayed like this (without cursor) Here is the code Xaml-DisplayDirExpDragDrop.zip (3.44 kb)
Displaying the output of Get-DirAsXml in an explorer with Powershell.  Although Powershell is a command line interface it can use forms to display output and get input. For an explorer the first thing we need is a treeview so we can use the System.Windows.Forms.TreeView control. Add it to a System.Windows.Forms.Form then recursively wander down the xml tree adding TreeNodes to it for all the nodes in the xml. Here is some code that will do that. .highlightbackground{ width: 100%; /*height: 100%;*/ overflow: auto; font-family: consolas, "Courier New", courier, mono; font-size: 12px; background-color: #eeeeee; color: #000000; padding: 2px 2px 2px 2px; white-space: nowrap; } .linenumberlist{border-left: solid 1px green;padding: 0px;} .linenumber{color: green;padding-left: 5px;} .foreground{color: #000000;} .command:color: #0000ff;} .commandparam{color: #000080;} .commandarg{color: #8a2be2;} .number{color: #800080;} .string{color: #8b0000;} .variable{color: #ff4500;} .type{color: #008080;} .operator{color: #a9a9a9;} .keyword{color: #00008b;} .comment{color: #006400;} .statementsep{color: #c86400;} .linecont{color: #c86400;} function Form-DisplayDir{    param ([xml]$XML)    function DisplayDir_wander($el, $tnode){        foreach($e in $el.get_ChildNodes()){ #|sort-object name){            $tn = new-object System.Windows.Forms.TreeNode            $tn.Text = $e.name            [Void]$tnode.Nodes.add( $tn )            if ($e.get_Name() -eq "folder"){                DisplayDir_wander ($e) ($tn)            }        }    }    $FORM = new-object Windows.Forms.Form        $FORM.Size = new-object System.Drawing.Size(300,310)        $FORM.text = "Form-DisplayDir"        $RESULT = ""    $TREEVIEW = new-object windows.forms.TreeView     $TREEVIEW.size = new-object System.Drawing.Size(295,269)       $TREEVIEW.Anchor = "top, left, bottom, right"    $TREEVIEW.add_AfterSelect({        $RESULT = $TREEVIEW.SelectedNode.FullPath    })            $r = $XML.selectSingleNode("/*")    $tn = new-object System.Windows.Forms.TreeNode     $tn.Text = $r.get_Name()                    DisplayDir_wander ($r) ($tn)            [void]$TREEVIEW.Nodes.Add($tn)    $FORM.Controls.Add($TREEVIEW)    [void]$FORM.showdialog()    $RESULT} You can use it like this PS> . .\Form-DisplayDir.ps1 # dot source the scriptPS> Form-DisplayDir (Get-DirAsXml test) It might be displayed like this.   PS> Form-DisplayDir (Get-DirAsXml test -props @{Length=""}) Will display this form with the file sizes in bytes shown.   The next thing to do is add an event handler to the TreeView. I like to use the AfterSelect event rather than the Click event because it doesn't fire when you expand or collapse nodes. $TREEVIEW.add_AfterSelect({    $RESULT = $TREEVIEW.SelectedNode.FullPath}) This bit of code can then be used as a simple folder/file selector. You could add the event to a Button and just stick $RESULT into the pipeline after the form closes. PS> Form-DisplayDir (Get-DirAsXml test) root\test\test2\test.txtor PS> Form-DisplayDir (Get-DirAsXml test) | SomeScriptThatNeedsAFilePath.ps1 Here is the code Form-DisplayDir.zip (621.00 bytes) The next thing to do is add a System.Windows.Forms.ListView control to display the files.         $LISTVIEW = new-object windows.forms.ListView    $LISTVIEW.Location = new-object System.Drawing.Size(300, 0)    $LISTVIEW.Size = new-object System.Drawing.Size(295,269)    $LISTVIEW.Anchor = "top, left, bottom, right"    $LISTVIEW.View = [System.Windows.Forms.View]::Details    $LISTVIEW.AllowColumnReorder = $true    [void]$LISTVIEW.Columns.Add("Name", -2, [windows.forms.HorizontalAlignment]::left)    foreach($att in $XML.SelectSingleNode("//file"            ).SelectNodes("@*[not(name()='Name')]")){        [void]$LISTVIEW.Columns.Add($att.get_Name(), -2, [windows.forms.HorizontalAlignment]::Right)    }    $TREEVIEW.add_AfterSelect({        $LISTVIEW.Items.Clear()        $xmlnode = $TREEVIEW.SelectedNode.Tag        foreach($child in $xmlnode.get_ChildNodes()){            $item = new-object windows.forms.ListViewItem($child.Name)            foreach($column in ($LISTVIEW.Columns|where{$_.Text -ne "Name"})){                if ($child.($column.Text) -ne $null){                    $item.SubItems.Add($child.($column.Text))                }            }            $LISTVIEW.Items.Add($item)        }    })    $FORM.Controls.Add($LISTVIEW) The TreeView AfterSelect event has been changed to populate the ListView. To make it look a bit more like Explorer you probably want to add a couple more columns to the xml PS> Form-DisplayDirExp (Get-DirAsXml test -props @{Length="";LastWriteTime=""}) It might be displayed like this   Here is the code Form-DisplayDirExp.zip (953.00 bytes) It is getting there but it is a bit confusing without icons, especially for the folders. There are a few different ways of doing this. You could create a System.Windows.Forms.ImageList object and add images to that then assign it to the $TREEVIEW.ImageList property but it gets quite labour intensive to make a lot of little icons. You could have just 2 icons, one for folders and one for files. So add some code like this $ImageList = New-Object System.Windows.Forms.ImageList $imageList.Images.Add([System.Drawing.Bitmap]::FromFile("folder.bmp"))$imageList.Images.Add([System.Drawing.Bitmap]::FromFile("file.bmp"))$TREEVIEW.ImageList = $ImageList $LISTVIEW.SmallImageList = $ImageList  It might be displayed like this   The best way to add icons is to use the SystemImageList or Shell Icon Cache. It is used everywhere and if you are using TreeView and ListView controls then one would think there would be a simple way of saying Hey! TreeView."UseTheEffingSystemImageList" But it is not that simple. You have to get down to the nitty gritty with System.Runtime.InteropServices SHGetFileInfo and SendMessage. So I wrapped all that 'c++ twitter' up in a file that you can just 'dot source' and then use the 2 methods (SetSystemImageListHandle, GetSystemImageListIndex) it contains. int SetSystemImageListHandle(Control, int Size) sets the ImageList for the TreeView/ListView control to the SystemImageList and the Size to large or small (0, 1). int GetSystemImageListIndex(string Filename, boolean Dir, int Size) gets the image index for the filename. Dir is a boolean indicating if it is a Folder and Size is large or small (0, 1). ImageListHandle.zip (2.19 kb) Here are some code snippets. . .\ImageListHandle.ps1$SHGFI_SMALLICON = 1; $SHGFI_LARGEICON = 0$LISTVIEW = new-object System.Windows.Forms.ListView$FORM.Controls.Add($LISTVIEW)[void][cjb.Shell32]::SetSystemImageListHandle($LISTVIEW, $SHGFI_SMALLICON)$TREEVIEW = new-object System.Windows.Forms.TreeView$FORM.Controls.Add($TREEVIEW)[void][cjb.Shell32]::SetSystemImageListHandle($TREEVIEW, $SHGFI_SMALLICON)$tn = new-object System.Windows.Forms.TreeNode$tn.ImageIndex = $tn.SelectedImageIndex = [cjb.Shell32]::GetSystemImageListIndex($e.Name, ($e.get_Name() -eq "folder"), $SHGFI_SMALLICON)$idx = [cjb.Shell32]::GetSystemImageListIndex($n.Name, ($n.get_Name() -eq "folder"), $SHGFI_SMALLICON)$item = new-object windows.forms.ListViewItem($n.Name,$idx) For this to work properly the control must be added to the form before SetSystemImageListHandle is called. Here is the version that uses the SystemImageList Form-DisplayDirExpIcons.zip (1.09 kb) It might be displayed like this A useful thing to set on the ListView control is AutoResizeColumns. This can have one of two values, HeaderSize or ColumnContent and resizes the columns depending on the width of the text. $LISTVIEW.AutoResizeColumns([Windows.Forms.ColumnHeaderAutoResizeStyle]::HeaderSize) Another is AllowColumnReorder. This allows you to move the columns around as you like $LISTVIEW.AllowColumnReorder = $true With all of the attributes that you can have in the xml it might be handy to specify which columns you want displayed in the ListView. This can be done by passing an array of attribute names that will be displayed as columns. Here Get-DirAsXml adds 9 attributes (Name + 2props + 6extendedProps) PS> Form-DisplayDirExp (Get-DirAsXml test -props @{Length="";LastWriteTime=""} -ExtendedProps @{Title=""; Subject=""; Author=""; Category=""; Keywords=""; Comments=""}) -columns @("Name", "Length", "LastWriteTime") So instead of displaying all 9 file properties it will only display Name, Length and LastWriteTime in that order. But supposing you DO want to see the other file properties? You could add a third pane to the Explorer     $LISTVIEW2 = new-object windows.forms.ListView    $FORM.Controls.Add($LISTVIEW2)    $LISTVIEW2.Location = new-object System.Drawing.Size(600, 0)    $LISTVIEW2.Size = new-object System.Drawing.Size(295,269)    $LISTVIEW2.Anchor = "top, bottom, right"    $LISTVIEW2.View = [System.Windows.Forms.View]::Details    [void]$LISTVIEW2.Columns.Add("Name", -2, [windows.forms.HorizontalAlignment]::Left)    [void]$LISTVIEW2.Columns.Add("Value", -2, [windows.forms.HorizontalAlignment]::Right)    $LISTVIEW.add_ItemActivate({        $LISTVIEW2.Items.Clear()        $xmlnode = $LISTVIEW.SelectedItems[0].Tag        foreach($att in $xmlnode.Attributes){            $item = new-object windows.forms.ListViewItem($att.get_Name())            $item.SubItems.Add($att.Value)            $LISTVIEW2.Items.Add($item)        }    })   It might be displayed like this  Here is the code Form-DisplayDirExpIcons3Pane.zip (1.27 kb) You could set the ToolTipText on the ListViewItems. First set ShowItemToolTips to true. This must be done before you set the SystemImageList handle if you are using icons. $LISTVIEW.ShowItemToolTips = $true Then build up your own ToolTipText or if you specified -ExtendedProps @{InfoTip=""} on Get-DirAsXml then you can use it as you populate the ListView control $item.ToolTipText = $child.InfoTip This might be displayed like this (with cursor) Adding a context menu to the ListView is quite easy.     $LISTVIEW.ContextMenu = $cm = new-object Windows.Forms.ContextMenu    $mi = $cm.MenuItems.Add("Do Thing One")    $mi.add_Click({        $XMLNODE = $LISTVIEWNODE.Tag        Write-Host "Thing One invoked on $LISTVIEWNODE."    })    $mi = $cm.MenuItems.Add("Do Thing Two")    $mi.add_Click({        $XMLNODE = $LISTVIEWNODE.Tag        Write-Host "Thing Two invoked on $LISTVIEWNODE."    })    $LISTVIEW.add_MouseDown({        if ($_.Button -eq [Windows.Forms.MouseButtons]::Right){            $LISTVIEWNODE = $LISTVIEW.GetItemAt($_.X, $_.Y)        }else{            $LISTVIEWNODE = $null        }    }) This might be displayed like this On the Host console you will see Here is the code Form-DisplayDirExpMenu.zip (1.29 kb) With all those ExtendedProperties you can get from Get-DirAsXml a search functionality is mighty useful. Here is some code to add an XPath search to the explorer. Any valid XPath can be used such as //file[contains(@Author, 'Chr')]  or //file[contains(@BitRate, '44')]. A find duplicate file names: //file[@Name = preceding::file/@Name]. See this previous entry for how to add a Checksum attribute then find duplicate files //file[@Checksum = preceding::file/@Checksum].         $SEARCHBOX = new-object windows.forms.TextBox    $SEARCHBOX.Location = new-object System.Drawing.Point(85, 275)    $SEARCHBOX.Size = new-object System.Drawing.Size(490,20)    $SEARCHBOX.Anchor = "left, bottom, right"    $FORM.Controls.Add($SEARCHBOX)    $BUTTON = new-object windows.forms.button    $BUTTON.Location = new-object System.Drawing.Point(10, 275)    $BUTTON.Anchor = "left, bottom"    $BUTTON.Text = "Search"    $BUTTON.add_Click({        $LISTVIEW.Items.Clear()        $nodes = $XML.SelectNodes($SEARCHBOX.Text)        foreach($child in $nodes){            $idx = [cjb.Shell32]::GetSystemImageListIndex($child.Name, ($child.get_Name() -eq "folder"), $SHGFI_SMALLICON)            $item = new-object windows.forms.ListViewItem($child.Name, $idx)            $item.Tag = $child            $item.ToolTipText = $child.InfoTip            foreach($column in ($LISTVIEW.Columns|where{$_.Text -ne "Name"})){                if ($child.($column.Text) -ne $null){                    $item.SubItems.Add($child.($column.Text))                }else{                    $item.SubItems.Add("")                }            }            $LISTVIEW.Items.Add($item)        }        $LISTVIEW.AutoResizeColumns([Windows.Forms.ColumnHeaderAutoResizeStyle]::ColumnContent)    })    $FORM.Controls.Add($BUTTON)    It might be displayed like this (with cursor) Here is the code Form-DisplayDirExpSearch.zip (1.26 kb) Another thing you might want to do is drag'n'drop. I will go back to the 3 pane solution but with a ListView in the middle and a TreeView on either side. The drag'n'drop rule is simple. Things can be dragged only from the ListView to the right side TreeView.         $LISTVIEW.add_ItemDrag({        param($sender, $e)        $nodes = @()        foreach($lvi in $sender.SelectedItems){            $nodes += $lvi.Tag        }        $sender.DoDragDrop($nodes, [System.Windows.Forms.DragDropEffects]::Link)    })        $TREEVIEW2.add_DragDrop({        param($sender, $e)        $point = new-object System.Drawing.Point -ArgumentList @($e.X, $e.Y)        $targetPoint = $sender.PointToClient($point)        $dropnode = $sender.GetNodeAt($targetPoint)        if ($dropnode.Tag.get_Name() -eq "folder"){            $nodes = $e.Data.GetData("System.Object[]")            foreach($n in $nodes){                $newnode = $XML2.ImportNode($n, $true)                $dropnode.Tag.AppendChild($newnode)            }            $dropnode.Nodes.Clear()            DisplayDir_wander ($dropnode.Tag) ($dropnode) ($true)        }    }) It might be displayed like this (without cursor)   So imagine that the left is a list of all files in a tree, on the right are groups and topics in a knowledgebase. It might be a movie or mp3 library with moods or genres. It needs human interaction to decide how the resulting xml is layed out and what it contains. Here is a possibility, it takes the usual as input plus another xml that it creates on the fly. The output when the form is closed is sent to the T-Dax2Wpl translet to create a Windows Media Player playlist. Or it might be an inventory list for a software release and all the resulting files are copied to an output media. PS> Form-DisplayDirExp (Get-DirAsXml test -props @{Length="";LastWriteTime=""} -ExtendedProps @{Title="";Subject=""; Author="";Category="";Keywords=""; Comments=""}) -columns @("Name", "Length", "LastWriteTime") -xml2 ([xml]'<root><folder Name="Knowledge"><folder Name="Topic" /><folder Name="Group" /></folder></root>') | T-Dax2Wpl Here is the code Form-DisplayDirExpDragDrop.zip (1.72 kb)
Here is a  translet that will transform Timed Text (TT) Authoring Format 1.0  to Microsoft Synchronized Accessible Media Interchange (SAMI). This one allows you to specify a TimeShift value in milliseconds to the transform. It also does better subtitle colouring. function T-TtafToSmi{    param (  [xml]$inxml            ,[int]$TimeShift = 0                )    BEGIN {        . pslib:\xml\invoke-transform.ps1        [xml]$xslt = @' <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"xmlns:t="http://www.w3.org/2006/10/ttaf1"xmlns="http://www.w3.org/2006/10/ttaf1"xmlns:ttp="http://www.w3.org/2006/10/ttaf1#parameter"xmlns:tts="http://www.w3.org/2006/10/ttaf1#style"xmlns:ttm="http://www.w3.org/2006/10/ttaf1#metadata"xmlns:msxsl="urn:schemas-microsoft-com:xslt"xmlns:cjb="http://www.bayes.co.uk/script" version="1.0" exclude-result-prefixes="#default t ttp tts ttm msxsl cjb">    <xsl:param name="TimeShift" select="0" />    <xsl:output encoding="utf-8" indent="yes" method="xml" omit-xml-declaration="yes" />    <msxsl:script language="C#" implements-prefix="cjb">        public double getMilliseconds(string time)        {        return (string.IsNullOrEmpty(time) ? 0 : TimeSpan.Parse(time).TotalMilliseconds);        }        public string hyphenate(string str){        Text.StringBuilder sb = new Text.StringBuilder("", 100);        foreach (char c in str){        if (char.IsUpper(c)){sb.Append("-");sb.Append(char.ToLower(c));}        else{sb.Append(c);}}        return sb.ToString();        }    </msxsl:script>    <xsl:variable name="default-style">        <xsl:for-each select="/t:tt/t:head/t:styling/t:style[last()]">            <xsl:sort order="ascending" select="count(@tts:*)" />            <xsl:value-of select="@id" />        </xsl:for-each>    </xsl:variable>    <xsl:variable name="default-class">        <xsl:value-of select="/t:tt/@xml:lang" />cc    </xsl:variable>    <xsl:template match="t:tt">        <SAMI>            <HEAD>                <TITLE>                    <xsl:value-of select="/t:tt/t:head/t:metadata/ttm:title" />                </TITLE>                <xsl:comment>                    Copyright <xsl:value-of select="/t:tt/t:head/t:metadata/ttm:copyright" />                </xsl:comment>                <xsl:copy-of select="/t:tt/t:head/comment()" />                <STYLE>                    <xsl:comment>                        p {                        text-align: center;                        font-family: arial, sans-serif;                        font-weight: normal;                        color: white;                        font-size: 16pt;                        <xsl:apply-templates select="/t:tt/t:head/t:styling/t:style[@id=$default-style]/@tts:*" />                        }                        .<xsl:value-of select="$default-class" /> {                        text-align: center;                        font-family: arial, sans-serif;                        font-weight: normal;                        color: white;                        font-size: 16pt;                        <xsl:apply-templates select="/t:tt/t:head/t:styling/t:style[@id=$default-style]/@tts:*" />                        Name: <xsl:value-of select="/t:tt/@xml:lang" /> Subtitles;                        lang: <xsl:value-of select="/t:tt/@xml:lang" />;                        SAMI_Type: CC;                        }                    </xsl:comment>                </STYLE>            </HEAD>            <xsl:apply-templates select="/t:tt/t:body" />        </SAMI>    </xsl:template>    <xsl:template match="t:body">        <BODY>            <xsl:apply-templates />        </BODY>    </xsl:template>    <xsl:template match="t:body/t:div">        <xsl:apply-templates />    </xsl:template>    <xsl:template match="t:p">        <xsl:variable name="start" select="cjb:getMilliseconds(string(@begin)) + $TimeShift" />        <xsl:variable name="end" select="cjb:getMilliseconds(string(@end)) + $TimeShift" />        <xsl:variable name="following-start" select="cjb:getMilliseconds(string(following-sibling::t:p/@begin)) + $TimeShift" />        <Sync START="{$start}">            <p>                <xsl:if test="@style">                    <xsl:attribute name="class">                        <xsl:value-of select="$default-class" />                    </xsl:attribute>                </xsl:if>                <span>                    <xsl:if test="@style=$default-style and @tts:*">                        <span>                            <xsl:attribute name="style">                                <xsl:apply-templates select="@tts:*" />                            </xsl:attribute>                        </span>                    </xsl:if>                    <xsl:if test="@style!=$default-style">                        <xsl:attribute name="style">                            <xsl:apply-templates select="/t:tt/t:head/t:styling/t:style[@id=current()/@style]/@tts:*|@tts:*" />                        </xsl:attribute>                    </xsl:if>                    <xsl:apply-templates />                </span>            </p>        </Sync>        <xsl:if test="$end &lt; ($following-start - 10)">            <xsl:text>      </xsl:text>            <Sync START="{$end}">                <p>                    <nonbreakingspace />                </p>            </Sync>        </xsl:if>    </xsl:template>    <xsl:template match="@tts:*">        <xsl:value-of select="cjb:hyphenate(local-name())" />:<xsl:value-of select="." />;    </xsl:template>    <xsl:template match="@tts:fontFamily">        <xsl:value-of select="cjb:hyphenate(local-name())" />:<xsl:value-of select="cjb:hyphenate(.)" />;    </xsl:template>    <xsl:template match="@tts:fontSize">        <xsl:value-of select="cjb:hyphenate(local-name())" />:<xsl:value-of select="." /> pt;    </xsl:template>    <xsl:template match="*">        <xsl:element name="{local-name(.)}">            <xsl:if test="@tts:*">                <xsl:attribute name="style">                    <xsl:apply-templates select="@tts:*" />                </xsl:attribute>            </xsl:if>            <xsl:apply-templates select="@*[namespace-uri()!='http://www.w3.org/2006/10/ttaf1#style']" />            <xsl:apply-templates />        </xsl:element>    </xsl:template>    <xsl:template match="text()">        <xsl:copy />    </xsl:template>    <xsl:template match="@*">        <xsl:attribute name="{local-name()}">            <xsl:value-of select="." />        </xsl:attribute>    </xsl:template></xsl:stylesheet>'@        }    PROCESS{        if ($_ -is [xml]){            $result = (invoke-transform -inxml $_ -inxsl $xslt -arguments @{TimeShift=$TimeShift})            $result = $result.replace(" xmlns=`"http://www.w3.org/2006/10/ttaf1`"", "")            $result = $result.replace("<nonbreakingspace />", "&nbsp;")            $result        }    END{        if ($inxml -is [xml]){            $result = (invoke-transform -inxml $inxml -inxsl $xslt -arguments @{TimeShift=$TimeShift})            $result = $result.replace(" xmlns=`"http://www.w3.org/2006/10/ttaf1`"", "")            $result = $result.replace("<nonbreakingspace />", "&nbsp;")            $result        }    }} The replaces on the $result string look like a bit of a hack but they do need to be there for reasons that are very long to explain. It can be used like this PS> . .\T-TtafToSmiM3.ps1 # dot source this filePS> T-TtafToSmi [xml](gc ttaf.xml) -T 1000| sc -encoding ascii or PS> . .\T-TtafToSmiM3.ps1 # dot source this filePS> [xml](gc .\ttaf.xml) | T-TtafToSmi -T 1000 | sc -encoding ascii The -T 1000 will shift the subtitle forward by 1 second. A value of -T -1000 will shift the subtitle backward by 1 second. Here is the code  T-TtafToSmiM3.zip (2.50 kb)
Here is a  translet that will transform Timed Text (TT) Authoring Format 1.0  to Microsoft Synchronized Accessible Media Interchange (SAMI). This one is quite simple. All text is the same colour. Not much different from SRT format really. function T-TtafToSmi{    param ($inxml)    BEGIN {        . pslib:\xml\invoke-transform.ps1       [xml]$xslt = @' <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"xmlns:t="http://www.w3.org/2006/10/ttaf1"xmlns="http://www.w3.org/2006/10/ttaf1"xmlns:ttp="http://www.w3.org/2006/10/ttaf1#parameter"xmlns:tts="http://www.w3.org/2006/10/ttaf1#style"xmlns:ttm="http://www.w3.org/2006/10/ttaf1#metadata"xmlns:msxsl="urn:schemas-microsoft-com:xslt"xmlns:cjb="http://www.bayes.co.uk/script"version="1.0"exclude-result-prefixes="#default t ttp tts ttm msxsl cjb">    <xsl:output encoding="utf-8" indent="yes" method="xml" omit-xml-declaration="yes" />    <msxsl:script language="C#" implements-prefix="cjb">        public string getMilliseconds(string time)        {        return (string.IsNullOrEmpty(time) ? "" : TimeSpan.Parse(time).TotalMilliseconds.ToString());        }        public string hyphenate(string str){        Text.StringBuilder sb = new Text.StringBuilder("", 100);        foreach (char c in str){        if (char.IsUpper(c)){sb.Append("-");sb.Append(char.ToLower(c));}        else{sb.Append(c);}}        return sb.ToString();        }    </msxsl:script>    <xsl:variable name="default-style">        <xsl:for-each select="/t:tt/t:head/t:styling/t:style[last()]">            <xsl:sort order="ascending" select="count(@tts:*)" />            <xsl:value-of select="@id" />        </xsl:for-each>    </xsl:variable>    <xsl:variable name="default-class">        <xsl:value-of select="/t:tt/@xml:lang" />cc    </xsl:variable>    <xsl:template match="t:tt">        <SAMI>            <HEAD>                <TITLE>                    <xsl:value-of select="/t:tt/t:head/t:metadata/ttm:title" />                </TITLE>                <xsl:comment>                    Copyright <xsl:value-of select="/t:tt/t:head/t:metadata/ttm:copyright" />                </xsl:comment>                <xsl:copy-of select="/t:tt/t:head/comment()" />                <STYLE>                    <xsl:comment>                        .<xsl:value-of select="$default-class" /> {                        text-align: center;                        font-family: arial, sans-serif;                        font-weight: normal;                        color: white;                        font-size: 16pt;                        Name: <xsl:value-of select="/t:tt/@xml:lang" /> Subtitles;                        lang: <xsl:value-of select="/t:tt/@xml:lang" />;                        SAMI_Type: CC;                        /* anything below will override above */                        <xsl:apply-templates select="/t:tt/t:head/t:styling/t:style[@id=$default-style]/@tts:*" />                        }                    </xsl:comment>                </STYLE>            </HEAD>            <xsl:apply-templates select="/t:tt/t:body" />        </SAMI>    </xsl:template>    <xsl:template match="t:body">        <BODY>            <xsl:apply-templates />        </BODY>    </xsl:template>    <xsl:template match="t:body/t:div">        <xsl:apply-templates />    </xsl:template>    <xsl:template match="t:p">        <xsl:variable name="start" select="cjb:getMilliseconds(string(@begin))" />        <xsl:variable name="end" select="cjb:getMilliseconds(string(@end))" />        <xsl:variable name="following-start" select="cjb:getMilliseconds(string(following-sibling::t:p/@begin))" />        <Sync START="{$start}">            <p class="{$default-class}">                <xsl:if test="@style">                    <xsl:attribute name="class">                        <xsl:value-of select="$default-class" />                    </xsl:attribute>                </xsl:if>                <div>                    <xsl:if test="@style=$default-style and @tts:*">                        <xsl:attribute name="style">                            <xsl:apply-templates select="@tts:*" />                        </xsl:attribute>                    </xsl:if>                    <xsl:if test="@style!=$default-style">                        <xsl:attribute name="style">                            <xsl:apply-templates select="/t:tt/t:head/t:styling/t:style[@id=current()/@style]/@tts:*|@tts:*" />                        </xsl:attribute>                    </xsl:if>                    <xsl:apply-templates />                </div>            </p>        </Sync>        <xsl:if test="$end &lt; ($following-start - 10)">            <xsl:text>      </xsl:text>            <Sync START="{$end}">                <p>                    <nonbreakingspace />                </p>            </Sync>        </xsl:if>    </xsl:template>    <xsl:template match="@tts:*">        <xsl:value-of select="cjb:hyphenate(local-name())" />:<xsl:value-of select="." />;    </xsl:template>    <xsl:template match="*">        <xsl:element name="{local-name(.)}">            <xsl:if test="@tts:*">                <xsl:attribute name="style">                    <xsl:apply-templates select="@tts:*" />                </xsl:attribute>            </xsl:if>            <xsl:apply-templates select="@*[namespace-uri()!='http://www.w3.org/2006/10/ttaf1#style']" />            <xsl:apply-templates />        </xsl:element>    </xsl:template>    <xsl:template match="text()">        <xsl:copy />    </xsl:template>    <xsl:template match="@*">        <xsl:attribute name="{local-name()}">            <xsl:value-of select="." />        </xsl:attribute>    </xsl:template></xsl:stylesheet>'@    }    PROCESS{        if ($_ -is [xml]){            $result = (invoke-transform -inxml $_ -inxsl $xslt)            $result = $result.replace(" xmlns=`"http://www.w3.org/2006/10/ttaf1`"", "")            $result = $result.replace("<nonbreakingspace />", "&nbsp;")            $result        }        }    END{        if ($inxml -is [xml]){            $result = (invoke-transform -inxml $inxml -inxsl $xslt)            $result = $result.replace(" xmlns=`"http://www.w3.org/2006/10/ttaf1`"", "")            $result = $result.replace("<nonbreakingspace />", "&nbsp;")            $result        }    }} The replaces on the $result string look like a bit of a hack but they do need to be there. It can be used like this PS> . .\T-TtafToSmiM1.ps1 # dot source this filePS> T-TtafToSmi [xml](gc ttaf.xml) | sc -encoding ascii or PS> . .\T-TtafToSmiM1.ps1 # dot source this filePS> [xml](gc .\ttaf.xml) | T-TtafToSmi | sc -encoding ascii Here is the code T-TtafToSmiM1.zip (2.39 kb)
Here is a translet that will transform Timed Text (TT) Authoring Format 1.0  to SRT. function T-TtafToSrt{    param ($inxml)    BEGIN {        . pslib:\xml\invoke-transform.ps1        $xslt = New-Object Xml.Xmldocument        $xslt.PSBase.PreserveWhitespace = $true        $xslt.LoadXml(@' <xsl:stylesheetxmlns:xsl="http://www.w3.org/1999/XSL/Transform"xmlns:t="http://www.w3.org/2006/10/ttaf1"xmlns="http://www.w3.org/2006/10/ttaf1"xmlns:ttp="http://www.w3.org/2006/10/ttaf1#parameter"xmlns:tts="http://www.w3.org/2006/10/ttaf1#style"xmlns:ttm="http://www.w3.org/2006/10/ttaf1#metadata"version="1.0"><xsl:output indent="yes" method="text" /><xsl:template match="/t:tt"><xsl:apply-templates select="/t:tt/t:body" /></xsl:template><xsl:template match="t:div"><xsl:apply-templates select="t:p" /></xsl:template><xsl:template match="t:p"><xsl:text></xsl:text><xsl:value-of select="count(preceding-sibling::t:p)" /><xsl:text></xsl:text><xsl:value-of select="concat(substring-before(@begin, '.'), ',', substring-after(@begin, '.'))" />0 --&gt; <xsl:value-of select="concat(substring-before(@begin, '.'), ',', substring-after(@begin, '.'))" />0<xsl:text></xsl:text><xsl:apply-templates /></xsl:template><xsl:template match="t:br"><xsl:text></xsl:text></xsl:template><xsl:template match="node()|@*"><xsl:copy><xsl:apply-templates select="@*|node()" /></xsl:copy></xsl:template></xsl:stylesheet>'@)        }    PROCESS{        if ($_ -is [xml]){            invoke-transform -inxml $_ -inxsl $xslt        }        }    END{        if ($xml -is [xml]){           invoke-transform -inxml $xml -inxsl $xslt        }    }} It can be used like this PS> . .\T-TtafToSrt.ps1 # dot source this filePS> T-TtafToSrt [xml(gc .\ttaf.xml) | sc -encoding ascii or PS> . .\T-TtafToSrt.ps1 # dot source this filePS> [xml](gc .\ttaf.xml) | T-TtafToSrt | sc -encoding ascii Note that if you aren't passing it on down the pipeline and are sending it to a file you have to set the encoding to ascii or applications will ignore it. Here is the code. T-TtafToSrt.zip (1.10 kb)
One would think this should be easy. Just put Powershell into the Language of an msxsl:script element. But it doesn't work :-D Pity.  I rooted around and the language attribute can be any of the CodeDomProviders available on the system. PS> [System.CodeDom.Compiler.CodeDomProvider]::GetAllCompilerInfo()CodeDomProviderType IsCodeDomProviderTypeValid------------------- --------------------------Microsoft.CSharp.CSharpCodeProvider TrueMicrosoft.VisualBasic.VBCodeProvider TrueMicrosoft.JScript.JScriptCodeProvider TrueMicrosoft.VisualC.CppCodeProvider True According to thisthere won't be one for Powershell soon. Never mind, I still want to use Powershell from Xslt. So I tried this. function T-AddChecksum{    param ($inxml)    begin{       . pslib:\xml\invoke-transform.ps1       [xml]$xslt = @'<?xml version="1.0" encoding="utf-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:cjb="cjb">  <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="@Base" /><xsl:text>\</xsl:text><xsl:value-of select="@Name" />    </xsl:for-each>  </xsl:template></xsl:stylesheet>'@$func = @'    function GetChecksum([string]$file){        $stream = [System.IO.File]::OpenRead($file)        $sha256 = new-object System.Security.Cryptography.SHA256Managed        $checksum = $sha256.ComputeHash($stream)        [System.BitConverter]::ToString($checksum).Replace("-", [system.String]::Empty)    }'@       $pso = new-object psobject       $pso = add-member -inputobject $pso -membertype scriptmethod -name GetChecksum -value {$func} -PassThru    }    process{        if ($_ -is [xml]){            [xml](invoke-transform -inxml $_ -inxsl $xslt -extensionobjects @{"cjb"=$pso})        }    }    end{        if ($inxml -is [xml]){            [xml](invoke-transform -inxml $inxml -inxsl $xslt -extensionobjects @{"cjb"=$pso})        }    }} But that doesn't work. It is so long since I wrote it I can't remember if it worked, worked under version 1.0, worked in the CTP, seemed like a good idea that should work but didn't or it didn't work at all. Anyway it still looks like a good idea to me. This is the object created with C# in the previous example. PS > $cs|gmTypeName: ChecksumName MemberType Definition ---- ---------- ---------- Equals Method bool Equals(System.Object obj) GetChecksum Method string GetChecksum(string file)GetHashCode Method int GetHashCode() GetType Method type GetType() ToString Method string ToString() This is the Powershell object created with PSObject PS D:\powershell\temp> $pso|gmTypeName: System.Management.Automation.PSCustomObjectName MemberType Definition ---- ---------- ---------- Equals Method bool Equals(System.Object obj)GetHashCode Method int GetHashCode() GetType Method type GetType() ToString Method string ToString() GetChecksum ScriptMethod System.Object GetChecksum(); So it is not a valid Xslt extension object? Right, so we can call C# from Xslt via an extension object and we can call Powershell from C# so all we have to do is write a C# wrapper around the Powershell script and we can call Powershell from Xslt. function T-AddChecksum{    param ($inxml)    begin{        . pslib:\xml\invoke-transform.ps1        [xml]$xslt = @'<?xml version="1.0" encoding="utf-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:cjb="cjb">  <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 = @'using System;using System.Management.Automation;public partial class PSCaller{    ScriptBlock _script;    public PSCaller(ScriptBlock script) { Script = script; }    public ScriptBlock Script    {        get { return _script; }        set { _script = value; }    }    public String GetChecksum(String file)    {        try {            return (string)Script.Invoke(file)[0].BaseObject;        } catch(Exception ex) {            throw new InvalidOperationException("Script failed***!", ex);        }    }}'@       Add-Type -TypeDefinition $code       $psco = new-object PSCaller({            $file = $args[0]            $stream = [System.IO.File]::OpenRead($file)            $sha256 = new-object System.Security.Cryptography.SHA256Managed            $checksum = $sha256.ComputeHash($stream)            [System.BitConverter]::ToString($checksum).Replace("-", [system.String]::Empty)       })    }    process{        [xml](invoke-transform -inxml $_ -inxsl $xslt -extensionobjects @{"cjb"=$psco})    }    end{        [xml](invoke-transform -inxml $inxml -inxsl $xslt -extensionobjects @{"cjb"=$psco})    }} This works but it is a bit clunky. The name and signature 'String GetChecksum(String file)' is in the C# script and is seperate from the actual code that is in the Powershell script. A right mashup :-) What we need is a way of describing the signature and Powershell script in one place. I started looking at DSLs. Here is a simple class/method DSL $dsl = {    psclass public PSCaller{        method String GetChecksum([String]){            $file = $args[0]            $stream = [System.IO.File]::OpenRead($file)            $sha256 = new-object System.Security.Cryptography.SHA256Managed            $checksum = $sha256.ComputeHash($stream)            [System.BitConverter]::ToString($checksum).Replace("-", [system.String]::Empty)        }    }} I was using 'class' as the class keyword but it is now a Powershell reserved word so I am using 'psclass'. It can handle multiple methods with multiple arguments and the arguments can be any type i.e. [System.Xml.XmlElement]. This example code defines 1 method with the signature 'String GetChecksum([String])' followed by the script itself. It produces the following C# using System; using System.Collections; using System.Management.Automation; using System.Xml; public class PSCaller { ScriptBlock script0; public PSCaller(ScriptBlock Script0) { script0 = Script0; } public String GetChecksum(string p0) { ScriptBlock Script = script0; try { return (String)Script.Invoke(p0)[0].BaseObject; } catch (Exception ex) { throw new InvalidOperationException("Script failed***!", ex); } } } and an array of ScriptBlocks. It then compiles the code and passes the array of ScriptBlocks to the new object constructor. [string]$code = &$dslAdd-Type -TypeDefinition $code -ReferencedAssemblies System.xml$NewObject = new-object PSCaller($scriptArray) This is what the object looks like. PS > $newobject|gmTypeName: PSCaller2Name MemberType Definition ---- ---------- ---------- Equals Method bool Equals(System.Object obj) GetChecksum Method string GetChecksum(string p0) GetDifferentChecksum Method string GetDifferentChecksum(string p0)GetHashCode Method int GetHashCode() GetType Method type GetType() ToString Method string ToString() So this is what a Translet might look like function T-AddChecksum{    param ($inxml)    begin{       . pslib:\xml\invoke-transform.ps1       . pslib:\xml\Create-ObjectFromDSL.ps1        [xml]$xslt = @'<?xml version="1.0" encoding="utf-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:cjb="cjb">  <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>'@                $dsl = {            psclass public PSCaller{                method String GetChecksum([String]){                    $file = $args[0]                    $stream = [System.IO.File]::OpenRead($file)                    $sha256 = new-object System.Security.Cryptography.SHA256Managed                    $checksum = $sha256.ComputeHash($stream)                    [System.BitConverter]::ToString($checksum).Replace("-", [system.String]::Empty)                }                method String GetDifferentChecksum([String]){                    $file = $args[0] #different                    $stream = [System.IO.File]::OpenRead($file)                    $sha256 = new-object System.Security.Cryptography.SHA256Managed                    $checksum = $sha256.ComputeHash($stream)                    [System.BitConverter]::ToString($checksum).Replace("-", [system.String]::Empty)                }            }        }        $psco = Create-ObjectFromDSL $dsl    }    process{        if ($_ -is [xml]){            [xml](invoke-transform -inxml $_ -inxsl $xslt -extensionobjects @{"cjb"=$psco})        }    }    end{        if ($inxml -is [xml]){            [xml](invoke-transform -inxml $inxml -inxsl $xslt -extensionobjects @{"cjb"=$psco})        }    }} To use it you need the Create-ObjectFromDSL.ps1 script. Here it is Create-ObjectFromDSL.zip (953.00 bytes) Why you would want to do this and what you would use it for is up to you. Most people would use Java, JavaScript or C# to extend Xslt. In most cases I would generate Xml from the things that Powershell can do well and pass that into a transform. But this just shows that is is partly possible to use Powershell as an Xslt scripting language and it adds another arrow to your quiver. Since we have to do the Create-ObjectFromDSL step anyway we could change the Xslt to something like this <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"xmlns:cjbxsl="urn:schemas-bayes-co-uk:xslt"xmlns:ucjb="urn:schemas-bayes-co-uk:extension-object" version="1.0">    <cjbxsl:script language="Powershell" implements-prefix="ucjb">        <cjbxsl:method name="GetChecksum" returns="string" args="string">            <![CDATA[                $file = $args[0]                $stream = [System.IO.File]::OpenRead($file)                $sha256 = new-object System.Security.Cryptography.SHA256Managed                $checksum = $sha256.ComputeHash($stream)                [System.BitConverter]::ToString($checksum).Replace("-", [system.String]::Empty)            ]]>        </cjbxsl:method>    </cjbxsl: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> then we can pass the stylesheet through another translet to create the C# and the extension object :-D. The output from that translet would be the extension object that is actually passed as the extension object to the original stylesheet and used in the pipeline. Oh dizzy! function T-CreateObjectFromPSScriptXslt{    param ($inxml)    begin{        . pslib:\xml\invoke-transform.ps1        $xslt = New-Object Xml.Xmldocument        $xslt.PSBase.PreserveWhitespace = $true        $xslt.LoadXml(@' <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"xmlns:cjbxsl="urn:schemas-bayes-co-uk:xslt" version="1.0">    <xsl:output method="text" />    <xsl:template match="cjbxsl:script">$code=@"        using System;        using System.Collections;        using System.Management.Automation;        using System.Xml;        public class <xsl:value-of select="@assembly-name" />        {        <xsl:apply-templates select="cjbxsl:method" mode="declarations" />        public <xsl:value-of select="@assembly-name" />(        <xsl:apply-templates select="cjbxsl:method" mode="constructor-args" />        )        {        <xsl:apply-templates select="cjbxsl:method" mode="constructor-body" />        }        <xsl:apply-templates select="cjbxsl:method" mode="methods" />        }"@Add-Type -TypeDefinition $code -ReferencedAssemblies System.xml$NewObject = New-Object <xsl:value-of select="@assembly-name" />(<xsl:apply-templates select="cjbxsl:method" mode="new-object" />)$NewObject    </xsl:template>    <xsl:template match="cjbxsl:method" mode="declarations">        public ScriptBlock script<xsl:value-of select="count(preceding::cjbxsl:method)" />;    </xsl:template>    <xsl:template match="cjbxsl:method" mode="constructor-args">        ScriptBlock Script<xsl:value-of select="count(preceding::cjbxsl:method)" />        <xsl:if test="not(position()=last())">, </xsl:if>    </xsl:template>    <xsl:template match="cjbxsl:method" mode="constructor-body">        script<xsl:value-of select="count(preceding::cjbxsl:method)" /> = Script<xsl:value-of select="count(preceding::cjbxsl:method)" />;    </xsl:template>    <xsl:template match="cjbxsl:method" mode="methods">        public <xsl:value-of select="@returns" /> <xsl:text>          </xsl:text> <xsl:value-of select="@name" />(<xsl:value-of select="@args" />)        {        ScriptBlock Script = script<xsl:value-of select="count(preceding::cjbxsl:method)" />;        try        {        return (String)Script.Invoke(p0)[0].BaseObject;        }        catch (Exception ex)        {        throw new InvalidOperationException("Script failed***!", ex);        }        }    </xsl:template>    <xsl:template match="cjbxsl:method" mode="new-object">        {<xsl:value-of select="." />}<xsl:if test="not(position()=last())">,</xsl:if>    </xsl:template>    <xsl:template match="@* | node()">        <xsl:apply-templates select="@* | node()" />    </xsl:template></xsl:stylesheet>'@)    }    process{        if ($_ -is [xml]){            invoke-transform -inxml $_ -inxsl $xslt        }    }    end{        if ($inxml -is [xml]){            invoke-transform -inxml $inxml -inxsl $xslt        }    }} And here is a stylesheet that will use it function T-AddChecksum{    param ($inxml)    begin{        . pslib:\xml\xslt\T-CreateObjectFromPSScriptXslt.ps1        . pslib:\xml\invoke-transform.ps1        [xml]$xslt = @' <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:cjbxsl="urn:schemas-bayes-co-uk:xslt" xmlns:ucjb="urn:cjb" version="1.0">    <cjbxsl:script language="Powershell" implements-prefix="ucjb" assembly-name="ChecksumSHA3">        <cjbxsl:method name="GetChecksum" returns="string" args="string p0">            <![CDATA[                $file = $args[0]                $stream = [System.IO.File]::OpenRead($file)                $sha256 = new-object System.Security.Cryptography.SHA256Managed                $checksum = $sha256.ComputeHash($stream)                [System.BitConverter]::ToString($checksum).Replace("-", [system.String]::Empty)            ]]>        </cjbxsl:method>        <cjbxsl:method name="GetChecksum384" returns="string" args="string p0">            <![CDATA[                $file = $args[0]                $stream = [System.IO.File]::OpenRead($file)                $sha384 = new-object System.Security.Cryptography.SHA384Managed                $checksum = $sha384.ComputeHash($stream)                [System.BitConverter]::ToString($checksum).Replace("-", [system.String]::Empty)            ]]>        </cjbxsl:method>    </cjbxsl: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>'@        $psco = Invoke-Expression (T-CreateObjectFromPSScriptXslt $xslt)    }    process{        if ($_ -is [xml]){            [xml](invoke-transform -inxml $_ -inxsl $xslt -extensionObjects @{ucjb=$psco})        }    }    end{        if ($inxml -is [xml]){            [xml](invoke-transform -inxml $inxml -inxsl $xslt -extensionObjects @{ucjb=$psco})        }    }} Voila!!! Powershell as an xslt scripting language.
Here is a simple translet for finding duplicates. function T-FindDuplicates{    param ($inxml)    begin{        . PSlib:\xml\invoke-transform.ps1        [xml]$xslt = @" <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">    <xsl:output method="xml" indent="yes" />    <xsl:key name="file-checksums" match="file" use="@Checksum" />    <xsl:template match="file">        <xsl:copy>            <xsl:attribute name="Duplicate">                <xsl:value-of select="count(key('file-checksums', @Checksum)) &gt; 1" />            </xsl:attribute>            <xsl:apply-templates select="@*|node()" />        </xsl:copy>    </xsl:template>    <xsl:template match="@* | node()">        <xsl:copy>            <xsl:apply-templates select="@* | node()" />        </xsl:copy>    </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)        }    }} As you can see in the code it adds a Duplicate attribute with a value of true or false depending on whether there is a file with a duplicate @Checksum. It can be used like this PS> . .\T-GetDirAsXml.ps1 PS> . .\T-AddChecksum.ps1 PS> . .\T-FindDuplicates.ps1 PS> Get-DirAsXml | T-AddChecksum | T-FindDuplicates It might produce <root Name="root" Root="True" Date="2008/11/03 01:35:14">    <folder Name="test" Base="D:\powershell\blog\test" Parent="D:\powershell\blog">        <folder Name="test2">            <file Duplicate="true" Name="test.ps1" Checksum="C47313D06C6AADA288AF6D61E03EFD7FA7C52DD73AB097E9D556535D330798D3" />            <file Duplicate="false" Name="test.txt" Checksum="CE217706948A41613FFA00C46B64D48A514D3D80758C8334EE00D6B0786AE47F" />            <file Duplicate="true" Name="test.zip" Checksum="7F2CCA02F17FF0E9458C0777C659D6D00B80F1C9D2921AEC971AE9A82D296AA5" />            <file Duplicate="true" Name="tmp.xml" Checksum="1351245F9834D0406C42DD5AF622FCA691A9A36F440A7C88F389927800292303" />        </folder>        <file Duplicate="true" Name="test.ps1" Checksum="C47313D06C6AADA288AF6D61E03EFD7FA7C52DD73AB097E9D556535D330798D3" />        <file Duplicate="false" Name="test.txt" Checksum="0D7439F5894B4E8EFEC8FB409635D0D8EA7A450E902F6B30B335907B5867DF16" />        <file Duplicate="true" Name="test.zip" Checksum="7F2CCA02F17FF0E9458C0777C659D6D00B80F1C9D2921AEC971AE9A82D296AA5" />        <file Duplicate="true" Name="tmp.xml" Checksum="1351245F9834D0406C42DD5AF622FCA691A9A36F440A7C88F389927800292303" />    </folder></root> Here is the code T-FindDuplicates.zip (745 b) All of the files in folder test2 are copies of the files in test except for test.txt and as you can see only having an @Duplicate indicator doesn't tell you which file the file is a duplicate of so this translet is only useful if you have very few duplicate files. What you do when you find a duplicate is up to you and depends very much on the downstream application. One thing you could do is put a list of the duplicate files into an attribute like this <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">    <xsl:output method="xml" indent="yes" />    <xsl:key name="file-checksums" match="file" use="@Checksum" />    <xsl:template match="file">        <xsl:copy>            <xsl:if test="count(key('file-checksums', @Checksum)) &gt; 1">                <xsl:attribute name="Duplicate">true</xsl:attribute>                <xsl:attribute name="Duplicates">                    <xsl:for-each select="key('file-checksums', @Checksum)">                        <xsl:call-template name="get-path" />                        <xsl:value-of select="'&#xA;'" />                    </xsl:for-each>                </xsl:attribute>            </xsl:if>            <xsl:apply-templates select="@*|node()" />        </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:template match="@* | node()">        <xsl:copy>            <xsl:apply-templates select="@* | node()" />        </xsl:copy>    </xsl:template></xsl:stylesheet> Which will produce <root Name="root" Root="True" Date="2008/11/03 01:35:14">    <folder Name="test" Base="D:\powershell\blog\test" Parent="D:\powershell\blog">        <folder Name="test2">            <file Duplicate="true" Duplicates="D:\powershell\blog\test\test2\test.ps1 D:\powershell\blog\test\test.ps1" Name="test.ps1" Checksum="C47313D06C6AADA288AF6D61E03EFD7FA7C52DD73AB097E9D556535D330798D3" />            <file Name="test.txt" Checksum="CE217706948A41613FFA00C46B64D48A514D3D80758C8334EE00D6B0786AE47F" />            <file Duplicate="true" Duplicates="D:\powershell\blog\test\test2\test.zip D:\powershell\blog\test\test.zip" Name="test.zip" Checksum="7F2CCA02F17FF0E9458C0777C659D6D00B80F1C9D2921AEC971AE9A82D296AA5" />            <file Duplicate="true" Duplicates="D:\powershell\blog\test\test2\tmp.xml D:\powershell\blog\test\tmp.xml" Name="tmp.xml" Checksum="1351245F9834D0406C42DD5AF622FCA691A9A36F440A7C88F389927800292303" />        </folder>        <file Duplicate="true" Duplicates="D:\powershell\blog\test\test2\test.ps1 D:\powershell\blog\test\test.ps1" Name="test.ps1" Checksum="C47313D06C6AADA288AF6D61E03EFD7FA7C52DD73AB097E9D556535D330798D3" />        <file Name="test.txt" Checksum="0D7439F5894B4E8EFEC8FB409635D0D8EA7A450E902F6B30B335907B5867DF16" />        <file Duplicate="true" Duplicates="D:\powershell\blog\test\test2\test.zip D:\powershell\blog\test\test.zip" Name="test.zip" Checksum="7F2CCA02F17FF0E9458C0777C659D6D00B80F1C9D2921AEC971AE9A82D296AA5" />        <file Duplicate="true" Duplicates="D:\powershell\blog\test\test2\tmp.xml D:\powershell\blog\test\tmp.xml" Name="tmp.xml" Checksum="1351245F9834D0406C42DD5AF622FCA691A9A36F440A7C88F389927800292303" />    </folder></root> Here is the code T-FindDuplicatesInfoTip.zip (927 b)