用于在元素和#PCDATA 之间进行选择的 XML 架构 [英] XML Schema for choice between element and #PCDATA

查看:19
本文介绍了用于在元素和#PCDATA 之间进行选择的 XML 架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个预先存在的 XML 文档类型,它有一个元素可以有两种内容类型:一些元素,或者只是文本.将其建模为混合内容是过度的,因此 JAXB 的 XJC 会创建一个非常丑陋的绑定.

I have a preexisting XML document type that has an element that can have two content types: some elements, or just text. Modeling this as mixed content is overkill, and JAXB's XJC creates a very ugly binding as a result.

<bars><bar .../><bar .../></bars>

对比

<bars>Just a bunch of #PCDATA</bars>

xs:choice 似乎只针对复杂类型(而不是像 xs:string 这样的简单类型)结构化.有没有办法使用 XML 模式在元素或文本之间表达这种选择?在 DTD 中,这将类似于

xs:choice seems structured only for complex types (not simple types like xs:string). Is there a way to express this choice, between elements or text, using XML schema? In DTD this would be something like

<!ELEMENT bars (#PCDATA | bar*)>

推荐答案

您要定义的语言(字符序列或 bar 元素序列,但不是混合)不能是在 XSD 1.0 中定义(或在 XML DTD 中;您的 DTD 表示法有意义,但在 XML DTD 中不合法).

The language you want to define (either a sequence of character or a sequence of bar elements, but not a mixture) cannot be defined in XSD 1.0 (or in XML DTDs, either; your DTD notation would make sense but is not legal in XML DTDs).

在 XSD 1.1 中,您可以使用断言来确保如果任何 bar 元素作为子元素存在,则不会出现文本节点(或仅包含仅包含空格的文本节点).

In XSD 1.1, you can use an assertion to ensure that if any bar elements are present as children, no text nodes occur (or only text nodes that contain only whitespace).

实现大致相同效果的一种简单方法是说 bars 元素包含一系列 bar 元素或单个 stringvalue 元素(随心所欲地称呼它),其中 stringvalue 元素包含——顾名思义——只是一个字符串.

A simple way to achieve roughly the same effect is to say that the bars element contains either a sequence of bar elements or a single stringvalue element (call it whatever you like), where the stringvalue element contains -- as its name suggests -- just a string of characters.

这篇关于用于在元素和#PCDATA 之间进行选择的 XML 架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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