【问题标题】:XForms bind element errorXForms 绑定元素错误
【发布时间】:2010-06-03 18:37:20
【问题描述】:

我正在更改我的代码以在 XForms 中使用绑定(这比在任何地方都使用节点集更好!)但是我遇到了错误。

我收到的错误消息是:“Error: XForms Error (8): id (data_criterion) does not refer to a bind element...”

从我一直在使用的教程/指南来看,这似乎应该可行,但显然我遗漏了一些东西! (顺便说一句,我在此处的示例之后对绑定代码进行建模:http://en.wikibooks.org/wiki/XForms/Bind

我最初认为问题是由于我使用的是 xf:select 控件而不是 xf:input 示例,但即使我将代码简化为最简单的代码,我仍然会收到错误!

这是我正在使用的模型代码:

 <xf:model id="select_data">

     <xf:instance id="criteria_data" xmlns="">
         <file>
             <criteria>
                 <criterion></criterion>
             </criteria>
         </file>
     </xf:instance>

     <bind id="data_criterion" nodeset="instance('criteria_data')/criteria/criterion"/>

</xf:model>

至于ui代码,这是我的:

<xf:input bind="data_criterion">
    <xf:label>Enter criteria:</xf:label>
</xf:input>

我收到的错误消息是:“Error: XForms Error (8): id (data_criterion) does not refer to a bind element...”

有人知道问题出在哪里吗?此外,我应该注意绑定和 xf:select(带有 xf:itemset)控件的任何特殊用法吗? (我最终在我的表单上使用了很多 xf:select 控件..)

提前致谢!

编辑:

我通过这个validator 运行代码,我收到了这条消息(指的是绑定行): “警告:以下元素是否应该应用 XForms 命名空间?:bind(第 66 行)”

【问题讨论】:

  • 我不明白如何使用绑定是更好的做法。当您没有任何元素的验证规则时,您就不需要 bind 和它的 less 来编写。更少的代码意味着更少的错误。
  • 在我看来,它就像使用变量和更少的“硬编码”。这样,如果模型发生更改,我不必搜索整个文档来更改每个应该更改的节点集。这可能会在以后修改和维护代码时引起很多麻烦和错误。使用绑定只意味着对文档进行一次更改,而没有它们,可能是一百次!正确的?因此,我认为这是更好的做法。 (另外我在某处读过它;)大声笑。
  • 同意。我认为关键是“使用绑定是更好的做法”不一定总是正确的。添加绑定会增加一定程度的间接性,在某些情况下,这意味着您需要编写、阅读、维护和理解更多代码,但收益有限。

标签: data-binding binding xforms


【解决方案1】:

您可能想要更改的几件事:

  1. 不确定这是错误的原因,但nodeset 表达式应该是instance('criteria_data')/criteria/...,没有file。请记住:instance() 返回根元素,而不是文档节点。 (您通过更新问题来处理这个问题;很好)
  2. 您在bind 上缺少xf。应该是:&lt;xf:bind id="data_criterion" nodeset="instance('criteria_data')/criteria/criterion"/&gt;

请参阅下面的完整示例,其中包含您的代码,在 Orbeon Forms 下对我来说效果很好:

<xhtml:html xmlns:xhtml="http://www.w3.org/1999/xhtml"
      xmlns:xforms="http://www.w3.org/2002/xforms"
      xmlns:xf="http://www.w3.org/2002/xforms"
      xmlns:xxforms="http://orbeon.org/oxf/xml/xforms"
      xmlns:ev="http://www.w3.org/2001/xml-events"
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      xmlns:fr="http://orbeon.org/oxf/xml/form-runner">
    <xhtml:head>
        <xhtml:title>SO Bind</xhtml:title>
        <xf:model id="select_data">

            <xf:instance id="criteria_data" xmlns="">
                <file>
                    <criteria>
                        <criterion>Gaga</criterion>
                    </criteria>
                </file>
            </xf:instance>
            <xf:bind id="data_criterion" nodeset="instance('criteria_data')/criteria/criterion"/>
       </xf:model>

    </xhtml:head>
    <xhtml:body>
        <xf:input bind="data_criterion">
            <xf:label>Enter criteria:</xf:label>
        </xf:input>
    </xhtml:body>
</xhtml:html>

【讨论】:

  • 嗯...这是一个很好的观点,也是我的疏忽。但我仍然有错误.. :( 我将在我的问题中编辑我的代码以反映这一点。感谢您的回复!:)
  • @iHeartGreek,bind 上也缺少前缀。请参阅上面的 #2,以及我编辑后的回复。
  • 天啊。谢谢。仅此而已.. :( 我现在感到非常愚蠢。感谢您的额外眼睛。我现在会更加小心。我想删除这个问题,因为我很尴尬,但我不会因为你应得的分数回答!:)
  • @iHeartGreek,我很高兴它有帮助。有时我们只需要额外的眼睛。我知道它是怎么回事:)。
猜你喜欢
  • 1970-01-01
  • 2019-11-25
  • 1970-01-01
  • 1970-01-01
  • 2016-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多