XML schema still allowing duplicate id's with unique

后端 未结 2 1765
心在旅途
心在旅途 2021-01-18 14:40

I am trying to design an XML schema for books where a unique ID needs to be specified for each book entry. However it just doesn\'t seem to work. Below is the XSD i am using

2条回答
  •  执念已碎
    2021-01-18 15:33

    You've got the in the wrong place - it needs to be inside the definition of the ancestor element within which the Book elements should be unique, not in the Book element definition itself. The following would force Book ids to be unique within each shelf, but would allow the same ID on different shelves:

      
        
          
            
            
              
                
                
              
            
          
        
      
    

    If instead you want the IDs to be globally unique across all shelves then put the unique constraint at the BookShelf level and adjust the selector appropriately:

      
        
          
            
            
          
        
        
          
          
        
      
    

    For future reference, note that if your schema had a targetNamespace then those selectors would not work as-is, because unprefixed names in selector XPaths always mean "no namespace". You would need to add xmlns:tns="" to your xs:schema element and then use a selector of tns:Shelf/tns:Book.

提交回复
热议问题