我尝试使用xsd:keyref从节点/子节点结构中引用一个全局表,该表是xml根元素的子元素。
下面是一个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:key和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会出现"//SubNode“无法编译的问题
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,请不要失败。
发布于 2010-03-25 23:46:00
我喜欢分享我找到的解决方案。
正确的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>
感谢任何人开始在这方面的工作。
https://stackoverflow.com/questions/2516698
复制相似问题