首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >xsd:来自分层节点结构的keyref

xsd:来自分层节点结构的keyref
EN

Stack Overflow用户
提问于 2010-03-25 23:11:50
回答 1查看 1.2K关注 0票数 0

我尝试使用xsd:keyref从节点/子节点结构中引用一个全局表,该表是xml根元素的子元素。

下面是一个xml示例

代码语言:javascript
复制
<?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表达式可能存在的问题。

代码语言:javascript
复制
<?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“无法编译的问题

代码语言:javascript
复制
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,请不要失败。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-03-25 23:46:00

我喜欢分享我找到的解决方案。

正确的xsd如下所示:缺少名称空间:

代码语言:javascript
复制
<?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>

感谢任何人开始在这方面的工作。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2516698

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档