Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我可以在XML模式元素名称中使用正则表达式吗?_Xml_Xsd - Fatal编程技术网

我可以在XML模式元素名称中使用正则表达式吗?

我可以在XML模式元素名称中使用正则表达式吗?,xml,xsd,Xml,Xsd,我正在尝试为传递的一段代码生成的XML创建一个XML模式。我将描述我的问题的简化版本。假设由该代码生成的XML文件描述了一个文本文档;它看起来是这样的: <document> <r1>A line of text</r1> <r2 style="bold">Another line which is bold</r2> <r3>Yet another line</r3> </document&g

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

<document>
  <r1>A line of text</r1>
  <r2 style="bold">Another line which is bold</r2>
  <r3>Yet another line</r3>
</document>
<?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>

一行文字
另一行是粗体的
还有一行
等等。我知道这不是最好的设计——如果行号是属性会更好,但这是我必须处理的。它代表的是行号,这就是问题所在。有没有办法编写一个模式,让我为元素名指定一个正则表达式(或类似的)呢?我希望XSD文件看起来会像这样:

<document>
  <r1>A line of text</r1>
  <r2 style="bold">Another line which is bold</r2>
  <r3>Yet another line</r3>
</document>
<?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结尾的名称,这是一个整数”所需的任何表达式


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

我对XSD文件的正则表达式功能不太熟悉,但表达式本身相当简单

这将捕获“rX”行号

<([Rr][0-9]{1,})>

XSD要求元素名称按字面形式指定;我认为你心目中的那种声明是不受支持的

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


因此,在短期内,您的最佳选择似乎是要么更改生成代码,要么为所需声明发明自己的符号,并从中生成合法的XSD模式文档。

在XSD 1.1中,您可以使用xs:any来允许任何名称的元素,然后使用断言将名称限制为与正则表达式匹配的名称:

    <xs:complexType>
        <xs:sequence>
            <xs:any minOccurs="1" maxOccurs="unbounded"/>
        </xs:sequence>
        <xs:assertion test="every $x in * 
                            satisfies matches(local-name($x), '[Rr][0-9]+')"/>
    </xs:complexType> 


XSD 1.1目前在Xerces(beta版)和Saxon(9.4版)中实现。

很有趣,但我不确定是否可以使用XSD 1.1。不幸的是,关于使用什么工具和库,我的手被束缚住了。我会查一查的,我想我最终会这么做的。