关于分层节点结构中的xml:xsd:keyref

xsd:keyref from hierarchical nodes structure

我尝试使用xsd:keyref从节点/子节点结构中引用到xml根元素的子级的全局表。

这是一个示例xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="UTF-8"?>
<Root xmlns="http://www.example.org/keyTest"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.org/keyTest keyTest.xsd">

<Globals key="key1"/>  
<Globals key="key2"/>
<Globals key="key3"/>

<Node>
<SubNode keyref="key2"/>
<SubNode keyref="key3"/>    
<SubNode keyref="key1">
    <SubNode keyref="key2">
        <SubNode keyref="key1"/>
    </SubNode>  
</SubNode>      
</Node>
</Root>

我还有一个xsd,用于定义文档中的xsd:key和xsd:keyref字段。这些键应在xml文档开始时验证所有keyref值都在全局表内。到目前为止,我还没有弄清楚选择器xpath表达式可能存在的问题。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
        targetNamespace="http://www.example.org/keyTest"
        xmlns:tns="http://www.example.org/keyTest"
        elementFormDefault="qualified">

<complexType name="Global">
   
</complexType>

<complexType name="Node">
    <sequence maxOccurs="unbounded">
        <element name="SubNode" type="tns:Node" minOccurs="0"/>
    </sequence>
   
</complexType>

<complexType name="Root">
    <sequence>
        <element name="Globals" type="tns:Global" maxOccurs="unbounded"/>
        <element name="Node" type="tns:Node" maxOccurs="1"/>
    </sequence>
</complexType>

<element name="Root" type="tns:Root">
    <key name="key">
        <selector xpath="Global"/>
        <field xpath="@key"></field>
    </key>
    <keyref name="keyref" refer="tns:key">
        <selector xpath="//SubNode"/>
        <field xpath="@keyref"/>
    </keyref>
</element>

问题是无法编译" // SubNode "的xmllint问题

1
2
3
4
keyTest.xsd:30: element selector: Schemas parser error :
       Element '{http://www.w3.org/2001/XMLSchema}selector', at
       atribute 'xpath': The XPath expression '//SubNode' could not be compiled.
       WXS schema keyTest.xsd failed to compile

当我使用xpath验证器尝试xpath表达式时,它会按照W3C标准中的定义选择文档中的所有子节点,那么为什么这个xpath在选择器表达式中不起作用?

我也尝试过.//SubNode。这样可以正确编译,但是如果我输入了错误的keyref,也不会失败。


我想分享我找到的解决方案。

正确的xsd就像这样,缺少命名空间:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
        targetNamespace="http://www.example.org/keyTest"  
        xmlns:tns="http://www.example.org/keyTest"
        elementFormDefault="qualified">

<complexType name="Global">
   
</complexType>

<complexType name="Node">
    <sequence maxOccurs="unbounded">
        <element name="SubNode" type="tns:Node" minOccurs="0"/>
    </sequence>
   
</complexType>

<complexType name="Root">
    <sequence>
        <element name="Globals" type="tns:Global" maxOccurs="unbounded"/>
        <element name="Node" type="tns:Node" maxOccurs="1"/>
    </sequence>
</complexType>

<element name="Root" type="tns:Root">
    <key name="key">
        <selector xpath=".//tns:Globals"/>
        <field xpath="@key"></field>
    </key>
    <keyref name="keyref" refer="tns:key">
        <selector xpath=".//tns:SubNode"/>
        <field xpath="@keyref"/>
    </keyref>
    <unique name="uniqKey">
        <selector xpath=".//tns:Globals"/>
        <field xpath="@key"/>
    </unique>
</element>

感谢任何人开始为此工作。