3

I've received a great answer about sorting XML - I need to use XSLT. But how do I actually do that? What software is required?

What command or application do I need to start to get a "converted" XML output file, given that I've got an XML file and an XSLT file as input?

I don't have any development environment installed; this is a regular office computer with WinXP+IE7.

Update:
With help from this site, I created a small package that I want to share: XML-Sorter_v0.3.zip

3 Answers3

7

First decide whether you want to use XSLT 1.0 or 2.0. XSLT 2.0 is a much richer language, and the only reason for preferring XSLT 1.0 is that it's supported in a wider range of environments (for example, in the browser).

Then decide what XSLT processor you want to use. There's a wide choice for XSLT 1.0; a rather narrower choice for XSLT 2.0.

Then look in the documentation for that XSLT processor to find out how to run it.

Given that you seem to be OK with running the transformation from the Windows command line, I would recommend using Saxon-HE, which you can get from http://saxon.sf.net/. You will need to install Java, and then you can run Saxon. The documentation is here: http://www.saxonica.com/documentation/index.html#!using-xsl/commandline

If you prefer a simple GUI interface, consider "Kernow for Saxon".

If you want a development environment with a nice editor and debugger, you will have to pay for it, but Stylus Studio and oXygen are both good value, and both give you a choice of XSLT engines.

2

An XSLT Processor like Xalan-J for a command line solution. For a GUI editor/debugger you can use Eclipse, a tutorial here.

Edit: A fully web-based solution found here

TiCL
  • 453
1

assuming you have powershell available, and the XML file, and the XSLT file

<#
.SYNOPSIS
    Transform an xml file
.DESCRIPTION
    load a XML file and load a XSLT file, then transform the XML using the XSLT and output to the console
.PARAMETER $xsltfilename
    The path to the XSLT file
.PARAMETER $filename
    The path to the XML file to be transformed
.EXAMPLE
    C:\PS> 
    .\tranform.ps1 .\rgs_broke.xml .\fix_escalation.xslt > rgs_fixed.xml
.NOTES
    Author: Alex McCool
    Date:   Jan 17, 2017
#>

param(
[Parameter(Mandatory=$true)]
[string]$xsltfilename, 
[Parameter(Mandatory=$true)]
[string]$filename
)


function Load-Xml
{
param([string]$filename)

$content = Get-Content $filename

$stream = new-object System.IO.MemoryStream

$writer = new-object System.IO.StreamWriter($stream)
$writer.Write("$content")
$writer.Flush()
$stream.position = 0

$xml = new-object System.Xml.XmlTextReader($stream)

return $xml
}

function Load-Xslt
{
param([string]$filename)

$content = Get-Content $filename

$stream = new-object System.IO.MemoryStream
$writer = new-object System.IO.StreamWriter($stream)
$writer.Write("$content")
$writer.Flush()
$stream.position = 0

$reader = [System.Xml.XmlReader]::create($stream)
$xslt = New-Object System.Xml.Xsl.XslCompiledTransform
$xslt.Load($reader)

return $xslt
}


$xml = Load-Xml($filename)
$xslt = Load-Xslt($xsltfilename)

$output = New-Object System.IO.MemoryStream
$reader = new-object System.IO.StreamReader($output)


$arglist = new-object System.Xml.Xsl.XsltArgumentList
$xslt.Transform($xml, $arglist, $output)

$output.position = 0
$transformed = [string]$reader.ReadToEnd()
$reader.Close()

write-output $transformed

and here's a gist in case it changes

https://gist.github.com/amccool/560b533f1ea94560e0dae6367c2b75ce

BozoJoe
  • 205