<!DOCTYPE article [
  <!ENTITY version "@PACKAGE_VERSION@">
]>
<d:article xmlns:d='http://docbook.org/ns/docbook'
	   xmlns:tcl='http://tclxml.sourceforge.net/doc'
	   xmlns:xlink='http://www.w3.org/1999/xlink'>
  <d:info>
    <d:title>XML Support For Tcl</d:title>
    <d:subtitle>TclXML, TclDOM and TclXSLT</d:subtitle>

    <d:releaseinfo>$Id: README.xml,v 1.7 2005/05/20 12:02:18 balls Exp $</d:releaseinfo>

    <d:revhistory>
      <d:revision>
	<d:revnumber>&version;</d:revnumber>
      </d:revision>
    </d:revhistory>

    <d:author>
      <d:firstname>Steve</d:firstname>
      <d:surname>Ball</d:surname>
      <d:affiliation>
	<d:orgname>Explain</d:orgname>
	<d:address>
	  <d:otheraddr>
	    <d:link xlink:href='http://www.explain.com.au/'>www.explain.com.au</d:link>
	  </d:otheraddr>
	</d:address>
      </d:affiliation>
    </d:author>
  </d:info>

  <d:para>This package provides XML parsers, DOM scripting and XSL Transformations for <d:link xlink:href='http://www.tcl.tk'>Tcl</d:link>. In previous distributions, these features were supplied as separate packages. Now they have been combined into a single package to make installation easier.</d:para>

  <d:para>Contact <d:link xlink:href='mailto:Steve.Ball@explain.com.au'>Steve Ball</d:link> for information about this release.</d:para>

  <d:section>
    <d:info>
      <d:title>TclXML</d:title>
    </d:info>

    <d:para>TclXML provides a streaming parser for XML documents. This is the lowest-level interface for processing XML documents in Tcl. The package has a generic front-end interface with plugin parser implementations.  A number of parser implementations or wrappers are provided:</d:para>

    <d:itemizedlist>
      <d:listitem>
	<d:para>Gnome libxml2 library.  This package is known as TclXML/libxml2.</d:para>
      </d:listitem>
      <d:listitem>
	<d:para>A generic Tcl implementation (which does not require compilation).  This package is known as TclXML/tcl.</d:para>
      </d:listitem>
    </d:itemizedlist>

    <d:para>Both of these implementations may be installed at the same time. See <d:link xlink:href='doc/tclxml.html'>the manual page</d:link> for more information.</d:para>
  </d:section>

  <d:section>
    <d:info>
      <d:title>TclDOM</d:title>
    </d:info>

    <d:para>TclDOM provides a tree view for XML documents. This is usually the best interface for scripting XML documents using Tcl. The package has two implementations:</d:para>

    <d:itemizedlist>
      <d:listitem>
	<d:para>Gnome libxml2 library.  This package is known as TclDOM/libxml2.</d:para>
      </d:listitem>
      <d:listitem>
	<d:para>A generic Tcl implementation (which does not require compilation).  This package is known as TclDOM/tcl.</d:para>
      </d:listitem>
    </d:itemizedlist>

    <d:para>Only one of these will be installed.</d:para>

    <d:para>See <d:link xlink:href='doc/tcldom.html'>the manual page</d:link> for more information.</d:para>
  </d:section>

  <d:section>
    <d:info>
      <d:title>TclXSLT</d:title>
    </d:info>

    <d:para>TclXSLT provides a method to invoke XSL Transformations upon XML documents. This package is a wrapper for the libxslt library.</d:para>

    <d:para>See <d:link xlink:href='doc/tclxslt.html'>the manual page</d:link> for more information.</d:para>
  </d:section>

  <d:section xml:id='installation'>
    <d:info>
      <d:title>Installation</d:title>
    </d:info>

    <d:section>
      <d:info>
	<d:title>Dependencies</d:title>
      </d:info>

      <d:variablelist>
	<d:varlistentry>
	  <d:term>Tcllib</d:term>
	  <d:listitem>
	    <d:para><d:link xlink:href='http://www.tcl.tk/software/tcllib/'>http://www.tcl.tk/software/tcllib/</d:link></d:para>

	    <d:para>In order for the Tcl-only parser to resolve external entities, the tcllib package must be installed.</d:para>

	    <d:para>Be sure to get a version which includes the <tcl:package>uri</tcl:package> package.  Version 1.11 or better is recommended.</d:para>
	    <d:itemizedlist>
	      <d:listitem>
		<d:para><d:link xlink:href='ftp://prdownloads.sf.net/tcllib/tcllib-1.11.tar.gz'>GZip'd tarball</d:link></d:para>
	      </d:listitem>
	      <d:listitem>
		<d:para><d:link xlink:href='ftp://prdownloads.sf.net/tcllib/tcllib-1.11.zip'>ZIP file</d:link></d:para>
	      </d:listitem>
	    </d:itemizedlist>
	    <d:para>The latest CVS snapshot may be found at <d:link xlink:href='http://sourceforge.net/projects/tcllib'>the SourceForge project page</d:link>.</d:para>
	  </d:listitem>
	</d:varlistentry>
	<d:varlistentry>
	  <d:term>libxml2</d:term>
	  <d:listitem>
	    <d:para>libxml2 is required for the compiled version of the TclXML/libxml2 package.  libiconv may also be required.</d:para>

	    <d:para>The source code for libxml2 and libiconv is <d:emphasis>not</d:emphasis> supplied with this package.  Download libxml2 from <d:link xlink:href='http://xmlsoft.org/'>xmlsoft.org</d:link> separately. libiconv may also be required; download from a GNU mirror site.</d:para>

	    <d:para>Version 2.7.2 (or better) is recommended.</d:para>
	  </d:listitem>
	</d:varlistentry>
      </d:variablelist>
    </d:section>

    <d:section>
      <d:info>
	<d:title>Pure-Tcl Installation</d:title>
	<d:subtitle>no compilation required</d:subtitle>
      </d:info>

      <d:para>Run the configure script and invoke the command:</d:para>
      <d:informalexample>
	<d:command>make install</d:command>
      </d:informalexample>

      <d:para>If the pure-Tcl parser is good enough for you, then read no further.</d:para>
    </d:section>

    <d:section>
      <d:info>
	<d:title>Compiled Installation</d:title>
      </d:info>

      <d:section>
	<d:info>
	  <d:title>Unix/Linux</d:title>
	</d:info>

	<d:para>You must have Tcl/Tk version 8.2 or better installed on your system.  Tcl/Tk 8.3 or better is recommended.</d:para>

	<d:orderedlist>
	  <d:listitem>
	    <d:para>Make sure you have Tcllib 1.11 (or better) installed.  Tcllib is still required, even for the compiled parser.</d:para>
	  </d:listitem>
	  <d:listitem>
	    <d:para>If you wish to use TclXML/libxml2, make sure libxml2-2.7.2 (or better) is installed.</d:para>
	  </d:listitem>
	  <d:listitem>
	    <d:para>Unpack the TclXML distribution and <d:command>cd</d:command> into the <d:filename>tclxml-&version;</d:filename> directory.</d:para>
	  </d:listitem>
	  <d:listitem>
	    <d:para>Run the <d:filename>configure</d:filename> script, with the <d:arg>--prefix</d:arg> and <d:arg>--enable-threads</d:arg> switches (the latter only if Tcl has been built with threads enabled). Use the <d:arg>--with-xml2-config</d:arg> switch to specify the location of the libxml2 configuration script, <d:filename>xml2Conf.sh</d:filename>. Similarly, use the <d:arg>--with-xslt-config</d:arg> if necessary.</d:para>
	    <d:para>TclXML/libxml2 may be configured to statically link the libxml2 and libxslt libraries to the libtclxml.so shared library. This is advantageous when using TclXML/libxml2 in a StarKit. To statically link the libraries use the <d:arg>--with-xml-static</d:arg> switch.</d:para>
	    <d:para>For example, on my system I have Tcl 8.5 installed in <d:filename>/usr/local/tcl8.5</d:filename> and libxm2 installed in <d:filename>/usr/local/gnome</d:filename>. I also need to statically link the libraries. Therefore I would use the command:</d:para>

	    <d:programlisting>./configure --prefix=/usr/local/tcl8.5 --enable-threads --with-xml2-config=/usr/local/gnome/bin/xml2Conf.sh --with-xml-static=1</d:programlisting>
	  </d:listitem>
	  <d:listitem>
	    <d:programlisting>make</d:programlisting>
	  </d:listitem>
	  <d:listitem>
	    <d:note>Don't test the package using <d:literal>make test</d:literal> until all of the packages are installed (it is a current deficiency of the build system that the package cannot be tested before installation - we hope to fix this soon!).</d:note>
	  </d:listitem>
	  <d:listitem>
	    <d:programlisting>make install</d:programlisting>

	    <d:para>You may need to do this as root, depending on your installation.</d:para>
	  </d:listitem>
	  <d:listitem>
	    <d:programlisting>make test</d:programlisting>
	  </d:listitem>
	  <d:listitem>
	    <d:programlisting>make doc</d:programlisting>
	  </d:listitem>
	  <d:listitem>
	    <d:programlisting>make install-doc</d:programlisting>
	  </d:listitem>
	</d:orderedlist>
      </d:section>

      <d:section>
	<d:info>
	  <d:title>Windows (MSYS/MINGW)</d:title>
	</d:info>

	<d:note>[Advice:  ActiveTcl includes binaries for TclXML.]</d:note>

	<d:para>You must have Tcl/Tk version 8.2 or better installed on your system.  Tcl/Tk 8.5.5 or better is recommended.</d:para>

	<d:para>Before starting, download the binaries for libxml2 (or build them from source).  <d:link xlink:href='http://xmlsoft.org/'>xmlsoft</d:link> has a link to the MS Windows binary distribution.</d:para>

	<d:para>If you have a TEA build environment setup, just use the normal <d:literal>configure/make/make install</d:literal> pattern.</d:para>
      </d:section>

      <d:section>
	<d:info>
	  <d:title>Windows (NMAKE/VC++ 6.0)</d:title>
	</d:info>

	<d:note>TclXML/libxml2 is built with MSYS/MINGW, see above, so this build system is untested.</d:note>

	<d:para>Alternatively, the <d:filename>win</d:filename> subdirectory contains a <d:filename>makefile.vc</d:filename> file for Visual Studio C++ v6.0.  In a Command Prompt window set up your environment so that <d:application>nmake</d:application> is on the path (by running <d:filename>VCVARS32.BAT</d:filename>), then type the following:</d:para>

	<d:informalexample>
	  <d:programlisting>nmake -f makefile.vc TCLDIR=<d:replaceable>C:\Path\To\Tcl</d:replaceable> INSTALLDIR=<d:replaceable>C:\Path\To\Tcl</d:replaceable> LIBZDIR=<d:replaceable>C:\Path\To\libz</d:replaceable> LIBICONVDIR=<d:replaceable>C:\Path\To\libiconv</d:replaceable> LIBXML2DIR=<d:replaceable>C:\Path\To\libxml2</d:replaceable> LIBXSLTDIR=<d:replaceable>C:\Path\To\libxslt</d:replaceable></d:programlisting>
	</d:informalexample>

	<d:para>As an example, on my system I have Tcl installed in <d:filename>C:\Tcl</d:filename> and the libxml2 and libxslt binaries unpacked in the directory <d:filename>C:\gnome</d:filename>.  Accordingly, I would use the following command line:</d:para>
	<d:informalexample>
	  <d:programlisting>nmake -f makefile.vc TCLDIR=C:\Tcl INSTALLDIR=C:\Tcl LIBZDIR=C:\gnome\zlib-1.1.4.win32 LIBICONVDIR=C:\gnome\libiconv-1.9.1.win32 LIBXML2DIR=C:\gnome\libxml2-2.7.2.win32 LIBXSLTDIR=C:\gnome\libxslt-1.1.24.win32</d:programlisting>
	</d:informalexample>

	<d:para>Install the package by appending 'install' to the command line used above, for example:</d:para>

	<d:informalexample>
	  <d:programlisting>nmake -f makefile.vc TCLDIR=<d:replaceable>C:\Path\To\Tcl</d:replaceable> INSTALLDIR=<d:replaceable>C:\Path\To\Tcl</d:replaceable> LIBZDIR=<d:replaceable>C:\Path\To\libz</d:replaceable> LIBICONVDIR=<d:replaceable>C:\Path\To\libiconv</d:replaceable> LIBXML2DIR=<d:replaceable>C:\Path\To\libxml2</d:replaceable> LIBXSLTDIR=<d:replaceable>C:\Path\To\libxslt</d:replaceable> install</d:programlisting>
	</d:informalexample>
      </d:section>

      <d:section>
	<d:info>
	  <d:title>Macintosh OS X</d:title>
	</d:info>

	<d:note>Binary distributions of libxml2, libxslt and TclXML as frameworks are provided by <d:link xlink:href='http://www.explain.com.au/oss/'>Explain</d:link>.</d:note>

	<d:para>There are two ways to build TclXML under Mac OS X:</d:para>
	<d:orderedlist>
	  <d:listitem>
	    <d:para>The usual Unix way, see above.</d:para>
	  </d:listitem>
	  <d:listitem>
	    <d:para>As an embedded Framework using Xcode.</d:para>
	  </d:listitem>
	</d:orderedlist>

	<d:para>The <d:filename>macosx</d:filename> directory contains the Xcode files for building under OS X (Leopard/Panther).  TclXML/libxml2 has been tested on OS X 10.5 (or is that X.5?).</d:para>

	<d:para>Start-up the project.  Make sure that the references to the libxml2 and Tcl external frameworks are correct.  Select the 'Make' target and build.  This builds everything.  The result is two Mac OS X Frameworks; a "normal" and an "embedded".  The embedded framework will be in the <d:filename>embedded</d:filename> subdirectory of the Build Products directory.  Copy <d:filename>tclxml.framework</d:filename> to any of the usual places for frameworks (<d:filename>~/Library/Frameworks</d:filename>, <d:filename>/Library/Frameworks</d:filename>, etc).</d:para>

	<d:para>For earlier version of OS X using Project Builder, you will have to retrieve a previous version of the Project Builder files from the CVS repository.</d:para>
      </d:section>

    </d:section>
  </d:section>

  <d:section>
    <d:info>
      <d:title>Usage</d:title>
    </d:info>

    <d:para>See <d:link xlink:href='http://tclxml.sourceforge.net/'>the website</d:link> for links to tutorials and the reference manual.</d:para>

    <d:para>In the meantime, here's a quick tutorial:</d:para>

    <d:section>
      <d:info>
	<d:title>Parsing XML, Streaming</d:title>
      </d:info>

      <d:para>This is the lowest-level access to an XML document; use SAX-like events to stream through the document. The simple program below counts the number of characters in the content of an XML document.</d:para>

      <d:programlisting>
package require xml &version;

set parser [xml::parser]
$parser configure -elementstartcommand EStart \
    -characterdatacommand PCData

proc EStart {tag attlist args} {
    array set attr $attlist
    puts "Element \"$tag\" started with [array size attr] attributes"
}

proc PCData text {
    incr ::count [string length $text]
}

set count 0
$parser parse [read stdin]

puts "The document contains $count characters"
exit 0
</d:programlisting>
    </d:section>

    <d:section>
      <d:info>
	<d:title>Parsing XML with DOM</d:title>
      </d:info>

      <d:para>This is the next level up in accessing an XML document; use the Document Object Model (DOM) to view the XML document as a tree. The simple program below counts the number of characters in the content of an XML document.</d:para>

      <d:programlisting>
package require xml &version;

set doc [dom::parse [read stdin]]
set count 0
foreach textNode [dom::selectNode $doc //text()] {
    incr count [string length [$textNode cget -nodeValue]]
}

puts "The document contains $count characters"
      </d:programlisting>
    </d:section>

    <d:section>
      <d:info>
	<d:title>Transforming XML with XSLT</d:title>
      </d:info>

      <d:para>This is the highest level in processing an XML document; use a XSL stylesheet to transform a XML document. The simple program below reads two XML documents, compiles one into a XSL stylesheet and performs the transformation.</d:para>

      <d:programlisting>
package require xml &version;

set chan [open "count.xsl"]
set styleDoc [dom::parse [read $chan]]
close $chan
set sourceDoc [dom::parse [read stdin]]

set style [xslt::compile $styleDoc]
set resultDoc [$style transform $sourceDoc]

puts [dom::serialize $resultDoc]
      </d:programlisting>

      <d:para>The XSL stylesheet <d:filename>count.xsl</d:filename>, which counts the number of characters in the source document, looks like this:</d:para>

      <d:programlisting><![CDATA[
<xsl:stylesheet version='1.0'
  xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

  <xsl:template match='/'>
    <xsl:text>The document contains </xsl:text>
    <xsl:call-template name='add'>
      <xsl:with-param name='nodes' select='//text()'/>
    </xsl:call-template>
    <xsl:text> characters.
</xsl:text>
  </xsl:template>

  <xsl:template name='add'>
    <xsl:param name='sum' select='0'/>
    <xsl:param name='nodes' select='/..'/>

    <xsl:choose>
      <xsl:when test='not($nodes)'>
        <xsl:value-of select='$sum'/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name='add'>
          <xsl:with-param name='sum'
            select='$sum + string-length($nodes[1])'/>
          <xsl:with-param name='nodes'
            select='$nodes[position() != 1]'/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>
]]></d:programlisting>
    </d:section>
  </d:section>

  <d:section>
    <d:info>
      <d:title>XPath</d:title>
    </d:info>

    <d:para>In addition to XML parsing packages, TclXML also provides a package for parsing XPath location paths.  The XPath package only parsing the path's syntax, it does interpret the path.  See <d:link xlink:href='http://tclxml.sourceforge.net/tcldom.html'>TclDOM</d:link> for a package that will interpret XPath location paths.</d:para>

    <d:note>This package is in its infancy, and does not support the full range of XPath features.  Only a very limited subset of location paths are supported, of the form "/simple/example[2]".  Paths within predicates will definitely fail.</d:note>

    <d:para>To use the XPath package:</d:para>

    <d:programlisting>
package require xpath
</d:programlisting>

    <d:para>To parse a location path:</d:para>

    <d:programlisting>
xpath::split {/simple/example}
</d:programlisting>

    <d:para>This returns a Tcl list, each element of which is a three element sublist: {axis node-test {?predicate ...?}}.</d:para>
  </d:section>
</d:article>

