【问题标题】:What is allowed in Restricted XPaths (constraints)?Restricted XPaths(约束)允许什么?
【发布时间】:2011-04-07 22:46:27
【问题描述】:

我需要在大型 XML 文件中应用约束,如下所示:

<library>
    <book>
        <bookAuthor ID="1" nameAlias="PeerBR jr"/>
    </book>
    <book>
        <bookAuthor ID="1"/>
    </book>
</library>
<authorCatalogue>
    <author ID="1" name="PeerBr"/>
</authorCatalogue>

我需要每个 bookAuthor 的 ID 来引用有效的作者。

我发现使用“受限 XPath”非常笨拙,但可能会忽略一些东西。我这样定义约束是否正确:

<xs:keyref name="bookAuthor" refer="author">
    <xs:selector xpath="library/book/bookauthor"/>
    <xs:field xpath="@ID"/>
</xs:keyref>        
<xs:key name="author">
    <xs:selector xpath="authorCatalogue/author"/>
    <xs:field xpath="@ID"/>
</xs:key>

它可以工作,但我的文件实际上更嵌套,所以它变得非常混乱。另外,我必须为“图书馆/书籍/共同作者”写一个新的约束。没有什么比我更优雅的了吗?我不能缩写选择器吗?

我可以限制约束的应用(“bookauthor[@nameAlias]”)吗?

提前感谢您的帮助。

【问题讨论】:

  • 这个问题与一般 XPath 表达式无关。来自:w3.org/TR/xmlschema-1/#cIdentity-constraint_Definitions{selector} specifies a restricted XPath ([XPath]) expression relative to instances of the element being declared.。所以,我已经编辑了你的标签。另外,从w3.org/TR/xmlschema-1/#coss-identity-constraint 看来,您可以使用| 节点集联合运算符。所以,也许你可以使用library/book/bookauthor | library/book/CoAuthor,但我不是 XSchema 专家......
  • @Alejandro:是的,你完全正确,我已经阅读了架构并按照你的建议尝试了联合运算符。谢谢。

标签: xml xsd restrictions xmlspy


【解决方案1】:

在 O'Reilly 的“XML Schema”中找到了答案。没关系:

  • 带有子元素的相对路径
  • 命名空间
  • 所有元素(“*”)
  • 任何子元素(在我的例子中是“.//bookauthor”)

不能包含任何花哨的东西,例如:

  • 父元素
  • 测试(“[@nameAlias]”,因此无法仅对具有 nameAlias 属性的节点应用约束)
  • 绝对路径

希望这对以后遇到此问题的人有所帮助。

【讨论】:

    猜你喜欢
    • 2016-06-15
    • 2016-12-09
    • 2020-07-03
    • 2015-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多