Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. <#
  2. .SYNOPSIS
  3. Transform an xml file
  4. .DESCRIPTION
  5. load a XML file and load a XSLT file, then transform the XML using the XSLT and output to the console
  6. .PARAMETER $xsltfilename
  7. The path to the XSLT file
  8. .PARAMETER $filename
  9. The path to the XML file to be transformed
  10. .EXAMPLE
  11. C:\PS>
  12. .\tranform.ps1 .\rgs_broke.xml .\fix_escalation.xslt > rgs_fixed.xml
  13. .NOTES
  14. Author: Alex McCool
  15. Date: Jan 17, 2017
  16. #>
  17.  
  18. param(
  19. [Parameter(Mandatory=$true)]
  20. [string]$xsltfilename,
  21. [Parameter(Mandatory=$true)]
  22. [string]$filename
  23. )
  24.  
  25.  
  26. function Load-Xml
  27. {
  28. param([string]$filename)
  29.  
  30. $content = Get-Content $filename
  31.  
  32. $stream = new-object System.IO.MemoryStream
  33.  
  34. $writer = new-object System.IO.StreamWriter($stream)
  35. $writer.Write("$content")
  36. $writer.Flush()
  37. $stream.position = 0
  38.  
  39. $xml = new-object System.Xml.XmlTextReader($stream)
  40.  
  41. return $xml
  42. }
  43.  
  44. function Load-Xslt
  45. {
  46. param([string]$filename)
  47.  
  48. $content = Get-Content $filename
  49.  
  50. $stream = new-object System.IO.MemoryStream
  51. $writer = new-object System.IO.StreamWriter($stream)
  52. $writer.Write("$content")
  53. $writer.Flush()
  54. $stream.position = 0
  55.  
  56. $reader = [System.Xml.XmlReader]::create($stream)
  57. $xslt = New-Object System.Xml.Xsl.XslCompiledTransform
  58. $xslt.Load($reader)
  59.  
  60. return $xslt
  61. }
  62.  
  63.  
  64. $xml = Load-Xml($filename)
  65. $xslt = Load-Xslt($xsltfilename)
  66.  
  67. $output = New-Object System.IO.MemoryStream
  68. $reader = new-object System.IO.StreamReader($output)
  69.  
  70.  
  71. $arglist = new-object System.Xml.Xsl.XsltArgumentList
  72. $xslt.Transform($xml, $arglist, $output)
  73.  
  74. $output.position = 0
  75. $transformed = [string]$reader.ReadToEnd()
  76. $reader.Close()
  77.  
  78. write-output $transformed
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement