开发者

What is allowed in Restricted XPaths (constraints)?

开发者 https://www.devze.com 2023-01-14 03:42 出处:网络
I need to apply constraints in a large XML file, like this: <library> <book> <bookAuthor ID=\"1\" nameAlias=\"PeerBR jr\"/>

I need to apply constraints in a large XML file, like this:

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

I need each bookAuthor's ID to refer to a valid author.

I am finding the "restricted XPath" very clumsy to work with, but maybe overlooking something. Am I right to define the constraint this way:

<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>

It works, but my file is actually more nested, so it gets really messy. Plus I have to 开发者_开发知识库write a new constraint for "library/book/CoAuthor". Is there nothing more elegant I can do? Can't I abbreviate the selector?

Can I restrict the application of the constraint ("bookauthor[@nameAlias]")?

Thanks in advance for your help.


Found the answer in O'Reilly's "XML Schema". It is ok to have:

  • relative paths with child element
  • namespaces
  • all elements ("*")
  • any child element (".//bookauthor" in my case will do the trick)

It is not ok to include anything fancy, such as:

  • parent element
  • tests ("[@nameAlias]", so no way to apply constraint only to nodes with a nameAlias attribute)
  • absolute paths

Hope this helps someone stumbling over the issue later.

0

精彩评论

暂无评论...
验证码 换一张
取 消