XML Schema keyref 问题,可能是递归元素的问题
好吧,我已经用头撞了这么久了,我想我已经严重损坏了我的大脑,以至于我忘记了莎士比亚。没关系,我不太用他。这是我的问题。
我在 xml 文档顶部有一个水果列表。可以说,这就是“查找表”。
然后我在下面构建了一个菜单系统。每个菜单可以有更多菜单或产品列表。这些产品必须与顶部的水果相对应。
我已经能够确认正在填充水果列表,甚至可以获取 keyref 来验证一级菜单。但我无法让它验证整个菜单树。我已用 标记了我尝试运行 use keyref 的位置。
我正在使用 xmllint 进行验证。如果我在其中执行任何带有 // 的 xpath,我会得到“无法编译”。
这是示例 xml:
<Snarf xmlns="" xsi:noNamespaceSchemaLocation="mySchema.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Fruit name="apple" />
<Fruit name="orange" />
<Fruit name="watermelon" />
<Blarg>
<Menu name="Top Menu">
<Menu name="Skillz">
<Menu name="Juizy">
<Product>orange</Product>
<Product>watermelon</Product>
</Menu>
<Menu name="nutty">
<Product>orange</Product>
</Menu>
</Menu>
<Menu name="Applz">
<Product>apple</Product>
</Menu>
</Menu>
</Blarg>
</Snarf>
这是 xsd:
<xs:complexType name="productmenu">
<xs:choice minOccurs="1" maxOccurs="1">
<xs:choice minOccurs="1" maxOccurs="unbounded">
<xs:element name="Menu" type="productmenu">
<!-- KEY FAIL -->
<!-- <xs:keyref name="subMenuProductRef" refer="productNumber">
<xs:selector xpath="Menu"/>
<xs:field xpath="Product"/>
</xs:keyref> -->
</xs:element>
</xs:choice>
<xs:choice minOccurs="1" maxOccurs="unbounded">
<xs:element name="Product" type="xs:integer"
minOccurs="1" maxOccurs="unbounded"/>
</xs:choice>
</xs:choice>
<xs:attribute name="name" type="xs:string" use="required" />
</xs:complexType>
<xs:element name="Snarf">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Fruit" maxOccurs="unbounded" minOccurs="1" type="string"/>
<xs:element name="Blarg" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="Menu" type="productmenu"
maxOccurs="unbounded" minOccurs="1">
<!-- KEY FAIL -->
<!-- <xs:keyref name="subMenuProductRef" refer="productNumber">
<xs:selector xpath="Menu"/>
<xs:field xpath="Product"/>
</xs:keyref>-->
</xs:element>
</xs:sequence>
<xs:attribute name="name" type="xs:string" use="required" />
</xs:complexType>
<!-- KEY FAIL -->
<!-- <xs:keyref name="menuProductRef" refer="productNumber">
<xs:selector xpath="Menu"/>
<xs:field xpath="Product"/>
</xs:keyref> -->
</xs:element>
</xs:choice>
</xs:complexType>
<xs:key name="productNumber">
<xs:selector xpath="./Product"/>
<xs:field xpath="@num"/>
</xs:key>
<!-- KEY FAIL -->
<!-- <xs:keyref name="menuProductRef" refer="productNumber">
<xs:selector xpath="./Blarg/Menu"/>
<xs:field xpath="Product"/>
</xs:keyref> -->
</xs:element>
Ok I've been banging my head against this one so long, I think I've damaged my brain so much I forgot Shakespeare. It's ok, I don't use him that much. Here's my problem.
I have a list of fruit at the top of an xml document. This is the "lookup table" so to speak.
I then build a menu system below. Each menu can have more menus, or a list of products. These products must correspond to the fruit at the top.
I have been able to confirm that the list of fruit is being populated, and can even get the keyref to validate one level of the menu. But I cannot get it to validate the whole menu tree. I have marked the locations I tried to run use keyref with <!-- KEY FAIL -->
.
I'm using xmllint to validate. If I do any xpath's with // in them, I get "Cannot compile".
Here is the example xml:
<Snarf xmlns="" xsi:noNamespaceSchemaLocation="mySchema.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Fruit name="apple" />
<Fruit name="orange" />
<Fruit name="watermelon" />
<Blarg>
<Menu name="Top Menu">
<Menu name="Skillz">
<Menu name="Juizy">
<Product>orange</Product>
<Product>watermelon</Product>
</Menu>
<Menu name="nutty">
<Product>orange</Product>
</Menu>
</Menu>
<Menu name="Applz">
<Product>apple</Product>
</Menu>
</Menu>
</Blarg>
</Snarf>
Here is the xsd:
<xs:complexType name="productmenu">
<xs:choice minOccurs="1" maxOccurs="1">
<xs:choice minOccurs="1" maxOccurs="unbounded">
<xs:element name="Menu" type="productmenu">
<!-- KEY FAIL -->
<!-- <xs:keyref name="subMenuProductRef" refer="productNumber">
<xs:selector xpath="Menu"/>
<xs:field xpath="Product"/>
</xs:keyref> -->
</xs:element>
</xs:choice>
<xs:choice minOccurs="1" maxOccurs="unbounded">
<xs:element name="Product" type="xs:integer"
minOccurs="1" maxOccurs="unbounded"/>
</xs:choice>
</xs:choice>
<xs:attribute name="name" type="xs:string" use="required" />
</xs:complexType>
<xs:element name="Snarf">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Fruit" maxOccurs="unbounded" minOccurs="1" type="string"/>
<xs:element name="Blarg" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="Menu" type="productmenu"
maxOccurs="unbounded" minOccurs="1">
<!-- KEY FAIL -->
<!-- <xs:keyref name="subMenuProductRef" refer="productNumber">
<xs:selector xpath="Menu"/>
<xs:field xpath="Product"/>
</xs:keyref>-->
</xs:element>
</xs:sequence>
<xs:attribute name="name" type="xs:string" use="required" />
</xs:complexType>
<!-- KEY FAIL -->
<!-- <xs:keyref name="menuProductRef" refer="productNumber">
<xs:selector xpath="Menu"/>
<xs:field xpath="Product"/>
</xs:keyref> -->
</xs:element>
</xs:choice>
</xs:complexType>
<xs:key name="productNumber">
<xs:selector xpath="./Product"/>
<xs:field xpath="@num"/>
</xs:key>
<!-- KEY FAIL -->
<!-- <xs:keyref name="menuProductRef" refer="productNumber">
<xs:selector xpath="./Blarg/Menu"/>
<xs:field xpath="Product"/>
</xs:keyref> -->
</xs:element>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
选择器是相对于其上下文的,因此通过
Menu
内的keyRef
,选择器将是Product
并且选择器内的字段只是 < code>.:您的
xs:key
需要更像:但我猜您只是没有像其他部分一样对示例进行清理。
The selector is relative to its context, so with the
keyRef
insideMenu
, the selector would theProduct
and the field within the selector is just.
:Your
xs:key
needs to be more like:but I'm guessing you just didn't sanitize it for the example like the other parts.