XML 架构.处理命名空间感知的属性值 [英] XML Schema. Processing namespace-aware attribute values

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

问题描述

我的项目的一部分涉及用户定义架构文档的大量转换.我需要能够更改目标命名空间、支持类型交叉引用并在多个架构文档上构建 wsdl.最常用的操作之一是在将架构导入 wsdl 文件之前更改命名空间前缀.我正在使用 org.xml.sax.ContentHandler 和它的 startPrefixMapping 方法来处理命名空间.除非我想更改元素类型,否则一切正常且完美无缺.

Part of my project involves heavy transformations of user-defined schema documents. I need to be able to change target namespaces, support type cross-references and build wsdl upon multiple schema documents. One of the most commonly used operations is changing namespace prefix prior to importing schema into wsdl file. I'm using org.xml.sax.ContentHandler and it's startPrefixMapping method to handle namespaces. All works fine and flawless unless I want to change element types.

这里是简单的架构片段

<schema xmlns="http://www.w3.org/2001/XMLSchema">
  <complexType name="Param">
    <sequence>
      <element name="key" type="string"/>
      <element name="value" type="string"/>
    </sequence>
  </complexType>
<!-- omitted -->

必须按如下方式放置在 wsdl 中:

which must be placed inside wsdl as follows:

<definitions xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <types>
    <xs:schema>
      <xs:complexType name="Param">
        <xs:sequence>
          <xs:element name="key" type="xs:string"/> <!-- 'xs:' to be added -->
          <xs:element name="value" type="xs:string"/> <!-- 'xs:' to be added -->
        </xs:sequence>
      </xs:complexType>
<!-- omitted -->

问题是某些模式的属性值( 中的type) 是命名空间感知的,并且在上面的示例中可能会发生变化.正如我所见,DOM 和 SAX 解析器都无法处理这种情况,因此我目前正在使用丑陋的字符串操作来从特定属性值中检索名称空间信息.

The problem is that some schema's attribute values (type in <element>, base in <extension>) are namespace-aware and are subject to change in the above example. As I can see, neither DOM, nor SAX parsers are able to handle this situation so I'm currently using ugly string operations to retrieve namespace info from particular attribute values.

我错过了什么吗?是否有任何 API、库或其他特定于架构的工具来处理此类任务?

Am I missing somethiing? Are there any api's, libraries or other schema-specific tools to handle such sort of tasks?

推荐答案

是的,这是 xml schema/wsdl 中的一个主要错误.在属性值中使用 xml 命名空间前缀是一个巨大的错误(因为前缀值本身并不重要,只有实际命名空间的占位符).不幸的是,我不知道该问题有什么好的解决方案(我不得不实现几乎相同的想法:将多个用户定义的模式合并到一个模式/wsdl 中).我知道 xerces 有一个 xml 模式模型 api,但是我不知道它是否支持将多个模式合并在一起并生成结果输出.

yes, this is a major bug in xml schema/wsdl. utilizing the xml namespace prefixes inside of attribute values was a giant mistake (since the prefix values themselves are not significant, only placeholders for the actual namespaces). unfortunately, i don't know of any good solution to the problem (i've had to implement virtually the same sort of idea: merging multiple user defined schemas into one schema/wsdl). i do know that xerces has an xml schema model api, but i don't know if it has support for merging together multiple schemas and generating the resulting output.

这篇关于XML 架构.处理命名空间感知的属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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