xsd 相关问题

XSD通常是指以W3C XML Schema格式编写的文档,其中包含特定类型的XML文档的描述。

在 C# 中反序列化 XML 时出错

这是我的 C# 反序列化代码: 私人无效button1_Click(对象发送者,EventArgs e) { LandXML myObject; XmlSerializer mySerializer = 新的 XmlSerializer(典型...

回答 2 投票 0

GeoTools WFS-ng 插件 - WFS GetFeature 请求在解析响应时失败

我正在尝试从此在线 WFS 服务加载数据: https://inspire.skgeodesy.sk/eskn/rest/services/INSPIREWFS/kn_wfs_inspire/GeoDataServer/exts/InspireFeatureDownload/service?VERSION=2.0.0&S...

回答 1 投票 0

如何使用 CXF xjcplugin 生成 equals 和 hashCode

我正在尝试从 XSD 文件生成一个包含 toString、equals 和 hashCode 方法的 Java 类。我得到了 toString 工作,但我无法弄清楚如何将 CXF 插件获取到基因...

回答 4 投票 0

转换期间 Saxon 模式验证的问题

根据评论进行编辑,不使用图像。没有认为这对某些用户来说是负面的。 我们正在开发一个应用程序,在其中创建一些额外的 XML 文件(S1000D 数据模块)...

回答 1 投票 0

soap xml 响应 xsi:类型值已更改

我们的tomcat webservice响应是soap xml格式。 xsi:type 从 xsi:type="ax235:SearchOutput"> 更改为 xsi:type="ax239:SearchOutput"> 有一天突然。 ns 返回...

回答 1 投票 0

如何使用 Kotlin 在运行时从 ZIP 延迟加载多文件 XSD?

出现了一个需求,其中需要在运行时加载包含许多模式的 xsd.zip 文件。该 ZIP 文件将在类路径上可用,最重要的是,它包含许多带有 xsd 的模式:

回答 1 投票 0

如何使用 Kotlin 在运行时从 ZIP 加载多文件 XSD?

出现了一个需求,其中需要在运行时加载包含许多模式的 xsd.zip 文件。该 ZIP 文件将在类路径上可用,最重要的是,它包含许多带有 xsd 的模式:

回答 1 投票 0

Apache CXF XSD 方案改为 Java,更改输出类和工厂名称

我正在使用 CXF maven 插件根据 XSD 方案定义生成 Java 类。我使用两个 xsdOptions 标签将两种不同方案的 Java 文件创建到两个不同的包中。生成的...

回答 1 投票 0

cxf-xjc-plugin:如何为复杂类型添加@XMLRoot注解?

我有一个 XSD 架构,并且有一个“任何”元素 我有一个 XSD 架构,并且有一个“任何”元素 <xs:any minOccurs="0" maxOccurs="unbounded" namespace="http://the-namespace.org" processContents="lax"/> 这意味着我可以在这里添加“http://the-namespace.org”中定义的任何元素 这正是我想做的: return MyObject.builder() // ... .withAny(AnyObjectFromNamespace.builder().build()) .build(); 当我现在运行 xsdtojava 时,我收到错误: Caused by: com.sun.istack.SAXException2: Marshalling von Typ "AnyObjectFromNamespace" as element is not possible, because a @XmlRootElement-Annotation is missing 所以我现在的问题是如何添加 XmlRootElement 注释而不更改我的 xsd 架构?` 这是我的pom.xml: <dependencies> <dependency> <groupId>org.jvnet.jaxb2_commons</groupId> <artifactId>jaxb2-basics</artifactId> <version>1.11.1</version> </dependency> <dependency> <groupId>org.jvnet.jaxb2_commons</groupId> <artifactId>jaxb2-basics-runtime</artifactId> <version>1.11.1</version> </dependency> <!-- needed for builder plugin --> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> <version>2.3.3</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-core</artifactId> <version>2.3.0.1</version> </dependency> <!-- java time instead of xml gregorian calendar --> <dependency> <groupId>com.migesok</groupId> <artifactId>jaxb-java-time-adapters</artifactId> <version>1.1.3</version> </dependency> </dependencies> <build> <defaultGoal>install</defaultGoal> <plugins> <plugin> <groupId>org.apache.cxf</groupId> <artifactId>cxf-xjc-plugin</artifactId> <version>3.3.0</version> <configuration> <extensions> <!-- Default Values --> <extension>org.apache.cxf.xjcplugins:cxf-xjc-dv:3.3.0</extension> <!-- Equals, HashCode, ToString, etc. --> <extension>org.jvnet.jaxb2_commons:jaxb2-basics:1.11.1</extension> <!-- Builder, Immutable, etc. --> <extension>net.codesup.util:jaxb2-rich-contract-plugin:2.0.1</extension> </extensions> </configuration> <executions> <execution> <id>generate-sources</id> <phase>generate-sources</phase> <goals> <goal>xsdtojava</goal> </goals> <configuration> <sourceRoot>${project.build.directory}/generated-sources</sourceRoot> <xsdOptions> <xsdOption> <extension>true</extension> <xsd>${basedir}/src/main/resources/xsd/schema.xsd</xsd> <bindingFile>${basedir}/src/main/resources/xjb/binding.xjb</bindingFile> <extensionArgs> <!-- Enable Default Values Generation --> <extensionArg>-Xdv</extensionArg> <!-- Enable Equals Generation --> <extensionArg>-Xequals</extensionArg> <!-- Enable Hashcode Generation --> <extensionArg>-XhashCode</extensionArg> <!-- Enable ToString Generation --> <extensionArg>-XtoString</extensionArg> <!-- Enable Builder Generation --> <extensionArg>-Xfluent-builder</extensionArg> </extensionArgs> </xsdOption> </xsdOptions> </configuration> </execution> </executions> </plugin> </plugins> </build> XMLSchema 内容模型不允许 anyType 作为顶级 node。因此,anyType不能属于根元素,并且@XmlRootElement不会是神话野兽的有效Java注释。 Content Model : {http://www.w3.org/2001/XMLSchema}Schema ( (include | import | redefine | annotation)*, ( ( ( ( (simpleType | complexType | group | attributeGroup) ) | element | attribute | notation ) ), annotation* )* ) 实验 此 下载 (zip) 包含一个独立的 Maven 项目,该项目使用 cxf-xjc-plugin 为简单的 po.xsd 生成 Java (JAXB) 类。您可以用它来进行实验anyType。

回答 1 投票 0

XML 架构验证无法找到带有 xsd 的声明

我必须使用 xsd 文件验证 xml。 xsd 文件 companySendType.xsd 是: 我必须使用 xsd 文件验证 xml。 xsd 文件 companySendType.xsd 是: <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:CC="http://test.com" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://test.com" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xs:include schemaLocation="companyType.xsd"/> <xs:element name="companySendType" > <xs:complexType> ... </xs:complexType> </xs:element> </xs:schema> Java代码: SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = sf.newSchema(new Source[]{ new StreamSource(ClassLoader.getSystemClassLoader().getResourceAsStream("companyType.xsd")), new StreamSource(ClassLoader.getSystemClassLoader().getResourceAsStream("companySendType.xsd")) }); Validator validator = schema.newValidator(); StringReader stringReader = new StringReader(xmlToValidate); StreamSource streamSource = new StreamSource(stringReader); validator.validate(streamSource); 我得到的错误是: 找不到元素“CC:companySendType”的声明 xml 是: <?xml version="1.0" encoding="UTF-8"?> <CC:companySendType version="1.0" xsi:schemaLocation="http://test.com companySendTypev1.18.xsd" xmlns:CC="http://test.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> </CC:companySendType> 我不明白为什么它不起作用,xml文件似乎没问题。 我检查了你的xsd,看来你还没有声明元素的类型CC:companySendType。您可以使用一些默认类型,例如: <xs:element name="companySendType" type="xs:string"/> 或者如果您有一个复杂类型,您应该像下面的示例一样定义它: <xs:element name="companySendType"> <xs:complexType> <xs:sequence> <xs:element name="x1" type="xs:string"/> <xs:element name="x2" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element>

回答 1 投票 0

如何配置 XSD 架构以在嵌套元素上生成 xmlns 属性?

我创建了这个 XSD 文件并将其命名为 Envelope.xsd... ...

回答 1 投票 0

如何为 XML DSIG DTO 设置 Java 数字签名

我需要通过 REST 发送签名的 XML 消息。 XSD 包含架构 https://www.w3.org/TR/2002/REC-xmldsig-core-20020212/xmldsig-core-schema.xsd。我能够成功基因...

回答 1 投票 0

Apache Camel:无法找到 xsd 文件异常

我有以下 XML 文件: 我有以下 XML 文件: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:context="http://www.springframework.org/schema/context" xmlns:cxf="http://cxf.apache.org/core" xmlns:camel="http://camel.apache.org/schema/spring" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring-2.9.0.xsd "> 每当我启动网络应用程序时,都会出现以下错误: Apr 19, 2012 3:10:59 PM org.springframework.util.xml.SimpleSaxErrorHandler warning WARNING: Ignored XML validation warning org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema document 'http://camel.apache.org/schema/spring/camel-spring-2.9.0.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>. at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source) at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.warning(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source) at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.reportSchemaErr(Unknown Source) at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.reportSchemaWarning(Unknown Source) at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.getSchemaDocument(Unknown Source) at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.parseSchema(Unknown Source) at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadSchema(Unknown Source) at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.findSchemaGrammar(Unknown Source) at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source) at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source) at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source) at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:75) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:388) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149) at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:124) at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:93) at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130) at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:467) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:397) at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:282) at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:204) at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4323) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:4780) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:139) at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:988) at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:771) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:139) at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:988) at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:275) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:139) at org.apache.catalina.core.StandardService.startInternal(StandardService.java:427) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:139) at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:649) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:139) at org.apache.catalina.startup.Catalina.start(Catalina.java:585) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288) at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:415) Apr 19, 2012 3:10:59 PM org.springframework.web.context.ContextLoader initWebApplicationContext SEVERE: Context initialization failed org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 81 in XML document from ServletContext resource [/WEB-INF/Beans.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'camelContext'. at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:396) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302) at ..... 我不明白为什么会发生这种情况。另外,如果这是由于某些互联网连接问题造成的,为什么我没有得到 cxf xsd 异常? 第一个解释不相关,因为 .xsd 存在于此处: http://camel.apache.org/schema/spring/camel-spring-2.9.0.xsd 检查您的 XML 文档。还有一个问题。 发布整个堆栈跟踪是彻底的,但没有必要。 XSD 在依赖项中提供。您应该验证您的类路径,您需要版本 2.9。您还可以使用通用位置来获取可用的位置: http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd 对于那些从 3.19.x 迁移到 3.22.x 的人来说,这是一个救星。 这解决了camel-config.xml中丢失的xsd文件 camel-spring.xsd 在camel-spring-3.10.0.jar 中丢失

回答 3 投票 0

如何使用 JAXB 从 xsd 生成实现 Serialized 接口的 Java 类?

我想将缓存引入到现有的 Spring 项目中,该项目使用 JAXB 来公开 WebServices。缓存将在端点级别完成。为了做到这一点,从...生成的类

回答 7 投票 0

如何从xsd生成实现可序列化的java文件

我必须生成一个也实现可序列化接口的类,以下是我正在使用的xsd文件,也使用maven jaxb2插件 这是我的 .xsd 文件 我必须生成一个也实现可序列化接口的类,以下是我正在使用的xsd文件,并且还使用maven jaxb2插件 这是我的 .xsd 文件 <?xml version="1.0" encoding="windows-1252" ?> <xsd:complexType name="Status"> <xsd:sequence> <xsd:element name="startTime" type="xsd:double" minOccurs="0" /> <xsd:element name="endTime" type="xsd:double" minOccurs="0" /> </xsd:sequence> </xsd:complexType> 我的JAXB2 plugin <plugin> <groupId>org.jvnet.jaxb2.maven2</groupId> <artifactId>maven-jaxb2-plugin</artifactId> <version>0.8.3</version> <executions> <execution> <id>status</id> <phase>generate-sources</phase> <goals> <goal>generate</goal> </goals> <configuration> <forceRegenerate>true</forceRegenerate> <schemaDirectory>src/main/resources/xsd</schemaDirectory> <schemaIncludes> <include>Status.xsd</include> </schemaIncludes> <generatePackage>com.test.model</generatePackage> <args> <arg>-XtoString</arg> <arg>-Xcopyable</arg> <arg>-Xequals</arg> </args> <plugins> <plugin> <groupId>org.jvnet.jaxb2_commons</groupId> <artifactId>jaxb2-basics</artifactId> <version>0.6.4</version> </plugin> </plugins> </configuration> </execution> </executions> <dependencies> <dependency> <groupId>com.datastax.cassandra</groupId> <artifactId>cassandra-driver-core</artifactId> <version>3.1.3</version> </dependency> <dependency> <groupId>com.datastax.cassandra</groupId> <artifactId>cassandra-driver-mapping</artifactId> <version>3.1.3</version> </dependency> </dependencies> </plugin> 这里生成的java类实现了Cloneable, CopyTo, Equals, ToString interfaces我尝试了<arg>-Xserializable</arg>但是它不起作用,我需要这个生成的类也应该实现Serializable interface 我像这样使用了 jaxb2-maven-plugin: <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>jaxb2-maven-plugin</artifactId> <executions> <execution> <id>xjc</id> <goals> <goal>xjc</goal> </goals> </execution> </executions> <configuration> <extension>true</extension> <arguments>-XtoString -Xcopyable</arguments> <outputDirectory>target/generated-sources/</outputDirectory> <schemaDirectory>src/main/resources</schemaDirectory> <bindingDirectory>src/main/resources</bindingDirectory> <bindingFiles>binding.xml</bindingFiles> </configuration> </plugin> 绑定.xml: <jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" jaxb:extensionBindingPrefixes="xjc" jaxb:version="1.0"> <jaxb:globalBindings generateIsSetMethod="true"> <xjc:serializable uid="12343" /> </jaxb:globalBindings> 有了这种依赖性: <dependency> <groupId>org.jvnet.jaxb2_commons</groupId> <artifactId>jaxb2-basics</artifactId> </dependency> 我找到了解决方案 已修改xsd:schema declaration to <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" jaxb:extensionBindingPrefixes="xjc" jaxb:version="1.0"> <xsd:annotation> <xsd:appinfo> <jaxb:globalBindings generateIsSetMethod="true"> <xjc:serializable uid="12343"/> </jaxb:globalBindings> </xsd:appinfo> </xsd:annotation> 然后在jaxp.properties下添加path/to/jdk1.8.0/jre/lib(如果不存在),然后在其中写入以下行: javax.xml.accessExternalSchema = all 对我有用的是像这样配置 Maven 插件: <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>jaxb2-maven-plugin</artifactId> <version>2.5.0</version> <executions> <execution> ... </execution> <execution> <id>schema_variations-scopes</id> <goals> <goal>xjc</goal> </goals> <configuration> ... <xjbSources> <xjbSource>src/main/resources/xjc/bindings.xjb</xjbSource> </xjbSources> </configuration> </execution> </executions> </plugin> 并将绑定.xml文件放置在路径下:src/main/resources/xjc/bindings.xjb <jxb:bindings version="1.0" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <jxb:globalBindings generateIsSetMethod="true"> <xjc:serializable uid="12343"/> </jxb:globalBindings> </jxb:bindings>

回答 3 投票 0

Python zeep xsi:type="xsd:string"

我正在尝试制作正确的文档,但它不起作用。我需要将“value”包装在 xsi:type="xsd:string"tag 中 请告诉我该怎么做) 谢谢 ... 从 zeep 导入客户端,

回答 1 投票 0

转换 UBL 发票以符合 xrechnung-semantic-model.xsd

我正忙着使我的发票适应 XRechnung 标准。现在我正在尝试应用 https://github.com/itplr-kosit 下提供的公共可视化工具 关于可视化工具,在 https://github...

回答 1 投票 0

XBRL 表格链接被阻止的单元格

我应该修复什么才能使所有单元格打开进行编辑?现在我无法填写表格,因为单元格被阻塞。 我认为表格的某些单元格被遮挡,这个问题与超立方体有关:

回答 1 投票 0

XSD 到 XML 转换器

我正在尝试将 XSD 文件转换为其相应的 XML 文档。使用 IntelliJ 的内置工具使这一切变得简单,但有些问题。它似乎正在正确地创造一切,而且这......

回答 3 投票 0

有没有办法强制/保留 XML 模式中 XML 元素的顺序?

让我们考虑以下 XML 模式: 让我们考虑以下 XML 架构: <?xml version="1.0" encoding="UTF-8"?> <schema targetNamespace="http://www.example.org/library" elementFormDefault="qualified" xmlns="http://www.w3.org/2001/XMLSchema" xmlns:lib="http://www.example.org/library"> <element name="library" type="lib:libraryType"></element> <complexType name="libraryType"> <sequence> <element name="books" type="lib:booksType"></element> </sequence> </complexType> <complexType name="booksType"> <sequence> <element name="book" type="lib:bookType" maxOccurs="unbounded" minOccurs="1"></element> </sequence> </complexType> <complexType name="bookType"> <attribute name="title" type="string"></attribute> </complexType> </schema> 以及相应的 XML 示例: <?xml version="1.0" encoding="UTF-8"?> <lib:library xmlns:lib="http://www.example.org/library" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.org/library src/library.xsd "> <lib:books> <lib:book title="t1"/> <lib:book title="t2"/> <lib:book title="t3"/> </lib:books> </lib:library> 有没有办法保证 <lib:book .../> 元素的顺序被保留?我想确保任何读取 XML 的解析器都会按指定的顺序返回书籍,即首先是带有 title="t1" 的书,然后是带有 title="t2" 的书,最后是带有 title="t3" 的书。 据我所知,XML 解析器不需要保留顺序。我想知道是否可以通过 XML Schema 强制执行这一点?对我来说,一个快速的解决方案是向 index 元素添加 <lib:book .../> 属性,并将订单保存委托给读取 XML 的应用程序。 评论?有建议吗? 根据 XML 世界中的重要人物 Michael Kay 的说法,顺序是保留的。 是的,您可以假设元素的顺序将被保留。 XML 规范的作者没有明确指出这一点,但那是因为他们认为这是显而易见的。不保留顺序的解析器可能在技术上是一致的,但没有人会使用它;很多很多 XML 应用程序都依赖于保留顺序,尤其是那些使用 XML 表示文档的应用程序。 来源:http://lists.xml.org/archives/xml-dev/201003/msg00045.html 正如上面评论中提到的, xs:sequence 定义一个 ORDERED 集合。证据如下: <xsd:complexType name="USAddress"> <xsd:sequence> <xsd:element name="name" type="xsd:string"/> <xsd:element name="street" type="xsd:string"/> <xsd:element name="city" type="xsd:string"/> <xsd:element name="state" type="xsd:string"/> <xsd:element name="zip" type="xsd:decimal"/> </xsd:sequence> <xsd:attribute name="country" type="xsd:NMTOKEN" fixed="US"/> </xsd:complexType> ...这些元素必须称为 name、street、city、state 和 zip,由声明的 name 属性的值指定,并且这些元素必须按照声明的顺序(顺序)出现。 来源:W3C:XML 架构第 0 部分 我知道这已经很老了,但尽管如此,我也在寻找一种方法来保证 XML 元素的保留顺序。 虽然当前所有解析器可能会保留顺序,但 XML 定义决不需要这样做,并且未来的解析器可能会完全不同地处理顺序。 目前存在的最佳解决方案似乎是为元素赋予“id”属性,以便在解析后可以在代码中验证顺序。 当 XML 文件中的顺序已经很明显时,我讨厌必须要求我的用户添加任意 id="1"、id="2" 等,但据我所知,XML 中没有官方标准用于要求重复元素的特定顺序。 更新 如果您使用 .NET,它有一个属性,您可以设置它来定义读取这些元素的顺序。因此,我想只要您知道您的 XML 将在哪里进行解析,并且解析器支持强制执行元素按照您要求的方式订购,然后订单就可以执行。 但是,如果第三方使用他们自己的解析器获取并尝试复制您的结果,则可能会发生混乱,因为该顺序是在其他地方配置的,而不是在 XML 中,因此没有其他解析器实现知道如何利用该顺序。 因此,这一切都可以总结为:(a) 顺序不能在 XML 规范本身内强制执行,但是 (b) 顺序可以并且经常由大量 XML 解析器强制执行。 元素的顺序(与属性不同)在 XML 中很重要,我知道的每个解析器都会保留它。 根据链接: 当订单很重要时: “当您使用 xs:sequence 指标时,您是在说元素的顺序很重要,并且它应该遵循指定的顺序。” 当订单不重要时: “当您使用 xs:all 指示器时,您是在说元素的顺序并不重要。”

回答 5 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.