I created an XML with inline schema and wrote a Powershell script to validate my XML, but it was failing every time.
Not sure whether the problem lies with my XML or with the validating script, I copied the inline-namespaced.xml example from Microsoft and my script said it was valid. My script used LINQ to XML. However, when I "invalidated" the XML by renaming the 'book' element to 'books' (which is not described in the schema), it still said it was valid (!). According to the answer to this question, LINQ to XML validates everything when the targetNamespace of the schema does NOT match the one of the document.
So I modified my script to use XmlReader, which also fails to validate the basic example from Microsoft. It says:
Line 2, Pos 2, Could not find schema information for the element 'catalog'.
So, there is already a problem with the root element.
Obviously, I'm doing something wrong, but what?
inline-namespaced.xml:
<?xml version="1.0"?>
<catalog xmlns:xsd="; xmlns:x="urn:book">
<!-- START OF SCHEMA -->
<xsd:schema targetNamespace="urn:book">
<xsd:element name="book">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="author" type="xsd:string"/>
<xsd:element name="title" type="xsd:string"/>
<xsd:element name="genre" type="xsd:string"/>
<xsd:element name="price" type="xsd:float"/>
<xsd:element name="publish_date" type="xsd:date"/>
<xsd:element name="description" type="xsd:string"/>
</xsd:sequence>
<xsd:attribute name="id" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<!-- END OF SCHEMA -->
<x:book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</x:book>
</catalog>
Powershell 5.1 script using XmlReader:
# Load the XML content
# From: (v=vs.85)
$xmlFile = "C:\Temp\inline-namespaced.xml"
# Prepare the event handler for validation errors
$eventHandler = {
param ($sender, $eventArgs)
Write-Host $("Line {0}, Pos {1}, {2}" -f $eventArgs.Exception.LineNumber, $eventArgs.Exception.LinePosition, $eventArgs.Exception.Message)
}
# Create XmlReaderSettings and enable schema validation
$settings = New-Object System.Xml.XmlReaderSettings
$settings.ValidationType = [System.Xml.ValidationType]::Schema
$settings.ValidationFlags =
[System.Xml.Schema.XmlSchemaValidationFlags]::ProcessInlineSchema -bor
[System.Xml.Schema.XmlSchemaValidationFlags]::ReportValidationWarnings
# Attach the event handler
$settings.add_ValidationEventHandler($eventHandler)
# Create the XmlReader with the settings
$reader = [System.Xml.XmlReader]::Create($xmlFile, $settings)
# Read through the XML content to trigger validation
while ($reader.Read()) { }
$reader.Close()
I created an XML with inline schema and wrote a Powershell script to validate my XML, but it was failing every time.
Not sure whether the problem lies with my XML or with the validating script, I copied the inline-namespaced.xml example from Microsoft and my script said it was valid. My script used LINQ to XML. However, when I "invalidated" the XML by renaming the 'book' element to 'books' (which is not described in the schema), it still said it was valid (!). According to the answer to this question, LINQ to XML validates everything when the targetNamespace of the schema does NOT match the one of the document.
So I modified my script to use XmlReader, which also fails to validate the basic example from Microsoft. It says:
Line 2, Pos 2, Could not find schema information for the element 'catalog'.
So, there is already a problem with the root element.
Obviously, I'm doing something wrong, but what?
inline-namespaced.xml:
<?xml version="1.0"?>
<catalog xmlns:xsd="http://www.w3./2001/XMLSchema" xmlns:x="urn:book">
<!-- START OF SCHEMA -->
<xsd:schema targetNamespace="urn:book">
<xsd:element name="book">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="author" type="xsd:string"/>
<xsd:element name="title" type="xsd:string"/>
<xsd:element name="genre" type="xsd:string"/>
<xsd:element name="price" type="xsd:float"/>
<xsd:element name="publish_date" type="xsd:date"/>
<xsd:element name="description" type="xsd:string"/>
</xsd:sequence>
<xsd:attribute name="id" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<!-- END OF SCHEMA -->
<x:book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</x:book>
</catalog>
Powershell 5.1 script using XmlReader:
# Load the XML content
# From: https://learn.microsoft/en-us/previous-versions/windows/desktop/ms759142(v=vs.85)
$xmlFile = "C:\Temp\inline-namespaced.xml"
# Prepare the event handler for validation errors
$eventHandler = {
param ($sender, $eventArgs)
Write-Host $("Line {0}, Pos {1}, {2}" -f $eventArgs.Exception.LineNumber, $eventArgs.Exception.LinePosition, $eventArgs.Exception.Message)
}
# Create XmlReaderSettings and enable schema validation
$settings = New-Object System.Xml.XmlReaderSettings
$settings.ValidationType = [System.Xml.ValidationType]::Schema
$settings.ValidationFlags =
[System.Xml.Schema.XmlSchemaValidationFlags]::ProcessInlineSchema -bor
[System.Xml.Schema.XmlSchemaValidationFlags]::ReportValidationWarnings
# Attach the event handler
$settings.add_ValidationEventHandler($eventHandler)
# Create the XmlReader with the settings
$reader = [System.Xml.XmlReader]::Create($xmlFile, $settings)
# Read through the XML content to trigger validation
while ($reader.Read()) { }
$reader.Close()
Share
Improve this question
edited yesterday
President James K. Polk
42k28 gold badges109 silver badges145 bronze badges
asked Feb 17 at 20:13
WimWim
1251 silver badge8 bronze badges
9
|
Show 4 more comments
1 Answer
Reset to default 0Your XML is complicated due to the presence of a namespace.
Please try the following solution that has two XSD files instead of one!!!
I tested it as-is, i.e. without PowerShell. You can try it in c# and LINQ to XML.
XML
<?xml version="1.0"?>
<catalog xmlns:x="urn:book" xmlns:xsi="http://www.w3./2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Wim.xsd">
<x:book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</x:book>
</catalog>
Wim.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3./2001/XMLSchema" elementFormDefault="qualified" xmlns:x="urn:book">
<xs:import namespace="urn:book" schemaLocation="x.xsd"/>
<xs:element name="catalog">
<xs:complexType>
<xs:sequence>
<xs:element ref="x:book"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="author" type="xs:string"/>
<xs:element name="title" type="xs:string"/>
<xs:element name="genre" type="xs:NCName"/>
<xs:element name="price" type="xs:decimal"/>
<xs:element name="publish_date" type="xs:NMTOKEN"/>
<xs:element name="description" type="xs:string"/>
</xs:schema>
x.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3./2001/XMLSchema" elementFormDefault="qualified" targetNamespace="urn:book" xmlns:x="urn:book">
<xs:import schemaLocation="Wim.xsd"/>
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:element ref="author"/>
<xs:element ref="title"/>
<xs:element ref="genre"/>
<xs:element ref="price"/>
<xs:element ref="publish_date"/>
<xs:element ref="description"/>
</xs:sequence>
<xs:attribute name="id" use="required" type="xs:NCName"/>
</xs:complexType>
</xs:element>
</xs:schema>
$settings.ValidationFlags = [System.Xml.Schema.XmlSchemaValidationFlags]::ProcessInlineSchema -bor [System.Xml.Schema.XmlSchemaValidationFlags]::ReportValidationWarnings
so yes, ReportValidationWarnings is added as a flag. But fet the book to books mod. That was when I used LINQ to XML and it said that both versions were valid which imho is not correct. That's why I switched to XmlReader. I assume (dangerous, I know) that Microsoft's example XML is correct, but I fail to validate that XML with the above script and don't understand why. – Wim Commented 2 days ago