我可以在 XML Schema 元素名称中使用正则表达式吗? [英] Can I use regular expressions in XML Schema element names?

查看:36
本文介绍了我可以在 XML Schema 元素名称中使用正则表达式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为传递给我的一段代码生成的 XML 创建一个 XML 模式.我将描述我的问题的简化版本.假设此代码生成的 XML 文件描述了一个文本文档;它看起来像这样:

I am trying to create an XML Schema for the XML produced by a piece of code I was passed. I'll describe a simplified version of my problem. Let's say an XML file produced by this code describes a text document; it would look like this:

<document>
  <r1>A line of text</r1>
  <r2 style="bold">Another line which is bold</r2>
  <r3>Yet another line</r3>
</document>

等等.我知道这不是最好的设计 - 如果行号是属性会更好,但这就是我必须使用的.它代表了问题所在的行号.有没有办法编写一个架构,让我为元素名称指定一个正则表达式(或类似的)?我希望 XSD 文件看起来像这样:

And so on. Not the best design, I know - it would be better if the row numbers were attributes, but that's what I have to work with. And it's representing the row numbers that is the problem. Is there some way to write a Schema that lets me specify a regular expression (or similar) for an element name? I expect the XSD file would look somehow like this:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
        targetNamespace="http://www.example.org/SimpleSchema" 
        xmlns:tns="http://www.example.org/SimpleSchema" 
        elementFormDefault="qualified">
    <xs:element name="document">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="rX" minOccurs="1" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:simpleContent>
                            <xs:extension base="xs:string">
                                <xs:attribute name="style" type="xs:string" />
                            </xs:extension>
                        </xs:simpleContent>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
 </xs:schema>

...其中第 9 行的rX"是表示一个以 r 开头并以 X 结尾的名称,这是一个整数"所需的任何表达式.

...where "rX" on line 9 is whatever expression is needed to mean "a name starting with r and ending with X, which is an integer".

我宁愿避免自己修复生成代码,所以我想看看是否可以先编写一个合适的 XML 模式.在此先感谢大家.

I'd rather avoid having to fix the generating code myself, so I'd like to see if it is possible to write a suitable XML Schema first. Thanks in advance to everybody.

推荐答案

XSD 要求按字面指定元素名称;不支持我认为您想到的那种声明.

XSD requires that element names be specified literally; the kind of declaration I think you have in mind is not supported.

正如您所描述的(一个以 r 开头并以 X 结尾的名称,这是一个整数),您要编写的声明将在模式中产生无限数量的元素组件;我所知道的唯一支持这种事情的语法形式是 Aard van Wijngaarden 为 Algol 68 开发的两级语法.

As you have described it (a name starting with r and ending with X, which is an integer), the declaration you would like to write would produce an infinite number of element components in the schema; the only grammatical formalism I know that supports this kind of thing is the two-level grammar developed by Aard van Wijngaarden for Algol 68.

因此,在短期内,您最好的选择似乎是更改生成代码,或者为所需声明发明自己的符号并从中生成合法的 XSD 架构文档.

So in the short run, your best options appear to be either to change the generating code, or to invent your own notation for your desired declaration and generate a legal XSD schema document from it.

这篇关于我可以在 XML Schema 元素名称中使用正则表达式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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