質問

Is it possible to determine which namespaces and schemas should be specified in some XML based on the schemas used to define the elements being used in that XML?

Given these schemas:

what additional information (if any) is required to determine the namespaces and schemas mentioned in the following XML extract source

<?xml version="1.0" encoding="UTF-8"?>
<GovTalkMessage xsi:schemaLocation="http://www.govtalk.gov.uk/schemas/govtalk/govtalkheader http://xmlgw.companieshouse.gov.uk/v1-0/schema/Egov_ch.xsd"
    xmlns="http://www.govtalk.gov.uk/schemas/govtalk/govtalkheader"
    xmlns:dsig="http://www.w3.org/2000/09/xmldsig#"
    xmlns:gt="http://www.govtalk.gov.uk/schemas/govtalk/core"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
  <EnvelopeVersion>1.0</EnvelopeVersion>
  <Header>
      [removed for brevity]
  </Header>
  <GovTalkDetails>
    <Keys />
  </GovTalkDetails>
  <Body>
    <NameSearchRequest xmlns="http://xmlgw.companieshouse.gov.uk/v1-0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://xmlgw.companieshouse.gov.uk/v1-0/schema/NameSearch.xsd" >
        [removed for brevity]
    </NameSearchRequest>
  </Body>
</GovTalkMessage>


In the GovTalkMessage element, I understand where the 2nd part of xsi:schemaLocation comes from, but I'm not sure whether it's possible to determine the first part from the schema itself.

I don't understand where the xmlns value has come from in either of GovTalkMessage or NameSearchRequest. Is it defined somewhere?

xmlns:dsig, xmlns:gt are mentioned in the GovTalkMessage schema, but so is xmlns:hd, how do I tell which one's need mentioning in the XML above and which don't?

Why has NameSearchRequest's xsi:schemaLocation only got one value? I thought that attribute required two values, separated by a space.

役に立ちましたか?

解決

Please see my answers below.

In the GovTalkMessage element, I understand where the 2nd part of xsi:schemaLocation comes from, but I'm not sure whether it's possible to determine the first part from the schema itself.

The first part of xsi:schemaLocation tells what namespace the schema located is associated with. Basically, you are using the namespace http://www.govtalk.gov.uk/schemas/govtalk/govtalkheader for the schema located at http://xmlgw.companieshouse.gov.uk/v1-0/schema/Egov_ch.xsd (for any schema used in xsi:schemaLocation, it is always the target namespace (see targetNamespace attribute) of that schema which comes first and then the location)

I don't understand where the xmlns value has come from in either of GovTalkMessage or NameSearchRequest. Is it defined somewhere?

xmlns is the default namespace. Elements fall under default namespace if they are not qualified by using prefix. In this case the elements GovTalkMessage and NameSearchRequest fall under the default namespace referred by xmlns attributes in your respective schemas.

xmlns:dsig, xmlns:gt are mentioned in the GovTalkMessage schema, but so is xmlns:hd, how do I tell which one's need mentioning in the XML above and which don't?

If you want to use elements or attributes defined by those namespaces (xmlns:dsig, xmlns:gt, xmlns:hd,...) in your XML, you will declare these namespaces. It's like declaring object references in Java and using attributes of those object references at a later point in your program.

Why has NameSearchRequest's xsi:schemaLocation only got one value? I thought that attribute required two values, separated by a space.

If you notice the schema http://xmlgw.companieshouse.gov.uk/v1-0/schema/NameSearch.xsd, which has no target namespace. This is the reason you don't have namespace but just location in this case. But, you have to use xsi:noNamespaceSchemaLocation instead of xsi:schemaLocation otherwise xml parsers will throw errors. xsi:schemaLocation only takes "namespace location" pairs.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top