<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <title> Practical PostgreSQL</title>
    <meta name="keywords" content="postgresql, support, recovery, design, software, hosting, expterise, programming, postgres, pgsql, replication, mysql, firebird, database, sql, free, open source, software" />
    <meta name="description" content="Command Prompt, Inc. is the largest and oldest US based commercial PostgreSQL support provider. We provide the only commercially viable integrated PostgreSQL replication solution, but also custom programming, and support. We authored the book Practical PostgreSQL, the procedural language plPHP, and adding trigger capability to plPerl. " />
    <link rel="shortcut icon" href="../favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" type="text/css" href="../css/layout.css" />
    <link rel="stylesheet" type="text/css" href="../css/cmd4.css" />
    <script language="javascript" type="text/javascript"><!--
      greymenuhomewideLit = new Image();
      greymenuhomewideLit.src = "/images/greymenu_homewide_lit.gif";
      greymenuaboutLit = new Image();
      greymenuaboutLit.src = "/images/greymenu_about_lit.gif";
      greymenuproductsLit = new Image();
      greymenuproductsLit.src = "/images/greymenu_products_lit.gif";
      greymenuservicesLit = new Image();
      greymenuservicesLit.src = "/images/greymenu_services_lit.gif";
      greymenusupportLit = new Image();
      greymenusupportLit.src = "/images/greymenu_support_lit.gif";
      greymenucommunityLit = new Image();
      greymenucommunityLit.src = "/images/greymenu_community_lit.gif";
    //--></script>
  </head>
  <body>
      <div align="center">
       <div id="widecontainer">
         <div id="widetop">
           <div class="throwleft" style="width: 300px; text-align: left;"><a href="http://www.commandprompt.com/"><img src="../images/cmd4logo_wide.gif" border="0" alt="Command Prompt, Inc." /></a></div>
           <div class="throwright" style="width: 400px; text-align: right;">
             <div id="topright">
               <form action="index.html" name="cmdform" method="post">
                 <ul>
                   <li><input type="text" name="search" value="" /></li>
                   <li><input type="submit" name="action" value="Search Book" /></li>
                 </ul>
               </form>
             </div>
           </div>
         </div>
          <ul id="greymenu_wide">
            <li><a href="http://www.commandprompt.com/home/"><img onmouseover="this.src = greymenuhomewideLit.src;" onmouseout="this.src = '/images/greymenu_homewide.gif';" src="../images/greymenu_homewide.gif" border="0" height="22" title="" alt="" /></a></li><li><img src="../images/greymenu_divider.gif" border="0" width="2" height="22" alt="|" /></li><li><a href="http://www.commandprompt.com/about/"><img onmouseover="this.src = greymenuaboutLit.src;" onmouseout="this.src = '/images/greymenu_about.gif';" src="../images/greymenu_about.gif" border="0" height="22" title="" alt="" /></a></li><li><img src="../images/greymenu_divider.gif" border="0" width="2" height="22" alt="|" /></li><li><a href="http://www.commandprompt.com/products/"><img onmouseover="this.src = greymenuproductsLit.src;" onmouseout="this.src = '/images/greymenu_products.gif';" src="../images/greymenu_products.gif" border="0" height="22" title="" alt="" /></a></li><li><img src="../images/greymenu_divider.gif" border="0" width="2" height="22" alt="|" /></li><li><a href="http://www.commandprompt.com/services/"><img onmouseover="this.src = greymenuservicesLit.src;" onmouseout="this.src = '/images/greymenu_services.gif';" src="../images/greymenu_services.gif" border="0" height="22" title="" alt="" /></a></li><li><img src="../images/greymenu_divider.gif" border="0" width="2" height="22" alt="|" /></li><li><a href="http://www.commandprompt.com/support/"><img onmouseover="this.src = greymenusupportLit.src;" onmouseout="this.src = '/images/greymenu_support.gif';" src="../images/greymenu_support.gif" border="0" height="22" title="" alt="" /></a></li><li><img src="../images/greymenu_divider.gif" border="0" width="2" height="22" alt="|" /></li><li><a href="http://www.commandprompt.com/community/"><img onmouseover="this.src = greymenucommunityLit.src;" onmouseout="this.src = '/images/greymenu_community.gif';" src="../images/greymenu_community.gif" border="0" height="22" title="" alt="" /></a></li>          </ul>

  
        <div style="padding-left: 16px; width: 96%;">
          <HTML
><HEAD
><TITLE
>Tag Parsing</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.72
"><LINK
REL="HOME"
TITLE="Practical PostgreSQL"
href="book1"><LINK
REL="UP"
TITLE="LXP"
href="lxp"><LINK
REL="PREVIOUS"
TITLE="Using Cookies with LXP"
href="x21649"><LINK
REL="NEXT"
TITLE="Branching Logic"
href="x21880"></HEAD
><BODY
CLASS="SECT1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Practical PostgreSQL</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
href="x21649"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 13. LXP</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
href="x21880"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="TAGPARSING"
>Tag Parsing</A
></H1
><P
>When a tag is parsed, its attributes are read in one of two ways&#8212;literally, or interpretively. Similar to
    existing conventions in a variety of languages, defining a value in single-quotes (e.g.,
    <TT
CLASS="LITERAL"
>name='value'</TT
>) causes the contents of the value to be parsed literally, regardless of the
    characters between quotes. Using double-quotes causes its contents to be parsed interpretively, meaning that some characters
    will be treated in special ways.</P
><P
>Specifically, these special characters are the dollar sign (<TT
CLASS="LITERAL"
>$</TT
>), the at sign
    (<TT
CLASS="LITERAL"
>@</TT
>), and the ampersand (<TT
CLASS="LITERAL"
>&#38;</TT
>). These characters correspond
    to variable substitution, object variable value substitution, and entity substitution, respectively.</P
><P
>Value substitution is the process by which a variable, cookie, object, or entity's <SPAN
><I
CLASS="EMPHASIS"
>value</I
></SPAN
> is
    substituted for its syntactically referenced <SPAN
><I
CLASS="EMPHASIS"
>name</I
></SPAN
>. This occurs at the name's original location in any
    arbitrary string of characters.</P
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN21732"
>Variable Substitution</A
></H2
><P
>What may be confusing to experienced programmers at first is that LXP supports the familiar dollar sign notation to
      substitute a named variable (e.g., <TT
CLASS="LITERAL"
>$myvariable</TT
>) with its associated value in a mixed
      character string.</P
><P
>When using LXP, it is important to understand the contexts in which variables are substituted (and the context in
      which they are not). Subsequently, it is also important to understand when to use variable substitution and when not
      to.</P
><P
>The first rule is that variables will <SPAN
><I
CLASS="EMPHASIS"
>never</I
></SPAN
> be substituted outside of an LXP tag. <A
href="x21718#INVALIDVARIABLESUBSTITUTION"
>Example 13-9</A
> attempts incorrectly to place the value of a variable named
      <TT
CLASS="LITERAL"
>variable</TT
> within an LXP document.</P
><DIV
CLASS="EXAMPLE"
><A
NAME="INVALIDVARIABLESUBSTITUTION"
></A
><P
><B
>Example 13-9. Invalid variable substitution</B
></P
><PRE
CLASS="SCREEN"
>&#60;lxp&#62;
  Here is my variable: $variable &#60;!-- Wrong --&#62;
&#60;/lxp&#62;</PRE
></DIV
><P
>Instead, suppose that the URL <SPAN
><I
CLASS="EMPHASIS"
>http://localhost/test.lxp?setbar=foo</I
></SPAN
> is opened in a browser, and
      that <SPAN
><I
CLASS="EMPHASIS"
>test.lxp</I
></SPAN
> contains the snippet of LXP mark-up shown in <A
href="x21718#VALIDVARIABLESUBSTITUTION"
>Example 13-10</A
>.</P
><DIV
CLASS="EXAMPLE"
><A
NAME="VALIDVARIABLESUBSTITUTION"
></A
><P
><B
>Example 13-10. Valid variable substitution</B
></P
><PRE
CLASS="SCREEN"
>&#60;lxp&#62;
  &#60;setvar bar="$setbar" /&#62; &#60;!-- sets bar's value to setbar's value --&#62;
  &#60;putvar name="bar" /&#62;    &#60;!-- output the value of bar --&#62;
&#60;lxp&#62;</PRE
></DIV
><P
>The mark-up in <A
href="x21718#VALIDVARIABLESUBSTITUTION"
>Example 13-10</A
> opens an LXP region and uses the
      <TT
CLASS="LITERAL"
>&#60;setvar&#62;</TT
> tag to assign the <SPAN
><I
CLASS="EMPHASIS"
>value</I
></SPAN
> of the variable named
      <TT
CLASS="LITERAL"
>setbar</TT
> to a new variable named <TT
CLASS="LITERAL"
>bar</TT
>. Variable substitution
      is correctly used in this case, because it occurs within an LXP tag.</P
><P
>Since the previously mentioned URL assigned a value of <TT
CLASS="LITERAL"
>foo</TT
> to
      <TT
CLASS="LITERAL"
>setbar</TT
>, this means that the new variable <TT
CLASS="LITERAL"
>bar</TT
> will be
      assigned a value of <TT
CLASS="LITERAL"
>foo</TT
>.</P
><P
>The use of the <TT
CLASS="LITERAL"
>&#60;putvar&#62;</TT
> tag introduces the second rule to watch out for in
      LXP. Some tags (such as the <TT
CLASS="LITERAL"
>&#60;putvar&#62;</TT
> tag) expect to receive a literal variable name
      in order to perform their job. Remember that dollar signs and at signs are not actually part of variable names; they are
      only used to substitute values in place of names.</P
><P
>You might be inclined to think that the syntax of the <TT
CLASS="LITERAL"
>&#60;putvar&#62;</TT
> tag in <A
href="x21718#VALIDVARIABLESUBSTITUTION"
>Example 13-10</A
> should have read like this:</P
><PRE
CLASS="SCREEN"
>  &#60;putvar name="$bar" /&#62; &#60;!-- output the value of bar --&#62;</PRE
><P
>This would actually result, however, in the value of the variable <TT
CLASS="LITERAL"
>bar</TT
> being
      substituted into the value of the <TT
CLASS="LITERAL"
>name</TT
> attribute. Since the value of the
      <TT
CLASS="LITERAL"
>bar</TT
> variable is <TT
CLASS="LITERAL"
>foo</TT
>, LXP would attempt to insert a
      variable with the <SPAN
><I
CLASS="EMPHASIS"
>name</I
></SPAN
> of <TT
CLASS="LITERAL"
>foo</TT
>.</P
><P
>The simplest way to know whether or not to use substitution characters is to remain aware of what the purpose of the
      tag is. If an attribute should be substituted with a variable's <SPAN
><I
CLASS="EMPHASIS"
>value</I
></SPAN
>, use the
      <TT
CLASS="LITERAL"
>$</TT
> symbol to substitute it. If an attribute is literally specifying a variable by name, as
      with the <TT
CLASS="LITERAL"
>&#60;putvar&#62;</TT
> tag, do not substitute it.</P
><P
>A literal dollar sign (<TT
CLASS="LITERAL"
>$</TT
>) may be used within double quotes by placing two of them
      immediately one after the other, sequentially (e.g., <TT
CLASS="LITERAL"
>&#60;setvar price="$$99.95" /&#62;</TT
>).</P
><DIV
CLASS="NOTE"
><BLOCKQUOTE
CLASS="NOTE"
><P
><B
>Note: </B
>When using substitution, if a variable with the specified name is not found, LXP will check for a cookie with the
        specified name. If one is found, its value will be substituted.</P
></BLOCKQUOTE
></DIV
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN21788"
>Object Variable Value Substitution</A
></H2
><P
>The substitution of a variable value from an object is very similar to normal variable substitution. However, instead
      of using the dollar sign (<TT
CLASS="LITERAL"
>$</TT
>) to substitute a value, you use the at sign
      (<TT
CLASS="LITERAL"
>@</TT
>). Syntactically, the only difference between referencing a variable value with
      <TT
CLASS="LITERAL"
>@</TT
> instead of <TT
CLASS="LITERAL"
>$</TT
> is that dots
      (<TT
CLASS="LITERAL"
>.</TT
>) and square brackets (<TT
CLASS="LITERAL"
>[]</TT
>) are allowed as part of the
      object name.</P
><P
>A literal at sign (<TT
CLASS="LITERAL"
>@</TT
>) can be placed inside of an attribute's value by typing the
      character twice consecutively (e.g., <TT
CLASS="LITERAL"
>&#60;setvar email="jlx@@commandprompt.com" /&#62;</TT
>).</P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN21803"
>Entity substitution</A
></H2
><P
>LXP automatically converts any recognized entity within an LXP tag's attribute value into its literally interpreted character symbol. As of Version 0.8, LXP's recognized entities consist of the five pre-defined XML entities:</P
><P
>        <P
></P
><UL
><LI
><P
>Ampersand (<TT
CLASS="LITERAL"
>&#38;amp;</TT
>)</P
></LI
><LI
><P
>Less-than symbol (<TT
CLASS="LITERAL"
>&#38;lt;</TT
>)</P
></LI
><LI
><P
>Greater-than symbol (<TT
CLASS="LITERAL"
>&#38;gt;</TT
>)</P
></LI
><LI
><P
>Apostrophe (<TT
CLASS="LITERAL"
>&#38;apos;</TT
>)</P
></LI
><LI
><P
>Double-quote (<TT
CLASS="LITERAL"
>&#38;quot;</TT
>)</P
></LI
></UL
>
      </P
><P
>It's useful to know about entity substitution, as sometimes both apostrophes and quotes may be needed within the
      value of an LXP tag attribute, making it otherwise impossible to insert them without the use of these entities. LXP's
      developers considered programmatic back-slash escape sequences as a means to solve this (as is common in other programming
      languages), but LXP's ability to natively handle entities both preserves the mark-up mentality and adds a new level of
      sophistication to the language.</P
><P
><A
href="x21718#USINGENTITYSUBSTITUTION"
>Example 13-11</A
> provides an example of entity substitution within the LXP
      <TT
CLASS="LITERAL"
>&#60;include&#62;</TT
> tag.</P
><DIV
CLASS="EXAMPLE"
><A
NAME="USINGENTITYSUBSTITUTION"
></A
><P
><B
>Example 13-11. Using entity substitution</B
></P
><PRE
CLASS="SCREEN"
>&#60;lxp&#62;
  &#60;setvar field="field_two" /&#62;
  &#60;include sql="SELECT field_one, $field FROM &#38;quot;CAPITALIZED_TABLE&#38;quot;"
           method="SQL"&#62;
    &#60;strong&#62;Column One:&#60;/strong&#62; &#60;field name="field_one" /&#62;&#60;br&#62;
    &#60;strong&#62;Column Two:&#60;/strong&#62; &#60;field name="field_two" /&#62;&#60;br&#62;
  &#60;/include&#62;
&#60;/lxp&#62;</PRE
></DIV
><P
><A
href="x21718#USINGENTITYSUBSTITUTION"
>Example 13-11</A
> demonstrates the use of entities inside of a direct SQL query in order to
      place quotes within quotes. This is frequently required to make identifiers case-sensitive within PostgreSQL, as
      identifiers are otherwise folded to lowercase.</P
><P
>When parsed, the <TT
CLASS="LITERAL"
>&#38;quot;</TT
> is changed into its literal counter-part, making the
      actual executed query as follows:</P
><PRE
CLASS="SCREEN"
>  SELECT field_one, field_two FROM "CAPITALIZED_TABLE"</PRE
><P
>See <A
href="x22143#DIRECTSQL"
>the Section called <I
>Including SQL Content</I
></A
>" for an explanation of what exactly this example's LXP markup would achieve.</P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN21840"
>Using &#60;varparser&#62;</A
></H2
><P
>LXP supports a simple search-and-replace mechanism for variable values with its
      <TT
CLASS="LITERAL"
>&#60;varparser&#62;</TT
> tag. This tag takes two
      attributes&#8212;<TT
CLASS="LITERAL"
>find</TT
> and <TT
CLASS="LITERAL"
>replace</TT
>. When you use the
      <TT
CLASS="LITERAL"
>&#60;varparser&#62;</TT
> tag, a region is opened within which any variable value that is
      <SPAN
><I
CLASS="EMPHASIS"
>substituted</I
></SPAN
> will be filtered through the defined search-and-replace rule.</P
><P
>The <TT
CLASS="LITERAL"
>&#60;varparser&#62;</TT
> is primarily useful for stripping or escaping unwanted
      characters. For example, in preparation to execute a SQL statement, single-quotes (<TT
CLASS="LITERAL"
>'</TT
>) must
      be prefixed by a backslash, as a single-quote delimits string constants to PostgreSQL. <A
href="x21718#USINGVARPARSERTOPREPARESQL"
>Example 13-12</A
> demonstrates the escaping of single-quotes in a variable called
      <TT
CLASS="LITERAL"
>txt</TT
>.</P
><DIV
CLASS="EXAMPLE"
><A
NAME="USINGVARPARSERTOPREPARESQL"
></A
><P
><B
>Example 13-12. Using &#60;varparser&#62; to prepare SQL</B
></P
><PRE
CLASS="SCREEN"
>&#60;lxp&#62;
  &#60;varparser find="'" replace="\'"&#62;
    &#60;include sql="SELECT * FROM table WHERE txtfield = '$txt'"&#62;
      &#60;field /&#62;&#60;br /&#62;
    &#60;/include&#62;
  &#60;/varparser&#62;
&#60;/lxp&#62;</PRE
></DIV
><P
>In <A
href="x21718#USINGVARPARSERTOPREPARESQL"
>Example 13-12</A
>, the <TT
CLASS="LITERAL"
>&#60;varparser find="'" replace="\'"&#62;</TT
>
      tag instructs LXP to replace any single-quote with a back-referenced <TT
CLASS="LITERAL"
>\'</TT
> sequence within any 
      <SPAN
><I
CLASS="EMPHASIS"
>substituted</I
></SPAN
> variable value.  </P
><P
>Note that this search-and-replace occurs <SPAN
><I
CLASS="EMPHASIS"
>only</I
></SPAN
> for <SPAN
><I
CLASS="EMPHASIS"
>substituted variable values</I
></SPAN
>.
      As such, the literally typed apostrophes in the <TT
CLASS="LITERAL"
>sql</TT
> attribute of the
      <TT
CLASS="LITERAL"
>&#60;include&#62;</TT
> tag are left unchanged; only the contents of variable values being
      <SPAN
><I
CLASS="EMPHASIS"
>substituted</I
></SPAN
> within that attribute (e.g., the <TT
CLASS="LITERAL"
>txt</TT
> variable's value, in
      <A
href="x21718#USINGVARPARSERTOPREPARESQL"
>Example 13-12</A
>) are modified upon their substitution.</P
><P
>The closing <TT
CLASS="LITERAL"
>&#60;/varparser&#62;</TT
> tag puts LXP back into normal variable substitution
      mode.</P
><DIV
CLASS="NOTE"
><BLOCKQUOTE
CLASS="NOTE"
><P
><B
>Note: </B
>You can configure several simultaneous search-and-replace rules by nesting several
        <TT
CLASS="LITERAL"
>&#60;varparser&#62;</TT
> tags within one another.</P
></BLOCKQUOTE
></DIV
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
href="x21649"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
href="book1"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
href="x21880"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Using Cookies with LXP</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
href="lxp"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Branching Logic</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>
        </div>
        <br />
        

          <div id="thefineprint">
            <a href="http://www.commandprompt.com/products/mammothpostgresql/"><img src="../images/powered_by_mammoth.gif" alt="Powered by Mammoth PostgreSQL" title="Powered by Mammoth PostgreSQL" border="0" /></a>
                        <br />
            Copyright &copy; 2000-2007 Command Prompt, Inc. All Rights Reserved. All trademarks property of their respective owners.
          </div>
        </div>
      </div>
  </body>
</html>