I want to create a function in my custom toolbox-module that uses a custom xml file to provide instructions (passed on using a $xmlpath
parameter). How can I validate that the provided xml is using the correct xml type/syntax/format(correct elements, root-node etc.)?
If I use a schema-file: How do I validate xml against it using ex. Test-XML (PSCX cmdlet)? Do I store the xsd file online and specify it's path in the xml-document? Do I store it in my modules-folder and hard-code it into the script? If so, how do I specify schema-path? Codesample:
#Stored in ..\Modules\MyModule\Process-Config.psm1)
function Process-Config
{
param($xmlpath)
#Test that file is xml
try{ $xml = [xml](Get-Content $xmlpath) } catch {}
#Test that xml-file is valid against schema in Module-folder (Modules\MyModule\xmlschema.xsd)
#Using Test-XML PSCX cmdlet
Test-XML -Path $xmlpath -SchemaPath #Schemapath#
}
EDIT: I found out that $PSScriptRoot
gives module-location so I can use $PSScriptRoot\Schemas\MySchema.xsd
as path. I thought it just worked on scripts, but seems functions work too. Do I specify schema in xml for a local file like this or publish the xsd online with schema-address in the xml file?