<> a <http://www.w3.org/2000/10/swap/logic.n3#N3Document>. # Ugly!

@prefix dc: <http://purl.org/dc/elements/1.1/>.

<> dc:title "An RDF Specification of XML";
   dc:source
   <http://www.w3.org/TR/2001/WD-query-algebra-20010215>,
   <http://www.w3.org/TR/xpath#data-model>,
   # and Bert's paper
   <http://www.w3.org/TR/xslt#section-XML-Output-Method>.

@prefix u: <http://www.w3.org/2000/01/rdf-schema#>.
@prefix charmod: <charmod#>.
@prefix : <xml#>.

:serialize
  u:label "XML serialize";
  u:range charmod:String.

@prefix func: <#>. # separate namespace?
@prefix forest: <#>. # separate namespace?
@prefix v: <#>. # variables
@prefix l: <http://www.w3.org/2000/10/swap/logic.n3#>.

<> l:forAll v:char, v:code, v:n, v:t, v:t2, v:chars, v:out.

:Text a u:Class; u:label "Text node".
:text a [ func:from charmod:String; func:to :Text ].
# ... and :Text is generated by :text

[ is :text of "" ] :serialize "".
[ is :text of "<"] :serialize "&lt;".
[ is :text of ">"] :serialize "&gt;".
[ is :text of "&"] :serialize "&amp;".

{ v:t :text v:chars;
      :serialize v:out.
  v:char charmod:notIn "<>&".
  v:t2 :text [ is charmod:concat of ( v:char v:chars ) ].
}
  l:implies
  { v:t2 :seralize [ is charmod:concat of (v:char v:out) ] }.

:Content
 is forest:ordered of ( :Element :Text :ProcessingInstruction :Comment ).

:anElement func:argTypes (:QName
                          :URI
                          [ is forest:unordered of ( :Attribute ) ]
                          :Content);
           func:accessors ( :qname :namespaceName :attributes :content );
           func:resultType :Element.

# ... and :Element func:generatedBy :anElement

# a simple case
{ v:ln a :LocalName.
  v:spc a :WhiteSpaceOpt.
  v:elt :qname v:ln;
        :namespaceName "";
        :attibutes forest:uEmpty;
        :content forest:oEmpty }
  l:implies
    { v:elt :serialize [ is charmod:concat of ("<" v:ln v:spc "/>") ] }.



:anAttrinbute func:argTypes (:QName
                             :URI
                             charmod:String
                            );
           func:accessors ( :qname :namespaceName :value );
           func:resultType :Attribute.

# ... and :Attribute func:generatedBy :anAttribute

# a simple case of attribute serialization...
{ v:ln a :LocalName.
  v:spc1 a :WhiteSpaceOpt.
  v:spc2 a :WhiteSpaceOpt.
  v:attr :qname v:ln;
         :namespaceName "";
         :value :str.
  v:str :attrValLit v:lit. }
  l:implies
    { v:attr :serialize [ is charmod:concat of (" " v:ln "='" v:lit "'") ] }.


### func:from / func:to
@prefix daml: <http://www.daml.org/2000/12/daml+oil#>.

{

{ v:funClass func:from v:cls.
  v:f a v:funClass.
}
 l:implies { v:f a daml:UniqueProperty;
                 u:domain v:cls }.

{ v:funClass func:to v:cls.
  v:f a v:funClass.
}
 l:implies { v:f a daml:UniqueProperty;
             u:range v:cls }.
}
 l:forAll v:funClass, v:cls, v:f.

{ v:f func:argTypes (); func:resultType v:c }
  l:means { v:f a v:c }.

# @@ can't figure out the recurring case;
# reminds me of scheme record macros.
{ v:f func:argTypes (v:t1); func:resultType v:c }
  l:means
  { v:f a [ func:from v:t1;
            func:to v:c ] }.
