具有命名空间的 XML 属性的 XSD 语法 [英] XSD syntax for XML attributes with namespace

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

问题描述

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

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 FilesJavajdk1.7.0_06in>xjc -p test C:ook.xsd解析架构...[错误] src-resolve.4.2:解析组件ns:number"时出错.被发现了'ns:number' 位于命名空间 'http://xmlns.oracle.com/id/1.0' 中,但组件此命名空间中的 s 不能从模式文档 'file:/C:/book.xsd'.如果这是不正确的命名空间,也许'ns:number'的前缀需要s 要改变.如果这是正确的命名空间,那么适当的导入"标记应添加到文件:/C:/book.xsd".file:/C:/book.xsd 的第 10 行

C:Program FilesJavajdk1.7.0_06in>xjc -p test C:ook.xsd parsing a schema... [ERROR] src-resolve.4.2: Error resolving component 'ns:number'. It was detected that 'ns:number' is in namespace 'http://xmlns.oracle.com/id/1.0', but component s 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' need s 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 语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆