xsd:keyref from hierarchical nodes structure
I try to refer with an xsd:keyref from within a node/subnode structure to a global table that is child of the xml root element.
Here is an example 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>
I also have an xsd defining the xsd:key and xsd:keyref fields within the document. These keys should verify that all keyref values are within the global table at the start of the xml document. Up to now I haven't figured out what the problem with the selector xpath expression could be.
<?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>
The Problem is that xmllint issues that "//SubNode" cannot be compiled
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
When I try the xpath expression with an xpath validator it selects all subnodes within the document as defined in the W3C standard, so why is this xpath not working within the selector expression?
I also tried .//SubNode. This compiles correctly but don't fail to validate if I enter a wrong keyref.
I like to share the solution I found.
The correct xsd is like this the namespace was missing:
<?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>
Thanks to anybody started to work on this.
精彩评论