WSDLToYAML.psm1

<#
 .Synopsis
  Parse WSDL and generate a YAML Model.
 
 .Description
  Parse WSDL, extract it's model and generate a YAML.
  Requires a root node name from which extract the model, a WSDL file path and optionally, the Out File name
 
 .Parameter Path
  The WSDL file Path.
 
  .Parameter NodeName
  The node name of the xs:element root node from which extract the nodel.
 
  .Parameter OutFile
  The YAML Model out file name. The default value is 'Model.yaml'
 
 .Example
   # Extracts Model from WSDL.xml, which contains a xs:element with name ServiceResponse and save it in ServiceModel.yaml file
   WSDLToYAML .\WSDL.xml ServiceResponse ServiceModel.yaml
#>


Import-Module powershell-yaml

function GetElements([Parameter(Mandatory=$true)][String]$NodeName) {
    $sequence = $global:doc.SelectNodes("//*[local-name()='complexType'][@name='$NodeName']").sequence
    $schemas = @{}
    $required = @()
    $table = @{}
    foreach($element in $sequence.element){
        $type = $element.type
        $type = $type.Substring($type.IndexOf(':') + 1)
        
        if ($type -eq "string" -or $type -eq "int" -or $type -eq "boolean"){
            if ($type -eq "int"){ $type = "integer"}
            $table.Add($element.name,@{type=$type})
        }
        else {
            $res = getElements $type
            $schemas += $res
            if ($element.maxOccurs -eq "unbounded"){
                $table.Add($element.name,@{type="array";items=@{'$ref'='#/components/schemas/' + $type}})
            }
            else {
                $table.Add($element.name,@{'$ref'='#/components/schemas/' + $type})
            }
        }
        if (!$element.nillable) { $required += $element.name }
    }
    $props=@{properties=$table;type='object'}
    if ($required.count -gt 0) { $props.Add('required',$required) }
    $schemas.Add($NodeName,$props)
    return $schemas
}

function WSDLToYAML ([Parameter(Mandatory=$true)][String] $Path, [Parameter(Mandatory=$true)][String] $NodeName, [String] $OutFile = "Model.yaml"){
    [XML]$global:doc = Get-Content $Path
    $type = $global:doc.SelectNodes("//*[local-name()='element'][@name='$NodeName']").type
    $type = $type.Substring($type.IndexOf(':') + 1)
    $schemas = getElements $type
    $components = @{schemas=$schemas}
    $model = @{'components'=$components}
    ConvertTo-YAML $model | Out-File $OutFile
}

Export-ModuleMember -Function WSDLToYAML