【发布时间】: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