te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>How to validate an XML containing an inline schema with Powershell 5.1? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to validate an XML containing an inline schema with Powershell 5.1? - Stack Overflow

programmeradmin3浏览0评论

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
  • Proshell is written in Net library so you can always check the c# online documentation. See XMLReader Settings Valiation flags : learn.microsoft/en-us/dotnet/api/… – jdweng Commented Feb 17 at 22:30
  • @jdweng, I do use the XmlReader ValidationFlags as in the link you provided. I even tried the inlineSchema.xml from the link and I got the exact same errors as described in the link. So my question remains: how to solve the first error "Could not find schema information for the element 'root'" (or 'catalog' as the root element is called in the original example)? – Wim Commented 2 days ago
  • I'm not seeing any errors when changing "book to books" using another tools. Only seeing warning "could not find books in schema". Did you see in link the option for ReportValidationWarnings? – jdweng Commented 2 days ago
  • The code that I posted contains $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
  • I did not find any errors. Did you check the xml with an online checker to see if there are errors? – jdweng Commented 2 days ago
 |  Show 4 more comments

1 Answer 1

Reset to default 0

Your 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>
发布评论

评论列表(0)

  1. 暂无评论