如何允许 XML 元素通过 XSD 出现在任何地方? [英] How to allow an XML element to appear anywhere via XSD?

查看:23
本文介绍了如何允许 XML 元素通过 XSD 出现在任何地方?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 XML:

<root>
  <someNode>
    <someChild attr="val" />
  </someNode>

  <otherNode>
    <otherChild>
      <otherSubChild />
    </otherChild>
  </otherNode>
</root>

一个有效的 XML 模式将非常容易.现在的问题是,我的程序必须支持在解析文件时评估的特殊元素,并导致元素的条件替换.

A valid XML Schema for this will be very easy. Now the problem is, my program must support a special element which is evaluated when parsing the file, and leads to conditional substitution of elements.

示例:

    <root>
      <someNode>
        <if environment="DEV">
          <someChild attr="val" />
        </if>
        <if environment="PROD">
          <someChild attr="otherVal" />
        </if>
      </someNode>

      <otherNode>
        <otherChild>
          <if environment="DEV">
            <otherSubChild />
          </if>
        </otherChild>
      </otherNode>
    </root>

我想你明白了.问题是,允许 if 元素出现在 XML 中的任何地方,但同时允许包含允许的任何元素对于放入的元素.

I think you get the idea. The thing is, the if element is allowed to appear everywhere in the XML, but at the same time allowed to include any element which is allowed for the element it is put in.

我将如何以一种好的方式定义它?目前,我的 XML 架构中有大约 90 种类型/元素,手动编辑每个类型/元素以允许允许正确子元素的 if 元素真的很麻烦.

How would I define this in a good way? Currently I am having about 90 types/elements in my XML schema, and it would be really cumbersome to manually edit every single type/element to allow an if element which allows the correct child elements.

是否可以创建一个允许如上所述的 if 元素的架构?

Is it possible to make a schema which allows the if element as I described above?

推荐答案

因此,您已经指定了内容模型的层次结构,具体说明了哪些元素可以出现在哪些其他元素中,以多少数量以及可能以什么顺序出现.现在您要添加,顺便说一下,if 元素可以在任何地方包装任何现有元素?

So, you've specified a hierarchy of content models that say specifically what elements can appear in what other elements, in what quantities and possibly in what orders as well. And now you want to add, By the way, an if element can wrap any existing element anywhere?

您不能在 XSD 中做到这一点.您无法在不修改这些特定指令的情况下以这种方式全局对抗所有特定约束,或者没有预先为这种可能性做出规定.(例如,您是否定义了一个元素替换组,它可能会在整个过程中出现多个位置您的文档,您可以轻松地向群组添加新成员.)

You can't do that in XSD. You can't counter all of your specific constraints globally in that manner without modifying those specific instructions, or without having made provisions for such a possibility a priori. (For example, had you defined an element substitution group, that could appear multiple places throughout your document, you could easily add a new member to the group.)

有一种方法可以说任何元素都可以出现在内容模型中的给定位置 (xs:any),但没有办法说给定元素可以出现在所有元素的任何位置内容模型.在 XSD 1.1 中,有一种新机制允​​许打开内容在内容模型中,但这也允许前瞻性规定,而不是追溯性的全局调整.

There's a way to say that any element can appear at a given spot within a content model (xs:any), but there is no way to say that a given element can appear anywhere in all content models. In XSD 1.1, there is a new mechanism to allow open content in content models, but that too allows for forward looking provisions, not retroactive global adjustments.

一个建议可能是在 XML 的 with-if 和 without-if 形式之间编写一个简单的 XSLT 转换,并分别验证这些文档.

One suggestion might be to write a simple XSLT transformation between the with-if and without-if forms of your XML and separately validate those documents.

另一种方法是将您的条件表示为将出现在任何元素上的属性,而不是可以包装任何其他元素的元素.这仍然需要更新您的 XSD,但在保留现有内容模型的同时更容易进行更新.

Another approach would be to express your condition as an attribute that would appear on any element, rather than an element that can wrap any other element. This would still require updates to your XSD, but the updates would be easier to make while preserving your existing content models.

这篇关于如何允许 XML 元素通过 XSD 出现在任何地方?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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