% This LaTeX document was generated using the LaTeX backend of PlDoc, % The SWI-Prolog documentation system \section{library(pwp): Prolog Well-formed Pages} \label{sec:pwp} \begin{tags} \tag{author} Richard O'Keefe \tag{To be done} Support compilation of PWP input files \end{tags} PWP is an approach to server-side scripting using Prolog which is based on a simple key principle: \begin{itemize} \item The source form of a PWP should be WELL-FORMED XML \end{itemize} Especially when generating XML rather than HTML, this is such an obvious thing to do. We have many kinds of XML checking tools. \begin{itemize} \item We can tell whether an XML document is WELL FORMED (all the punctuation is right, all tags balance) using practically any decent parser, including SWI Prolog's 'sgml'. \item If we can write a Document Type Description then we can check that a document is VALID using tools like Open SP (formerly James Clark's SP) or SWI Prolog's 'sgml'. This does not guarantee that the output will be valid, but it does catch a lot of errors very early. \item If we can write an XML Schema then we can check that a document is schema-valid. (SWI Prolog's 'sgml' does not yet come with a schema validator, but who knows what the future holds?). \item Since an XML document is just a data structure, we can use any checking tool that we can write in Prolog, IF the input is well-formed so that we can load a template as a Prolog data structure. \end{itemize} Having decided that the input should be well formed, that means \textbf{NO NEW SYNTAX} None of the weird and horrible $<$\% ... \%$>$ or whatever not-quite-XML stuff you see in other template systems, making checking so very hard (and therefore, making errors so distressingly common). That in turns means that PWP "markup" must be based on special elements or special attributes. The fact that an XML parser must allow undeclared attributes on any element even when validating, but must not allow undeclared elements, suggests doing this through attributes. In particular, one should be able to take an existing DTD, such as an XHTML DTD, and just use that without modification. So the design reduces to \begin{itemize} \item Allow dynamic data selection, insertion, and transformation just using a small number of extra attributes. \end{itemize} This description uses the following name space: \begin{code} xmlns:pwp='http://www.cs.otago.ac.nz/staffpriv/ok/pwp.pl' \end{code} The attributes are \begin{shortlist} \item pwp:ask = Query \item pwp:use = Term \item pwp:how = text \Sbar{} xml \item pwp:tag = QName or '-' \item pwp:att = '' \Sbar{} 'one non-alphanumeric character' \end{shortlist} Here's what they mean. Each element is expanded in the context of a set of variable bindings. After expansion, if the tag is not mapped to '-', all attributes in the pwp: namespace are removed and the children elements are recursively expanded. \begin{itemize} \item pwp:ask = Query \begin{itemize} \item Query is a Prolog goal. For each solution of Query, the element is further processed with the new variables of Query added to the context. \item If Query is not a well formed Prolog goal, or if execution of Query throws an exception, page transformation comes to a complete halt and no page is generated. \end{itemize} \item pwp:use = Term \item pwp:how = text \Sbar{} xml \Sbar{} text-file \Sbar{} xml-file \Sbar{} pwp-file \begin{code} Term is a Prolog term; variables in Term are bound by the context. An empty Term is regarded as a missing value for this attribute. The Prolog variable CONTEXT refers to the entire context, a list of Name = Value, where Name is a Prolog atom holding the name of the context variable and Value is an arbitrary Prolog term. \end{code} \begin{itemize} \item If pwp:how is text, The value of Term is used to define a sequence of characters. \begin{itemize} \item A number produces the same characters that \predref{write}{1} would. \item An atom produces the same characters that \predref{write}{1} would. \item A string produces the same characters that \predref{write}{1} would. \item A list of character codes produces those characters. \item The following terms produce the same sequence of characters that the corresponding goal would have sent to the current output stream: \begin{shortlist} \item \verb$write(Datum)$ \item \verb$writeq(Datum)$ \item \verb$write_canonical(Datum)$ \item \verb$print(Datum)$ \item \verb$print(Datum)$ \item \verb$format(Format)$ \item \verb$format(Format, Arguments)$ \end{shortlist} \item A singleton list [X] defines the characters that X defines. \item Any other term F(T1,...,Tn) defines the characters that T1 defines, followed by the characters that T2 defines, ..., followed by the characters that Tn defines. \end{itemize} \item If pwp:how is xml, The value of Term must be an XML term as defined in the SGML2PL documentation or a list of such terms. A single term is taken as if it had been [Term]. The resulting list of terms replaces the children of the current element and will not be further processed. \item If pwp:how is text-file, The value of Term is used to define a sequence of characters. That sequence of characters is used as a file name. The file is read as a sequence of characters, and that sequence used as character data. \item If pwp:how is xml-file, The value of Term is used to define a sequence of characters. That sequence of characters is used as a file name. The file is loaded as XML, and the sequence of XML items thus obtained used. This means that PWP provides XML inclusion without depending on the parser to support XInclude. \item If pwp:how is pwp-file, Like xml-file, but PWP attributes are evaluated and processed. The current context variables are passed to the PWP processor. The default value for pwp:how is text. \end{itemize} \item pwp:tag = QName or '-' \begin{code} If pwp:tag is missing or the value is empty, the current element appears in the output (after further processing) with its present tag. If pwp:tag is a QName, the current element appears (...) with that as its tag. That option is most useful in DTDs, where an "authoring" DTD may use one tag and have it automatically mapped to another tag in the output, e.g., ->
  • . Finally, if pwp:tag is '-', the children of the current element (either the result of pwp:use or the transformed original children, whichever applies) appear in the output but there is no element around them. A missing or empty pwp:ask is just like pwp:ask = 'true'. \end{code} \item pwp:att = '' \Sbar{} 'one non-alphanumeric character'. \begin{code} Attributes in the pwp namespace are not affected by this attribute. Such attributes are always stripped out and never substituted into. If pwp:att is missing or empty, attributes of the current element are copied over to the output unchanged. If pwp:att = 'c' for some non-alphanumeric character c, each attribute is examined for occurrences of c(...)c and c[...]c which are as short as possible. There is no one character which could be used every time, so you have to explicitly choose a substitution marker which is safe for the data you do not want to be altered. None of the pwp attributes are inherited, least of all this one. Text outside c(...)c groups is copied unchanged; text inside a c(...)c group is parsed as a Prolog term and treated as if by pwp:how = text. Text inside a c[...]c group is evaluated (in the current context), and if it fails, the entire attribute will be removed from the element. \end{code} \end{itemize} Examples: \begin{enumerate} \item \textbf{A "Hello World" like example} \begin{code} </head> <body> <p><span pwp:use="Greeting" pwp:tag='-'/></p> </body> </html> \end{code} where \file{msg.pl} contains \begin{code} msg('Hello, World!'). \end{code} This example illustrates an important point. Prolog Well-Formed Pages provide \textbf{NO} way to physically incorporate Prolog \textbf{clauses} into a page template. Prolog clauses must be put in separate files which can be checked by a Prolog syntax checker, compiler, cross-referencer, \&c WITHOUT the Prolog tool in question needing to know anything whatsoever about PWP. You load the files using pwp:ask on the root element. \item \textbf{Binding some variables and using them} \begin{code} <html xmlns:pwp="http://www.cs.otago.ac.nz/staffpriv/ok/pwp.pl"> <head><title>Example 2

    The answer is .

    \end{code} \item \textbf{Making a table} We are given a Prolog database \file{staff.pl} defining \verb$staff(NickName, FullName, Office, Phone, E_Mail_Address)$. \verb$status(NickName, full_time | part_time)$. We want to make a phone list of full time staff. \begin{code} Phone list for Full-Time staff.

    Phone list for Full-Time staff.

    NamePhone
    \end{code} \item \textbf{Substituting into an attribute} Same data base as before, but now we want to make a mailing list page. \begin{code} Phone list for Full-Time staff.

    Phone list for Full-Time staff.

    NameAddress
    \end{code} \item \textbf{If-then-else effect} A page that displays the value of the 'SHELL' environment variable if it has one, otherwise displays 'There is no default shell.' \begin{code} $SHELL

    The default shell is .

    There is no default shell.

    \end{code} There is one other criterion for a good server-side template language: It should be possible to compile templates so as to eliminate most if not all interpretation overhead. This particular notation satisfies that criterion with the limitation that the conversion of a term to character data requires run-time traversal of terms (because the terms are not known until run time). \end{enumerate} \vspace{0.7cm} \begin{description} \predicate[det]{pwp_files}{2}{:In:atom, +Out:atom} loads an Xml document from the file named \arg{In}, transforms it using the PWP attributes, and writes the transformed version to the new file named \arg{Out}. \predicate[det]{pwp_stream}{3}{:Input:input_stream, +Output:output_stream, +Context:list} Loads an Xml document from the given \arg{Input} stream, transforms it using the PWP attributes, and writes the transformed version to the given \arg{Output} stream. \arg{Context} provides initial contextual variables and is a list of Name=Value. \predicate{pwp_xml}{3}{:In:list(xml), -Out:list(xml), +Context} maps down a list of XML items, acting specially on elements and copying everything else unchanged, including white space. The \arg{Context} is a list of 'VariableName'=CurrentValue bindings. \end{description}