简体   繁体   English

内部带有前缀命名空间的非限定 XML 元素

[英]Unqualified XML-element with prefixed namespace inside

I have the following XML:我有以下 XML:

<ns1:verifySignedDocumentResponse xmlns:ns1="http://signing.ws.comarch.gov">
    <verifySignedDocumentReturn xmlns:ns2="http://exception.ws.comarch.gov">Some string content...</verifySignedDocumentReturn>
</ns1:verifySignedDocumentResponse>

In verifySignedDocumentReturn case I'm wondering, is it correct to define the prefix ( xmlns:ns2=... ), but not to qualify ( ns2:verifySignedDocumentReturn ) the appropriate element with this prefix?verifySignedDocumentReturn情况下,我想知道,定义前缀( xmlns:ns2=... ),但限定( ns2:verifySignedDocumentReturn )具有此前缀的适当元素是否正确?

w3schools.com gives examples , which show: w3schools.com给出了示例,其中显示:

  1. If element is prefixed, namespace for the prefix must be defined.如果元素带有前缀,则必须定义前缀的命名空间。
  2. Element should't be prefixed, if default namespace is used.如果使用默认命名空间,则元素不应带有前缀。

But in my example there is no default namespace.但是在我的示例中没有默认命名空间。 So, I expect verifySignedDocumentReturn to be prefixed with ns2 .所以,我希望verifySignedDocumentReturnns2为前缀。

I've got this XML-snippet from real service, so I wonder: is it correct and valid?我从真实服务中得到了这个 XML 片段,所以我想知道:它是否正确有效? Or just service creators' carelessness?还是只是服务创造者的粗心大意? I ask, because I'm new to XML/XSD.我问,因为我是 XML/XSD 的新手。

I've tried to generate XSD from this XML with different online-generators, but no generated schema looks reasonable.我试图用不同的在线生成器从这个 XML 生成 XSD,但没有生成的模式看起来合理。

Variant 1 (does not take into account ns2 namespace at all):变体 1(根本不考虑ns2命名空间):

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
           elementFormDefault="qualified" 
           targetNamespace="http://signing.ws.comarch.gov" 
           xmlns:ns1="http://signing.ws.comarch.gov">
   <xs:element name="verifySignedDocumentResponse">
   <xs:complexType>
      <xs:sequence>
        <xs:element name="verifySignedDocumentReturn" form="unqualified" 
                    type="xs:string"/>
      </xs:sequence>
   </xs:complexType>
 </xs:element>

Variant 2:变体 2:

schema0.xsd: schema0.xsd:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:ns1="http://signing.ws.comarch.gov" 
           xmlns:ns2="http://exception.ws.comarch.gov" 
           attributeFormDefault="unqualified" 
           elementFormDefault="qualified" 
           targetNamespace="http://signing.ws.comarch.gov" 
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:import schemaLocation="schema1.xsd" />
  <xs:element name="verifySignedDocumentResponse">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="verifySignedDocumentReturn" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

schema1.xsd: schema1.xsd:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" 
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <xs:element name="verifySignedDocumentReturn" type="xs:string" />
</xs:schema>

The first thing to note is the declaration of xmlns:ns2="http://exception.ws.comarch.gov" in your XML document does nothing.首先要注意的是 XML 文档中xmlns:ns2="http://exception.ws.comarch.gov"的声明没有任何作用。 Its just defining an alias ns2, which is never used.它只是定义了一个从未使用过的别名 ns2。

Both the generated schemas look OK.两个生成的模式看起来都不错。 A few notes about them though.关于他们的一些注意事项。

Although the first schema fits neatly in a single XSD, typically the elementFormDefault is set to qualified and not changed within the schema.尽管第一个模式非常适合单个 XSD,但通常elementFormDefault设置为qualified并且在模式内未更改。 In this schema the form is being set to unqualified for the inner element.在这个模式中, form被设置为unqualified的内部元素。 I'm not 100% sure, but I think that a validating XML parser using this schema will treat verifySignedDocumentReturn as if its in the namespace " http://signing.ws.comarch.gov ".我不是 100% 确定,但我认为使用此模式的验证 XML 解析器会将verifySignedDocumentReturn视为它在名称空间“ http://signing.ws.comarch.gov ”中。 I think changing the form in the middle of a XSD document is asking for incompatibility issues.我认为更改 XSD 文档中间的form会导致不兼容问题。

The second set of schemas look OK.第二组模式看起来不错。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM