导入没有命名空间的xsd

时间:2013-01-07 16:43:12

标签: xml xsd jaxb xjc

与标题一样,如何导入没有定义targetNamespace的模式?

我有以下案例:

文件:Data.xml

<?xml version="1.0"?>
<lib:library xmlns:lib="http://java2s.com/ns/library">
  <lib:book id="b0836217462">
    <lib:title>Being a Dog Is a Full-Time Job</lib:title>
    <lib:authors>
      <person id="CMS">
        <name>Charles M Schulz</name>
      </person>
    </lib:authors>
  </lib:book>
</lib:library>

文件:Schema.xsd

<?xml version="1.0"?>
<xs:schema targetNamespace="http://java2s.com/ns/library"
  elementFormDefault="qualified" attributeFormDefault="unqualified"
  xmlns:lib="http://java2s.com/ns/library"
  xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:import schemaLocation="another.xsd" />
  <xs:element name="library">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="book" type="lib:bookType" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:complexType name="bookType">
    <xs:sequence>
      <xs:element name="title" type="xs:string" />
      <xs:element name="authors">
        <xs:complexType>
          <xs:sequence>
            <xs:element ref="person" />
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
    <xs:attribute name="id" type="xs:ID" use="required" />
  </xs:complexType>
</xs:schema>

文件:another.xsd

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="person" type="personType" />
  <xs:complexType name="personType">
    <xs:sequence>
      <xs:element name="name" type="xs:string" />
    </xs:sequence>
    <xs:attribute name="id" type="xs:ID" use="required" />
  </xs:complexType>
</xs:schema>

这一切似乎没问题,但是当我尝试为这些XSD生成Jaxb类时,我得到以下异常:

org.xml.sax.SAXParseException: undefined element declaration 'person'
    at com.sun.xml.xsom.impl.parser.ParserContext$1.reportError(ParserContext.java:176)
    at com.sun.xml.xsom.impl.parser.NGCCRuntimeEx.reportError(NGCCRuntimeEx.java:170)
    at com.sun.xml.xsom.impl.parser.DelayedRef.resolve(DelayedRef.java:125)
    at com.sun.xml.xsom.impl.parser.DelayedRef.run(DelayedRef.java:100)
    at com.sun.xml.xsom.impl.parser.ParserContext.getResult(ParserContext.java:131)
    at com.sun.xml.xsom.parser.XSOMParser.getResult(XSOMParser.java:226)
    at com.sun.tools.xjc.ModelLoader.createXSOM(ModelLoader.java:519)
    at com.sun.tools.xjc.api.impl.s2j.SchemaCompilerImpl.bind(SchemaCompilerImpl.java:236)
    at com.sun.tools.xjc.api.impl.s2j.SchemaCompilerImpl.bind(SchemaCompilerImpl.java:85)
    at org.apache.cxf.tools.wsdlto.databinding.jaxb.JAXBDataBinding.initialize(JAXBDataBinding.java:411)
    at org.apache.cxf.tools.wsdlto.WSDLToJavaContainer.generateTypes(WSDLToJavaContainer.java:603)
    at org.apache.cxf.tools.wsdlto.WSDLToJavaContainer.processWsdl(WSDLToJavaContainer.java:248)
    at org.apache.cxf.tools.wsdlto.WSDLToJavaContainer.execute(WSDLToJavaContainer.java:142)
    at org.apache.cxf.tools.wsdlto.WSDLToJavaContainer.execute(WSDLToJavaContainer.java:300)
    at org.apache.cxf.tools.common.toolspec.ToolRunner.runTool(ToolRunner.java:103)
    at org.apache.cxf.tools.wsdlto.WSDLToJava.run(WSDLToJava.java:113)
    at org.apache.cxf.tools.wsdlto.WSDLToJava.run(WSDLToJava.java:86)
    at org.apache.cxf.maven_plugin.wsdl2java.WSDL2JavaMojo.generate(WSDL2JavaMojo.java:380)
    at org.apache.cxf.maven_plugin.AbstractCodegenMoho.execute(AbstractCodegenMoho.java:257)
    at org.apache.cxf.maven_plugin.wsdl2java.WSDL2JavaMojo.execute(WSDL2JavaMojo.java:477)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)

targetNamespace添加到文件another.xsd并在文件Schema.xsd中对其进行限定可以解决问题,但文件Delta.xml中的元素人员将位于该targetNamespace中,而不是我想要的是什么。

我的问题基本上是:为什么JAXB(或XJC)在没有命名空间时无法识别person元素?

对于Jaxb代,我使用cxf codegen插件,它使用以下内容:JAXB XJC的版本hudson-jaxb-ri-2.1-2

1 个答案:

答案 0 :(得分:0)

你遇到的问题无关“导入没有命名空间的xsd”。您列出的XSD非常好。问题很可能在于您的设置,因为person对象的XSD未得到解析/找到并因此被加载。要确保它不是您的JAXB版本(非常遥远的可能性),请从命令行调用xjc / wsimport并从那里获取...