Quantcast
Channel: get-command | PowerShell » XML
Viewing all articles
Browse latest Browse all 3

Infer XSD from XML

$
0
0

A quick and dirty way to infer an xml schema from an xml file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<#
.SYNOPSIS
        Infers an xml schema from an xml file
.PARAMETER XMLPath
        Specifies the filename or FileInfo object representing the source xml file, for now relative paths will produce an error. Get-Item or Get-ChildItem provide a good workaround.
.PARAMETER XSDPath
        Specifies the full filename for the output xml schema file.
.EXAMPLE
        PS > Infer-XSD -XMLPath c:\menu.xml -XSDPath c:\menu.xsd
#>
#requires -version 2
param (
        [Parameter(
                Mandatory = $true, 
                ValueFromPipeline = $true)]$XMLPath,
        [Parameter(Mandatory=$true)]
        [string]
        $XSDPath
)
try {
  Add-Type -assembly System.XML.Linq
  Add-type -assembly System.XML
 
  [System.Xml.Schema.XMLSchemaInference] $inference = `
     new-object System.Xml.Schema.XMLSchemaInference
  [System.XML.Schema.XMLSchemaSet] $schemaSet = `
     $inference.InferSchema([System.XML.XMLReader]::Create($XMLPath))
  [System.XML.XMLWriter] $xmlwriter = `
     [System.XML.XMLWriter]::Create($XSDPath)
  $schemaSet.Schemas() | % { $_.write($xmlwriter) }
  $xmlwriter.Close()
}
catch { $_ }

Viewing all articles
Browse latest Browse all 3

Trending Articles