Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在类型属性中使用自定义xml命名空间前缀_Xml_Xml Namespaces_Xsd Validation_Prefix_Xml Validation - Fatal编程技术网

在类型属性中使用自定义xml命名空间前缀

在类型属性中使用自定义xml命名空间前缀,xml,xml-namespaces,xsd-validation,prefix,xml-validation,Xml,Xml Namespaces,Xsd Validation,Prefix,Xml Validation,我想从xsd:string引入一个新的名称空间和前缀“extensing”hod:string。然后在我的模式中,我可以定义一个字符串元素: <xsd:element name="Label" type="hod:string"/> 我用“mystring”更改了所有的“hod:string”,以验证我定义的东西是正确的,而且看起来很开心,但如果可能的话,我想使用前缀。(还试图更改此设置:attributeFormDefault=“qualified”但似乎没有什么区别)简单类型从

我想从
xsd:string
引入一个新的名称空间和前缀“extensing”
hod:string
。然后在我的模式中,我可以定义一个字符串元素:

<xsd:element name="Label" type="hod:string"/>

我用“
mystring
”更改了所有的“
hod:string
”,以验证我定义的东西是正确的,而且看起来很开心,但如果可能的话,我想使用前缀。(还试图更改此设置:
attributeFormDefault=“qualified”
但似乎没有什么区别)

简单类型从
xs:schema
元素上的
targetNamespace
属性获取名称空间。由于您的
xs:schema
元素没有
targetNamespace
,因此您当前的元素/类型(包括自定义字符串类型)没有命名空间

如果希望将当前元素保留在无命名空间中,并且只对字符串类型使用命名空间,则需要在具有不同
targetNamepsace
的新架构中定义该类型,并将其导入此架构。例如:

hodtypes.xsd

<xsd:schema
       xmlns:xsd="http://www.w3.org/2001/XMLSchema"
       xmlns:hod="http://hod.com/2019/XMLSchema/hod"
       targetNamespace="http://hod.com/2019/XMLSchema/hod">

  <xsd:simpleType name="string">
    <xsd:restriction base="xsd:string">
      <xsd:maxLength value="80"/>
    </xsd:restriction>
  </xsd:simpleType>

</xsd:schema>
<xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:xsd="http://www.w3.org/2001/XMLSchema"
       xmlns:xs="http://www.w3.org/2001/XMLSchema"
       xmlns:hod="http://hod.com/2019/XMLSchema/hod"
       attributeFormDefault="unqualified" elementFormDefault="qualified">

  <xsd:import namespace="http://hod.com/2019/XMLSchema/hod" schemaLocation="hodtypes.xsd" />

  <xsd:complexType name="Object">
    <xsd:sequence>
      <xsd:element name="Label" type="hod:string"/>
      <xsd:element name="MateriaID" type="GUID"/>
      ...

main.xsd

<xsd:schema
       xmlns:xsd="http://www.w3.org/2001/XMLSchema"
       xmlns:hod="http://hod.com/2019/XMLSchema/hod"
       targetNamespace="http://hod.com/2019/XMLSchema/hod">

  <xsd:simpleType name="string">
    <xsd:restriction base="xsd:string">
      <xsd:maxLength value="80"/>
    </xsd:restriction>
  </xsd:simpleType>

</xsd:schema>
<xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:xsd="http://www.w3.org/2001/XMLSchema"
       xmlns:xs="http://www.w3.org/2001/XMLSchema"
       xmlns:hod="http://hod.com/2019/XMLSchema/hod"
       attributeFormDefault="unqualified" elementFormDefault="qualified">

  <xsd:import namespace="http://hod.com/2019/XMLSchema/hod" schemaLocation="hodtypes.xsd" />

  <xsd:complexType name="Object">
    <xsd:sequence>
      <xsd:element name="Label" type="hod:string"/>
      <xsd:element name="MateriaID" type="GUID"/>
      ...

...

显示
simpleType
元素的
name
属性需要是
NCName
,因此不允许使用
name=“hod:string”
。如果要在特定命名空间中声明类型,请设置一个架构,该架构的命名空间为
targetNamespace
xs:import
该架构。@MartinHonnen我尝试了您所说的,IDE很高兴,但当我运行它时,会引发此异常:System.Xml.Schema.XmlSchemaValidationException:未声明类型“”。@MartinHonnen我让它工作了。我认为导入会导致
XDocument.Load
自动加载
中引用的.xsd文件。显然情况并非如此;我寻找了一个自动加载的选项,但没有找到。为新文件调用
XmlSchemaSet::add
,修复了它。谢谢,成功了。在验证之前,我只需将新模式添加到XmlSchemaSet。