在XSD中允许xml:space="preserve"以进行验证。

3

我正在尝试使用我编写的模式对XML文件进行验证,但在以下行中失败:

Element 'Route', attribute '{http://www.w3.org/XML/1998/namespace}space': The attribute '{http://www.w3.org/XML/1998/namespace}space' is not allowed.

XML文件有时包含如下内容:
 <Route xml:space="preserve">

</Route>

显然这是导致问题的原因,我该如何修改我的XSD文件以允许这种情况发生?

以下是我删除了所有非相关内容后的XSD:

<?xml version="1.0" standalone="yes"?>
<xs:schema id="NewDataSet" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:element name="NewDataSet">
      <xs:element name="Route" type="xs:string" minOccurs="0" /> 
      <xs:element name="FurtherRequirements" type="xs:string" minOccurs="0" />

非常感谢您的帮助!

所有的帮助都受到了欣然接受!

1个回答

5

您需要将Route的类型从xs:string更改为string-with-xml-space,其中string-with-xml-space是一个具有简单内容的复杂类型,类似于以下定义:

<xs:complexType name="string-with-xml-space">
  <xs:complexContent>
    <xs:extension base="xs:string">
      <xs:attribute ref="xml:space"/>

您还需要对XML命名空间的模式进行xs:import导入:
<xs:import namespace='http://www.w3.org/XML/1998/namespace'  
           schemaLocation='http://www.w3.org/2001/xml.xsd'/>

1
谢谢,您说的“您还需要导入XML命名空间模式的xs:import”是什么意思?请像对待白痴一样解释给我听。 - fiscme
如果您引用另一个命名空间中的定义(例如xml:space),则需要导入该命名空间的模式。这适用于xml命名空间和其他任何命名空间。xml命名空间的模式位于http://www.w3.org/2004/10/xml.xsd。 - Michael Kay

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接