XML Schema keyref 问题,可能是递归元素的问题

发布于 2024-11-02 11:37:18 字数 3643 浏览 1 评论 0原文

好吧,我已经用头撞了这么久了,我想我已经严重损坏了我的大脑,以至于我忘记了莎士比亚。没关系,我不太用他。这是我的问题。

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦醒灬来后我 2024-11-09 11:37:18

选择器是相对于其上下文的,因此通过 Menu 内的 keyRef,选择器将是 Product 并且选择器内的字段只是 < code>.:

 <xs:element name="Menu" type="productmenu">
     <xs:keyref name="subMenuProductRef" refer="productNumber">
         <xs:selector xpath="Product"/>
         <xs:field xpath="."/>
     </xs:keyref>
 </xs:element>

您的 xs:key 需要更像:

<xs:key name="productNumber">
    <xs:selector xpath="./Fruit"/>
    <xs:field xpath="@name"/>
</xs:key>

但我猜您只是没有像其他部分一样对示例进行清理。

The selector is relative to its context, so with the keyRef inside Menu, the selector would the Product and the field within the selector is just .:

 <xs:element name="Menu" type="productmenu">
     <xs:keyref name="subMenuProductRef" refer="productNumber">
         <xs:selector xpath="Product"/>
         <xs:field xpath="."/>
     </xs:keyref>
 </xs:element>

Your xs:key needs to be more like:

<xs:key name="productNumber">
    <xs:selector xpath="./Fruit"/>
    <xs:field xpath="@name"/>
</xs:key>

but I'm guessing you just didn't sanitize it for the example like the other parts.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文