带有命名空间的XML属性的XSD语法

带有命名空间的XML属性的XSD语法

本文介绍了带有命名空间的XML属性的XSD语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个xml片段我需要写XSD

I have an xml fragment for which I need to write XSD

<root xmlns="http://xmlns.oracle.com/sca/1.0" xmlns:id="http://xmlns.oracle.com/id/1.0">
  <service name="Book" id:number="465"/>
</root>

以下XSD在生成JAXB类时出错。

The following XSD gives error while JAXB class generation.

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://xmlns.oracle.com/sca/1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="root">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="service">
          <xs:complexType>
            <xs:simpleContent>
              <xs:extension base="xs:string">
                <xs:attribute type="xs:string" name="name"/>
                <xs:attribute ref="ns:number" xmlns:ns="http://xmlns.oracle.com/id/1.0"/>
              </xs:extension>
            </xs:simpleContent>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  </xs:schema>

错误是

C:\ Program Files\Java\jdk1.7.0_06\bin> xjc -p test C:\ book.xsd
解析模式...
[错误] src-resolve.4.2:错误解析组件'ns:number'。检测到
'ns:number'在命名空间'http://xmlns.oracle.com/id/1.0'中,但来自此命名空间的组件
s不能从架构文档'文件中引用: / C:/书。
xsd'。如果这是不正确的命名空间,那么'ns:number'的前缀可能需要更改
s。如果这是正确的命名空间,则应将相应的'import'
标记添加到'file:/ C:/book.xsd'。
第10行文件:/ C:/book.xsd

C:\Program Files\Java\jdk1.7.0_06\bin>xjc -p test C:\book.xsdparsing a schema...[ERROR] src-resolve.4.2: Error resolving component 'ns:number'. It was detectedthat 'ns:number' is in namespace 'http://xmlns.oracle.com/id/1.0', but components from this namespace are not referenceable from schema document 'file:/C:/book.xsd'. If this is the incorrect namespace, perhaps the prefix of 'ns:number' needs to be changed. If this is the correct namespace, then an appropriate 'import'tag should be added to 'file:/C:/book.xsd'. line 10 of file:/C:/book.xsd

推荐答案

您实际上至少需要尽可能多的XSD文件作为名称空间,因为一个XSD文件只能定位一个名称空间,或者没有名称空间。

You actually need at least as many XSD files as namespaces since one XSD file can target only one namespace, or none.

由于你的根元素在一个名称空间中,而另一个属性在另一个名称空间中,你需要两个文件至少。您可以通过 xsd:import 链接它们。

Since your root element is in one namespace, and the attribute in another, you need then two files at least. You "link" them through an xsd:import.

热门XSD:

<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)-->
<xsd:schema xmlns="http://xmlns.oracle.com/sca/1.0" xmlns:id="http://xmlns.oracle.com/id/1.0" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://xmlns.oracle.com/sca/1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:import schemaLocation="xsd-syntax-for-xml-attributes-with-namespace1.xsd" namespace="http://xmlns.oracle.com/id/1.0" />
  <xsd:element name="root">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="service">
          <xsd:complexType>
            <xsd:attribute name="name" type="xsd:string" use="required" />
            <xsd:attribute ref="id:number" use="required" />
          </xsd:complexType>
        </xsd:element>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

xsd-syntax-for-xml-attributes-with-namespace1.xsd

<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)-->
<xsd:schema xmlns="http://xmlns.oracle.com/id/1.0" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://xmlns.oracle.com/id/1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:attribute name="number" type="xsd:unsignedShort" />
</xsd:schema>

这篇关于带有命名空间的XML属性的XSD语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 20:20