views:

12

answers:

1

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.

A: 

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.

PeerBr