Loading ...
Sorry, an error occurred while loading the content.

Separating structure and data restrictions

Expand Messages
  • goodman.m.w@gmail.com
    Hi, I have a use case that doesn t seem so complicated, but is proving difficult to implement. I want to: a) define a fixed structure for a document b)
    Message 1 of 1 , May 19 10:31 AM
    • 0 Attachment
      Hi, I have a use case that doesn't seem so complicated, but is proving difficult to implement. I want to:

        a) define a fixed structure for a document

        b) separately place constraints on data based on an attribute of a parent element

        c) allow a user to extend these constraints

      For example, in this test.xml file, if an <elem> has type="basic", it can contain an empty <inner>, but if type="one" or type="two", any <inner> must have type="1" or type="2", respectively:

      <?xml version="1.0" encoding="utf-8"?>
      <root>
        <elem type="basic">
          <inner/>
        </elem>
        <elem type="one">
          <inner type="1"/>
        </elem>
        <elem type="two">
          <inner type="2"/>
        </elem>
      </root>

      Modeling this by itself isn't so hard, but I want to fix the structure that <elem> elements can always contain <inner> elements, and other constraints are placed in addition to this. For example, a user may define a new <elem> with type="three", etc.

      I've got a solution working, but it seems overly convoluted:

      #--- root.rnc ---
      start = Root
      Root = element root { Elem* }
      Elem = grammar { include "structure.rnc" }

      #--- structure.rnc ---
      start = element elem { Elem.subtype }
      Elem.subtype = attribute type { Elem.type }, Elem.content
      Elem.type = notAllowed
      Elem.content = Inner*
      Inner = element inner { Inner.subtype }
      Inner.subtype = attribute type { Inner.type }?, Inner.content
      Inner.type = notAllowed
      Inner.content = empty

      With these, a user may place their own constraints in a separate file:

      #--- custom.rnc ---
      include "root.rnc" {
        Elem |= grammar { include "structure.rnc" {
          Elem.type = "basic"
        } }
        Elem |= grammar { include "structure.rnc" {
          Elem.type = "one"
          Inner.type = "1"
        } }
        Elem |= grammar { include "structure.rnc" {
         Elem.type = "two"
         Inner.type = "2"
        } }
      }

      And it seems to work with Jing:

      $ jing -c custom.rnc test.xml ; echo $?
      0

      But in custom.rnc I have an include that redefines another include, which feels like a terrible hack. Can anybody think of a better solution, or tell me what I'm doing wrong?

      Thanks!

      --
      -Michael Wayne Goodman
    Your message has been successfully submitted and would be delivered to recipients shortly.