forsyth20121205-174063a80cc13 years agoHistory
382 lines · 8.3 KB · Text
RawDownload
.TH W3C-XPOINTERS 2.SH NAMEw3c-xpointers \- parser for XPointers framework including XPath.SH SYNOPSIS.EXinclude "xpointers.m"; xpointers := load Xpointers Xpointers->PATH;Xpath, Xstep: import xpointers; # special operators ('+', '-', etc represent themselves)One, Ole, Oge, Omul, Odiv, Omod, Oand, Oor, Oneg,Onodetype, Onametest, Ofilter, Opath: con ...; # axis typesAancestor,Aancestor_or_self,Aattribute,Achild,Adescendant,Adescendant_or_self,Afollowing,Afollowing_sibling,Anamespace,Aparent,Apreceding,Apreceding_sibling,Aself: con iota; Xstep: adt {   axis:  int;  # Aancestor, ... (above)   op:    int;  # Onametest or Onodetype   ns:    string;   name:  string;   arg:   string;  # optional parameter to processing-instruction   preds: cyclic list of ref Xpath;    text:  fn(nil: self ref Xstep): string;   axisname:  fn(i: int): string;}; Xpath: adt {   pick{   E =>      op:    int;      l, r:  cyclic ref Xpath;   Fn =>      ns:    string;      name:  string;      args:  cyclic list of ref Xpath;   Var =>      ns:    string;      name:  string;   Path =>      abs:   int;      steps: list of ref Xstep;   Int =>      val:   big;   Real =>      val:   real;   Str =>      s:     string;   }   text: fn(nil: self ref Xpath): string;}; framework:  fn(s: string):               (string, list of (string, string, string), string); # predefined schemeselement:    fn(s: string): (string, list of int, string);xmlns:      fn(s: string): (string, string, string);xpointer:   fn(s: string): (ref Xpath, string);.EE.SH DESCRIPTION.B Xpointersimplements a parser for the World-Wide Web Consortium's XPointers framework,including a parser for XPath expressions..PP.B Initmust be called before any other operation in the module..PP.B Frameworkparses a string.I saccording to the grammar for the XPointers framework,and returns a tuple.BI ( short,\ pointers,\ err ) .On an error, the string.I errgives a diagnostic and the other two values are nil.Otherwise, if.I shortis non-nil, the XPointer was a `shorthand pointer', with the given value;.I pointerswill be nil.If a scheme-based pointer is used,.I shortis nil and.I pointershas a list of tuples.BI ( ns,\ scheme,\ data ) ,each representing one pointer value..I Nsis the XML name space for the given.IR scheme ;the default name space is represented by nil..I Schemeis the XPointer scheme name within that name space; and.I datais the actual pointer value following the rules of that scheme.(They all have completely different syntax.).PPThree common schemes are directly supported by the module,by functions named after the scheme.All of them follow the convention of returning a tuple in whichthe last element is a diagnostic string.On an error, all but that last element of the tuple will be nil,and the last element will be a non-nil string with a diagnostic..PP.B Xmlnsparses an XML name space definition of the form.IB ns = uri,and returns its components..PP.B Elementparses a value of the XPointer.B elementscheme, given by the grammar:.IP.EX.ft Iselector \f1::=\fP name child*  \f1|\fP  child+child \f1::=\fP '\f5/\fP' \f5[1-9][0-9]\fP*.EE.PPThe optional.I nameis an XPointer `shorthand pointer'.Each.I childnumber selects the child with that index (origin 1) at the corresponding level of the XML treebeneath the node selected by the.IR name ,or starting at the root of the XML tree..B Elementreturns a tuple.BI (( name,\ path ) ,\ err )where.I nameis the top element name or nil if none was specified,and.I pathis a.B "list of int"giving the path of child indices..PPThe most complex scheme is.BR xpointer ,because its syntax is that of XML's elaborate XPath expression..B Xpointerparses such an expression and returns a tuple.BI ( e,\ err )where.I erefers to an.B Xpathvalue that represents the abstract syntax of the XPath.BR Expr ..B Xpointerchecks only the syntax of.IR s ,and does not check that functions are limited to those specified by the.B xpointerscheme (that is consistent with it being a parse of.IR s ,rather than an XPointer or XPath evaluator)..PP.B Xpathand.B Xsteptogether represent an abstract syntax of the XPath grammar..PP.B Xsteprepresents the XPath.B Stepgrammar rule, with all abbreviations expanded to their full form:.IP.EX.ft IStep \f1::=\fP AxisName '\f5::\fP' NodeTest Predicate*NodeTest \f1::=\fP NameTest \f1|\fP NodeType '\f5(\fP' '\f5)\fP'NameTest \f1::=\fP '\f5*\fP' \f1|\fP NCName '\f5:\fP' '\f5*\fP' \f1|\fP (NCName '\f5:\fP')? NCNamePredicate \f1::=\fP '\f5[\fP' Expr '\f5]\fP'.EE.PPThe correspondence is as follows:.TF s.text().PD.TP.IB s .axisRepresents the.B AxisNameby one of the constants.B Aancestorto.BR Aself ..TP.IB s .op.B Onametestor.B Onodetypeto say which rule is represented.TP.IB s .nsFor a.IR NameTest ,gives the XML name space;can be.L *for `any name space' or nil for the default name space.For a.IR NodeType ,gives the type:.BR comment ,.BR node ,.BR processing-instruction ,or.BR text ..TP.IB s .nameGives the.I namefor a.IR NameTest ;can be.L *for `any name'..TP.IB s .argThe optional literal parameter to a.I NodeTypethat is a.BR processing-instruction ..TP.IB s .predsA list of.B Xpathvalues representing the optional sequence of.I Predicateexpressions.TP.IB s .text()Returns a string representing the.B Xstepin textual form..TP.IB s .axisname( a )Returns the printable text for axis code.I a(ie,one of.B Aancestorto.BR Aself ).PP.B Xpathvalues represent an abstract syntax for an XPath expression.Briefly, an expression follows the grammar below (see the XPath specification for the full concrete syntax)..IP.EX.ft I.ta \w'e ::=  'ue ::=	e '\f5or\fP' e   |	e '\f5and\fP' e   |	e \f1(\fP'\f5=\fP' \f1|\fP '\f5!=\fP'\f1)\fP e   |	e \f1(\fP'\f5<\fP' \f1|\fP '\f5<=\fP' \f1|\fP '\f5>=\fP' \f1|\fP '\f5>\fP'\f1)\fP e   |	e \f1(\fP'\f5+\fP' \f1|\fP '\f5-\fP'\f1)\fP e   |	e \f1(\fP'\f5*\fP' \f1|\fP '\f5div\fP' \f1|\fP '\f5mod\fP'\f1)\fP e   |	'\f5-\fP' e   |	e '\f5|\fP' e   |	filter   |	pathfilter ::= primary predicate* \f1(\fP\f1(\fP'\f5/\fP' \f1|\fP '\f5//\fP'\f1)\fP relpath\f1)\fP?primary ::= '\f5$\fP' QName \f1|\fP '\f5(\fP' e '\f5)\fP' \f1|\fP Literal \f1|\fP Number \f1|\fP FunctionName '\f5(\fP' \f1(\fPe \f1(\fP'\f5,\fP' e\f1)\fP*\f1)\fP '\f5)\fP'path ::= '\f5/\fP' relpath \f1|\fP relpathrelpath ::= relpath '\f5/\fP' relpath \f1|\fP relpath '\f5//\fP' relpath \f1|\fP Step.EE.PPMost of.I eis represented by a binary tree using the pick.BI Xpath.E( op,\ l,\ r )where.I opis an operator symbol (either the character itself or one of the constants.BR One ,.BR Odiv ,etc. for compound symbols),and.I land.I rrepresent the operands.The only unary operator.B Oneghas its operand in.IR l .A.I filteruses the binary operator.BI Xpath.E(Ofilter ,\ e,\ pred )to apply each.I predicateto the preceding.I primaryor.IR predicate .A.I filteralso uses.BI Xpath.E(Opath ,\ e,\ relpath )to apply the optional.I relpath(represented by a value of.BR Xpath.Path )to the preceding part of the filter expression..PPThe other cases in the pick adt correspond to the various choices of.I pathand.IR primary .Integer and real numbers are distinguished..I Literalis represented by.BR Xpath.Str ;variable references (ie,.BI $ QName\c)are represented by.BR XPath.Var ,where.I nsgives the optional XML name space of the.IR name ..I Pathis represented by.BI Xpath.Path( abs,\ steps )where.I abs is non-zero if and only if the path is absolute (starts with `/' or `//'),and.I stepslists the.B Xstepvalues corresponding to the slash-separated.I Stepsin the grammar.Abbreviated forms such as.RB ` // 'are converted by.B xpointerto their full internal form in terms of.RB ` / ',as defined by the specification,so there is no need to distinguish the delimiters in this representation..SH SOURCE.B /appl/lib/w3c/xpointers.b.SH SEE ALSO``XML Path Language (XPath) Version 1.0'',.B http://www.w3.org/TR/xpath.br``XPointer framework'',.B http://www.w3.org/TR/xptr-framework/.br``XPointer element() scheme'',.B http://www.w3.org/TR/xptr-element/.br``XPointer xmlns() scheme'',.B http://www.w3.org/TR/xptr-xmlns/.br``XPointer xpointer() scheme'',.B http://www.w3.org/TR/xptr-xpointer/