2010-03-25 123 views
0

keyref我尝试用一​​个xsd指:从一个节点/子节点结构中keyref全局表在XML根元素的孩子。XSD:从层次结构的节点

下面是一个例子的XML

<?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:键和xsd:在文档中keyref字段。这些键应检查所有keyref值是全球表中的XML文档的开始。到目前为止,我还没有弄清楚选择器xpath表达式的问题可能是什么。

<?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"> 
    <attribute name="key" type="string"/> 
</complexType> 

<complexType name="Node" > 
    <sequence maxOccurs="unbounded"> 
     <element name="SubNode" type="tns:Node" minOccurs="0"/> 
    </sequence> 
    <attribute name="keyref" type="string"/> 
</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> 

问题是xmllint问题是“//子节点”不能编译

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验证。

回答

2

我想分享我找到了解决办法。

正确的XSD是这样的命名空间失踪:

<?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"> 
    <attribute name="key" type="string"/> 
</complexType> 

<complexType name="Node" > 
    <sequence maxOccurs="unbounded"> 
     <element name="SubNode" type="tns:Node" minOccurs="0"/> 
    </sequence> 
    <attribute name="keyref" type="string"/> 
</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> 

感谢任何人开始在这方面努力。