gpt4 book ai didi

xml - 导入没有命名空间的 xsd

转载 作者:行者123 更新时间:2023-12-05 08:01:27 30 4
gpt4 key购买 nike

如标题所示,如何导入未定义 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>

文件:另一个.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)在没有 namespace 时无法识别 person 元素?

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

最佳答案

您遇到的问题与“导入[ing an] xsd with no namespace”无关。如所列,您的 XSD 非常好。问题很可能出在您的设置上,因为您的 person 对象的 XSD 未解析/找到,因此未加载。要确保它不是您的 JAXB 版本(可能性很小),请从命令行调用 xjc/wsimport 并从那里获取它...

关于xml - 导入没有命名空间的 xsd,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14200210/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com