2010-03-25 119 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> 

感謝任何人開始在這方面努力。