命名空间“http://yyy”中的子元素“xxx”无效,而 xml 中实际上没有额外的命名空间设置
Posted
技术标签:
【中文标题】命名空间“http://yyy”中的子元素“xxx”无效,而 xml 中实际上没有额外的命名空间设置【英文标题】:Invalid child element 'xxx' in namespace 'http://yyy' while there is actually no extra namespace setting in xml 【发布时间】:2021-01-14 18:40:48 【问题描述】:SO 有很多类似的讨论,但在查看了大约 5-6 个帖子之后。他们似乎都不符合我的情况。根据我的观察,之前的大多数帖子似乎都有额外的命名空间设置导致问题,但我认为我的情况不一样。
所以我有一个将接收以下 xml 的 web 服务
<?xml version="1.0" encoding="UTF-8"?>
<ProductBindingReportRequest xmlns="http://schemas.ms.it.oem/digitaldistribution/2010/10" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<CustomerBindingUniqueID>FA8E995F-50D2E911-9475-0050560063F7</CustomerBindingUniqueID>
<SoldToCustomerID>0000064397</SoldToCustomerID>
<ReceivedFromCustomerID>QACN-CQ</ReceivedFromCustomerID>
<TotalLineItems>2</TotalLineItems>
<ProductBindings>
<ProductBinding>
<YZProductKeyID>3423553504516</YZProductKeyID>
<XYSpecifics>
<XYSerialNumber>5CD936C2FW</XYSerialNumber>
<ConsumedDate>9/8/2019 3:47:37 PM</ConsumedDate>
</XYSpecifics>
<ServiceProductKeyID>4020124189204</ServiceProductKeyID>
</ProductBinding>
<ProductBinding>
<YZProductKeyID>3423553504661</YZProductKeyID>
<XYSpecifics>
<XYSerialNumber>5CD936C1KK</XYSerialNumber>
<ConsumedDate>9/8/2019 3:47:55 PM</ConsumedDate>
</XYSpecifics>
<ServiceProductKeyID>4020124189205</ServiceProductKeyID>
</ProductBinding>
</ProductBindings>
<XYSpecifics>
<SiteIdentifier>QACN-CQ</SiteIdentifier>
</XYSpecifics>
</ProductBindingReportRequest>
我们需要验证xml输入所以基于上面的xml我创建了下面的xsd
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="ProductBindingReportRequest">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="CustomerBindingUniqueID" type="xsd:string" />
<xsd:element name="SoldToCustomerID" type="xsd:unsignedShort" />
<xsd:element name="ReceivedFromCustomerID" type="xsd:string" />
<xsd:element name="TotalLineItems" type="xsd:unsignedByte" />
<xsd:element name="ProductBindings">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="unbounded" name="ProductBinding">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="YZProductKeyID" type="xsd:unsignedLong" />
<xsd:element name="XYSpecifics">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="XYSerialNumber" type="xsd:string" />
<xsd:element name="ConsumedDate" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="ServiceProductKeyID" type="xsd:unsignedLong" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="XYSpecifics">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="SiteIdentifier" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
我们通过下面的方法用xsd验证xml
public static bool ValidateXML(string strXML, string strSchemePath, string strXMLSchemeName, ref string strException)
if (strSchemePath.Substring(strSchemePath.Length - 1) != "\\")
strSchemePath = strSchemePath + "\\";
try
XmlReaderSettings settings = new XmlReaderSettings();
settings.Schemas.Add("http://schemas.ms.it.oem/digitaldistribution/2010/10", strSchemePath + strXMLSchemeName);
settings.ValidationType = ValidationType.Schema;
XmlReader readerCheck = XmlReader.Create(new StringReader(strXML), settings);
XmlDocument checkXmlDocument = new XmlDocument();
checkXmlDocument.Load(readerCheck);
checkXmlDocument.RemoveAll();
return true;
catch (Exception ex)
strException = ex.ToString();
return false;
大多数接收到的 xml 都可以在相同的实现下正常工作,但上面的组合给了我
命名空间中的元素“XYSpecifics” 'http://schemas.ms.it.oem/digitaldistribution/2010/10' 无效 命名空间中的子元素“SiteIdentifier” 'http://schemas.ms.it.oem/digitaldistribution/2010/10'。
我的问题是
-
是不是因为
XYSpecifics
存在于内部和外部
ProductBindings
?
有什么方法可以调整 xsd 以使验证正常工作?
【问题讨论】:
尝试将 StreamReader 更改为 XmlReader:XmlReader readerCheck = XmlReader.Create(strXML, settings); 【参考方案1】:我不完全确定是什么
settings.Schemas.Add("http://schemas.ms.it.oem/digitaldistribution/2010/10", strSchemePath + strXMLSchemeName);
作用于 XML 阅读器实例,但您也可以在 XML 模式文档本身中包含此命名空间:
XML 架构
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://schemas.ms.it.oem/digitaldistribution/2010/10">
<xsd:element name="ProductBindingReportRequest">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="CustomerBindingUniqueID" type="xsd:string" />
<xsd:element name="SoldToCustomerID" type="xsd:unsignedShort" />
<xsd:element name="ReceivedFromCustomerID" type="xsd:string" />
<xsd:element name="TotalLineItems" type="xsd:unsignedByte" />
<xsd:element name="ProductBindings">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="unbounded" name="ProductBinding">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="YZProductKeyID" type="xsd:unsignedLong" />
<xsd:element name="XYSpecifics">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="XYSerialNumber" type="xsd:string" />
<xsd:element name="ConsumedDate" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="ServiceProductKeyID" type="xsd:unsignedLong" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="XYSpecifics">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="SiteIdentifier" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
这应该可以工作,尽管有点不幸的是元素XYSpecifics
在此架构中定义了两次,具有不同的子元素。编写模式的更好方法是定义这种类型一次。例如:
XML 架构(改进)
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://schemas.ms.it.oem/digitaldistribution/2010/10"
xmlns="http://schemas.ms.it.oem/digitaldistribution/2010/10">
<xsd:element name="ProductBindingReportRequest">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="CustomerBindingUniqueID" type="xsd:string" />
<xsd:element name="SoldToCustomerID" type="xsd:unsignedShort" />
<xsd:element name="ReceivedFromCustomerID" type="xsd:string" />
<xsd:element name="TotalLineItems" type="xsd:unsignedByte" />
<xsd:element name="ProductBindings">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="unbounded" name="ProductBinding">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="YZProductKeyID" type="xsd:unsignedLong" />
<xsd:element name="XYSpecifics" type="XYSpecificsType"/>
<xsd:element name="ServiceProductKeyID" type="xsd:unsignedLong" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="XYSpecifics" type="XYSpecificsType"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:complexType name="XYSpecificsType">
<xsd:choice maxOccurs="3">
<xsd:element name="XYSerialNumber" type="xsd:string" />
<xsd:element name="ConsumedDate" type="xsd:string" />
<xsd:element name="SiteIdentifier" type="xsd:string" />
</xsd:choice>
</xsd:complexType>
</xsd:schema>
【讨论】:
【参考方案2】:您编写了一个模式来验证不在命名空间中的元素。您需要为命名空间“http://schemas.ms.it.oem/digitaldistribution/2010/10”中的元素编写模式,您可以通过在xs:schema
元素上设置 targetNamespace 属性来完成。这必须在架构本身中完成,不能在验证 API 中完成。
【讨论】:
以上是关于命名空间“http://yyy”中的子元素“xxx”无效,而 xml 中实际上没有额外的命名空间设置的主要内容,如果未能解决你的问题,请参考以下文章
映射 hbm 文件时出现错误“命名空间中的元素 'class' 具有无效的子元素 'set'”