Index: apps/xslt.tcl ================================================================== --- apps/xslt.tcl +++ apps/xslt.tcl @@ -26,35 +26,39 @@ # # Contributor(s): # # # $Log$ -# Revision 1.1 2002/02/22 01:05:34 rolf -# Initial revision +# Revision 1.2 2002/02/26 14:05:02 rolf +# Updated the [load ...] to the new version number 0.7 +# +# Revision 1.1.1.1 2002/02/22 01:05:34 rolf +# tDOM0.7test with Jochens first set of patches # # # # written by Rolf Ade # August, 2001 # #---------------------------------------------------------------------------- -load [file dirname [info script]]/../unix/tdom0.6.so -source [file dirname [info script]]/../lib/tdom.tcl +if {[catch {package require tdom} errMsg]} { + if {[catch { + load [file dirname [info script]]/../unix/tdom0.7.so + source [file dirname [info script]]/../lib/tdom.tcl + }]} { + puts $errMsg + } +} #---------------------------------------------------------------------------- # externalEntityRefHandler # #---------------------------------------------------------------------------- proc externalEntityRefHandler { base systemId publicId } { - # puts stderr "externalEntityRefHandler:" - # puts stderr "\tbase $base" - # puts stderr "\tsystemId $systemId" - # puts stderr "\tpublicId $publicId" - if {[regexp {^[a-zA-Z]+:/} $systemId]} { # Seems to be not relative to the base if {[regexp { *file://(.*)} $systemId dummy path]} { set fd [open $path] fconfigure $fd -translation binary DELETED doc/INDEX.MAP Index: doc/INDEX.MAP ================================================================== --- doc/INDEX.MAP +++ /dev/null @@ -1,65 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - expatapi - tdomcmd - - - dom - domNode - - - domDoc - - - dom - domNode - tdomcmd - - - domDoc - - - dom - domNode - - - dom - domNode - - - expat - tdomcmd - - - tnc - - - dom - domNode - - - Index: doc/dom.xml ================================================================== --- doc/dom.xml +++ doc/dom.xml @@ -68,12 +68,12 @@ handle namespaces. -html - If -html is specified, a fast HTML parser is used, which tries -to even parse badly formed HTML into a DOM tree. + If -html is specified, a fast HTML parser is +used, which tries to even parse badly formed HTML into a DOM tree. -keepEmpties If -keepEmpties is @@ -85,13 +85,14 @@ -channel <channel-ID> If -channel <channel-ID> is specified, the -input to be parsed is read from the specified channel. The input is read from -the channel in "raw" (binary) mode from the channel, ignoring any encoding -setting via -fconfigure of the channel. +input to be parsed is read from the specified channel. The encoding setting of +the channel (via fconfigure -encoding) is respected, ie the data read from the +channel are converted to UTF-8 according to the encoding settings, befor the +data is parsed. -baseurl <baseURI> @@ -145,10 +146,21 @@ node name docElemName. The objVar controlls the memory handling as explained above.

+ + dom createDocumentNS +docElemName uri ?objVar? + +

Creates a new DOM document object with one element node with +node name docElemName. Uri gives the namespace of the +document element to create. The objVar controlls the +memory handling as explained above.

+
+
+ dom setResultEncoding ?encodingName?

If encodingName is not given the current global result encoding is returned. Otherwise the global result encoding is set to Index: doc/domNode.xml ================================================================== --- doc/domNode.xml +++ doc/domNode.xml @@ -293,23 +293,21 @@ Otherwise an error is generated. prefix - ...todo... Returns the namespace prefix. + Returns the namespace prefix. namespaceURI - ...todo... Returns the URI from the namespace of the given -node. + Returns the URI from the namespace of the given node. - namespaceURI - ...todo... Returns the localName from the tag name of the given -node. + localName + Returns the localName from the tag name of the given node. selectNodes xpathQuery Returns the result of applying the XPath query xpathQuery DELETED doc/make-index.xsl Index: doc/make-index.xsl ================================================================== --- doc/make-index.xsl +++ /dev/null @@ -1,179 +0,0 @@ - - - - - - - - - - - - - - - This file was automatically generated - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - option - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Unclassified name in / - - - - - - - - - DELETED doc/my-keyword-index.xsl Index: doc/my-keyword-index.xsl ================================================================== --- doc/my-keyword-index.xsl +++ /dev/null @@ -1,98 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - <xsl:value-of select="@title"/>: Keyword Index - - - - - -

- - -
-
-
- - -
-
-
- - - - - - - - - - - - - - - - Keywords: - - - - - - - - - - - - - - - - - - - - - - - - - - DELETED doc/navpages.xsl Index: doc/navpages.xsl ================================================================== --- doc/navpages.xsl +++ /dev/null @@ -1,74 +0,0 @@ - - - - · - - - - - - - -

:

- - -
- - - - - - - - - - - - - - - - -Unrecognized GI UNRECOGNIZED - -
DELETED doc/tmml.dtd Index: doc/tmml.dtd ================================================================== --- doc/tmml.dtd +++ /dev/null @@ -1,382 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -]]> - - - - - - - - - - - - - - - - - - - - - - - - - - - - -]]> - Index: extensions/tnc/tnc.c ================================================================== --- extensions/tnc/tnc.c +++ extensions/tnc/tnc.c @@ -176,15 +176,14 @@ if (code > 0 && code < sizeof(message)/sizeof(message[0])) return message[code]; return 0; } - #define UTF8_1BYTE_CHAR(c) ( 0 == ((c) & 0x80)) -#define UTF8_2BYTE_CHAR(c) ( 0xC0 == ((c) & 0xC0)) -#define UTF8_3BYTE_CHAR(c) ( 0xE0 == ((c) & 0xE0)) -#define UTF8_4BYTE_CHAR(c) ( 0xF0 == ((c) & 0xF0)) +#define UTF8_2BYTE_CHAR(c) ( 0xC0 == ((c) & 0xE0)) +#define UTF8_3BYTE_CHAR(c) ( 0xE0 == ((c) & 0xF0)) +#define UTF8_4BYTE_CHAR(c) ( 0xF0 == ((c) & 0xF8)) /* The following 2 defines are out of the expat code */ /* A 2 byte UTF-8 representation splits the characters 11 bits between the bottom 5 and 6 bits of the bytes. Index: generic/dom.c ================================================================== --- generic/dom.c +++ generic/dom.c @@ -34,12 +34,31 @@ | 01 Rolf Ade baseURI stuff, ID support, external | entities, tdom command | | | $Log$ -| Revision 1.1 2002/02/22 01:05:34 rolf -| Initial revision +| Revision 1.6 2002/03/10 01:16:12 rolf +| Added support for [dom createDocumentNS]. Added tests for correctness +| of document Element tag name. +| +| Revision 1.5 2002/03/07 22:11:32 rolf +| Freeze of actual state, befor feeding stuff to Jochen. +| +| Revision 1.4 2002/03/01 01:30:20 rolf +| No real code changes. Only to log additional change in 1.3: +| Fixed namespace overflow throu repeated (identical in prefix and uri) +| namespace declarations. +| +| Revision 1.3 2002/03/01 01:18:22 rolf +| Changed parsing. [dom parse ..] now uses Tcl_GetStringFromObj(), +| dom parse -channel now respects the encoding of the channel. +| +| Revision 1.2 2002/02/23 01:13:33 rolf +| Some code tweaking for a mostly warning free MS build +| +| Revision 1.1.1.1 2002/02/22 01:05:34 rolf +| tDOM0.7test with Jochens first set of patches | | | | written by Jochen Loewer | April 5, 1999 @@ -245,10 +264,54 @@ domProcessingInstructionNode * coerceToProcessingInstructionNode( domNode *n ) { return (domProcessingInstructionNode *)n; } +/*--------------------------------------------------------------------------- +| domIsNAME +| +\--------------------------------------------------------------------------*/ +static int +domIsNAME ( + char *name + ) +{ + char *p; + + p = name; + if (!isNameStart(p)) return 0; + p += UTF8_CHAR_LEN(*p); + while (*p) { + if (isNameChar(p)) + p += UTF8_CHAR_LEN(*p); + else return 0; + } + return 1; +} + + +/*--------------------------------------------------------------------------- +| domIsNCNAME +| +\--------------------------------------------------------------------------*/ +static int +domIsNCNAME ( + char *name + ) +{ + char *p; + + p = name; + if (!isNCNameStart(p)) return 0; + p += UTF8_CHAR_LEN(*p); + while (*p) { + if (isNCNameChar(p)) + p += UTF8_CHAR_LEN(*p); + else return 0; + } + return 1; +} /*--------------------------------------------------------------------------- | domLookupNamespace | \--------------------------------------------------------------------------*/ @@ -297,10 +360,38 @@ ns = ns->next; } return NULL; } +/*--------------------------------------------------------------------------- +| domIsNamespaceInScope +| +\--------------------------------------------------------------------------*/ +int +domIsNamespaceInScope ( + domActiveNS *NSstack, + int NSstackPos, + char *prefix, + char *namespaceURI + ) +{ + int i; + + for (i = NSstackPos; i >= 0; i--) { + if (strcmp(NSstack[i].namespace->prefix, prefix)==0) { + if (strcmp(NSstack[i].namespace->uri, namespaceURI)==0) { + /* OK, exactly the same namespace declaration is in scope */ + return 1; + } else { + /* This prefix is currently assigned to another uri, + we need a new NS declaration, to override this one */ + return 0; + } + } + } + return 0; +} /*--------------------------------------------------------------------------- | domLookupURI | \--------------------------------------------------------------------------*/ @@ -533,11 +624,11 @@ domNode *node, *parentNode, *toplevel; domLineColumn *lc; domAttrNode *attrnode, *lastAttr; const char **atPtr, **idAttPtr; Tcl_HashEntry *h; - int hnew, len, pos, idatt; + int hnew, len, pos, idatt, newNSdecls; char *xmlns, *localname; char tagPrefix[MAX_PREFIX_LEN]; char prefix[MAX_PREFIX_LEN]; domNS *ns; char feedbackCmd[24]; @@ -613,15 +704,19 @@ /*-------------------------------------------------------------- | process namespace declarations | \-------------------------------------------------------------*/ #ifdef TDOM_NS + newNSdecls = 0; for (atPtr = atts; atPtr[0] && atPtr[1]; atPtr += 2) { if (strncmp((char *)atPtr[0], "xmlns", 5) == 0) { xmlns = (char *)atPtr[0]; if (xmlns[5] == ':') { + if (domIsNamespaceInScope (info->activeNS, info->activeNSpos, + &(xmlns[6]), (char *)atPtr[1])) + continue; ns = domNewNamespace(info->document, &(xmlns[6]), (char *)atPtr[1]); } else { ns = domNewNamespace(info->document, "", (char *)atPtr[1]); } /* push active namespace */ @@ -632,16 +727,17 @@ sizeof(domActiveNS) * 2 * info->activeNSsize); info->activeNSsize = 2 * info->activeNSsize; } info->activeNS[info->activeNSpos].depth = info->depth; info->activeNS[info->activeNSpos].namespace = ns; - continue; + newNSdecls++; } } -#endif - -#ifdef TDOM_NS + if (newNSdecls) { + + } + /*---------------------------------------------------------- | look for namespace of element \---------------------------------------------------------*/ domSplitQName ((char*)name, tagPrefix, &localname); for (pos = info->activeNSpos; pos >= 0; pos--) { @@ -1054,21 +1150,21 @@ /*--------------------------------------------------------------------------- | entityDeclHandler | \--------------------------------------------------------------------------*/ static void -entityDeclHandler (userData, entityName, is_parameter_entity, value, - value_length, base, systemId, publicId, notationName) - void *userData; - const char *entityName; - int is_parameter_entity; - const char *value; - int value_length; - const char *base; - const char *systemId; - const char *publicId; - const char *notationName; +entityDeclHandler ( + void *userData, + const char *entityName, + int is_parameter_entity, + const char *value, + int value_length, + const char *base, + const char *systemId, + const char *publicId, + const char *notationName +) { domReadInfo *info = (domReadInfo *) userData; Tcl_HashEntry *entryPtr; int hnew; @@ -1084,16 +1180,17 @@ /*--------------------------------------------------------------------------- | externalEntityRefHandler | \--------------------------------------------------------------------------*/ static int -externalEntityRefHandler (parser, openEntityNames, base, systemId, publicId) - XML_Parser parser; - CONST char *openEntityNames; - CONST char *base; - CONST char *systemId; - CONST char *publicId; +externalEntityRefHandler ( + XML_Parser parser, + CONST char *openEntityNames, + CONST char *base, + CONST char *systemId, + CONST char *publicId +) { domReadInfo *info = (domReadInfo *) XML_GetUserData (parser); Tcl_Obj *cmdPtr, *resultObj, *resultTypeObj, *extbaseObj, *xmlstringObj; Tcl_Obj *channelIdObj; @@ -1234,16 +1331,17 @@ /*--------------------------------------------------------------------------- | startDoctypeDeclHandler | \--------------------------------------------------------------------------*/ void -startDoctypeDeclHandler (userData, doctypeName, sysid, pubid, has_internal_subset) - void *userData; - const char *doctypeName; - const char *sysid; - const char *pubid; - int has_internal_subset; +startDoctypeDeclHandler ( + void *userData, + const char *doctypeName, + const char *sysid, + const char *pubid, + int has_internal_subset +) { domReadInfo *info = (domReadInfo *) userData; info->insideDTD = 1; } @@ -1251,12 +1349,13 @@ /*--------------------------------------------------------------------------- | endDoctypeDeclHandler | \--------------------------------------------------------------------------*/ void -endDoctypeDeclHandler (userData) - void *userData; +endDoctypeDeclHandler ( + void *userData +) { domReadInfo *info = (domReadInfo *) userData; info->insideDTD = 0; } @@ -1284,10 +1383,16 @@ domLineColumn *lc; domNode *rootNode; int hnew, done, len; domReadInfo info; char buf[1024]; +#if !TclOnly8Bits + Tcl_Obj *bufObj; + Tcl_DString dStr; + int useBinary; + char *str; +#endif domDocument *doc = (domDocument*) Tcl_Alloc(sizeof(domDocument)); GetTDomTSD(); if (!TSDPTR(domModuleIsInitialized)) { @@ -1299,13 +1404,15 @@ if (encoding_8bit) doc->nodeFlags |= USE_8_BIT_ENCODING; doc->documentNumber = ++TSDPTR(domUniqueDocNr); doc->ids = (Tcl_HashTable *)Tcl_Alloc (sizeof (Tcl_HashTable)); doc->unparsedEntities = (Tcl_HashTable *)Tcl_Alloc (sizeof (Tcl_HashTable)); doc->baseURIs = (Tcl_HashTable *)Tcl_Alloc (sizeof (Tcl_HashTable)); + doc->NSscopes = (Tcl_HashTable *)Tcl_Alloc (sizeof (Tcl_HashTable)); Tcl_InitHashTable (doc->ids, TCL_STRING_KEYS); Tcl_InitHashTable (doc->unparsedEntities, TCL_STRING_KEYS); Tcl_InitHashTable (doc->baseURIs, TCL_ONE_WORD_KEYS); + Tcl_InitHashTable (doc->NSscopes, TCL_ONE_WORD_KEYS); doc->extResolver = extResolver; info.parser = parser; info.document = doc; info.currentNode = NULL; @@ -1376,18 +1483,53 @@ if (!XML_Parse(parser, xml, length, 1)) { Tcl_Free ( (char*) info.activeNS ); return NULL; } } else { +#if !TclOnly8Bits + Tcl_DStringInit (&dStr); + if (Tcl_GetChannelOption (interp, channel, "-encoding", &dStr) != TCL_OK) { + Tcl_Free ( (char*) info.activeNS ); + return NULL; + } + if (strcmp (Tcl_DStringValue (&dStr), "binary")==0 ) useBinary = 1; + else useBinary = 0; + Tcl_DStringFree (&dStr); + if (useBinary) { + do { + len = Tcl_Read (channel, buf, sizeof(buf)); + done = len < sizeof(buf); + if (!XML_Parse (parser, buf, len, done)) { + Tcl_Free ( (char*) info.activeNS ); + return NULL; + } + } while (!done); + } else { + bufObj = Tcl_NewObj(); + Tcl_SetObjLength (bufObj, 6144); + do { + len = Tcl_ReadChars (channel, bufObj, 1024, 0); + done = (len < 1024); + str = Tcl_GetStringFromObj (bufObj, &len); + if (!XML_Parse (parser, str, len, done)) { + Tcl_Free ( (char*) info.activeNS ); + Tcl_DecrRefCount (bufObj); + return NULL; + } + } while (!done); + Tcl_DecrRefCount (bufObj); + } +#else do { len = Tcl_Read (channel, buf, sizeof(buf)); done = len < sizeof(buf); if (!XML_Parse (parser, buf, len, done)) { Tcl_Free ( (char*) info.activeNS ); return NULL; } } while (!done); +#endif } Tcl_Free ( (char*) info.activeNS ); rootNode->firstChild = doc->documentElement; while (rootNode->firstChild->previousSibling) { @@ -1482,14 +1624,16 @@ doc->nodeType = DOCUMENT_NODE; doc->documentNumber = ++TSDPTR(domUniqueDocNr); doc->ids = (Tcl_HashTable *)Tcl_Alloc (sizeof (Tcl_HashTable)); doc->unparsedEntities = (Tcl_HashTable *)Tcl_Alloc (sizeof (Tcl_HashTable)); doc->baseURIs = (Tcl_HashTable *)Tcl_Alloc (sizeof (Tcl_HashTable)); + doc->NSscopes = (Tcl_HashTable *)Tcl_Alloc (sizeof (Tcl_HashTable)); doc->documentElement = NULL; Tcl_InitHashTable (doc->ids, TCL_STRING_KEYS); Tcl_InitHashTable (doc->unparsedEntities, TCL_STRING_KEYS); Tcl_InitHashTable (doc->baseURIs, TCL_ONE_WORD_KEYS); + Tcl_InitHashTable (doc->NSscopes, TCL_ONE_WORD_KEYS); h = Tcl_CreateHashEntry( &TSDPTR(tagNames), "(rootNode)", &hnew); rootNode = (domNode*) domAlloc(sizeof(domNode)); memset(rootNode, 0, sizeof(domNode)); rootNode->nodeType = ELEMENT_NODE; @@ -1509,27 +1653,57 @@ | domCreateDocument | \--------------------------------------------------------------------------*/ domDocument * domCreateDocument ( - char *documentElementTagName + Tcl_Interp *interp, + char *documentElementTagName, + char *uri ) { Tcl_HashEntry *h; int hnew; domNode *node; domDocument *doc; + char prefix[MAX_PREFIX_LEN], *localName; + domNS *ns = NULL; GetTDomTSD(); + if (uri) { + domSplitQName (documentElementTagName, prefix, &localName); + DBG(fprintf (stderr, "rootName: -->%s<--, prefix: -->%s<--, localName: -->%s<--\n", documentElementTagName, prefix, localName);) + if (prefix[0] != '\0') { + if (!domIsNCNAME (prefix)) { + Tcl_SetObjResult (interp, Tcl_NewStringObj("invalid prefix name", -1)); + return NULL; + } + } + if (!domIsNCNAME (localName)) { + Tcl_SetObjResult (interp, Tcl_NewStringObj ("invalid local name", -1)); + return NULL; + } + } else { + if (!domIsNAME (documentElementTagName)) { + Tcl_SetObjResult (interp, Tcl_NewStringObj ("invalid root element name", -1)); + return NULL; + } + } doc = domCreateDoc (); h = Tcl_CreateHashEntry( &TSDPTR(tagNames), documentElementTagName, &hnew); + if (uri) { + ns = domNewNamespace (doc, prefix, uri); + } node = (domNode*) domAlloc(sizeof(domNode)); memset(node, 0, sizeof(domNode)); node->nodeType = ELEMENT_NODE; node->nodeFlags = 0; - node->namespace = 0; + if (uri) { + node->namespace = ns->index; + } else { + node->namespace = 0; + } node->nodeNumber = ++TSDPTR(domUniqueNodeNr); node->ownerDocument = doc; node->nodeName = (char *)&(h->key); doc->documentElement = node; doc->ids = (Tcl_HashTable *)Tcl_Alloc (sizeof(Tcl_HashTable)); @@ -1798,11 +1972,11 @@ } if (attr) { if (attr->nodeFlags & IS_ID_ATTRIBUTE) { h = Tcl_FindHashEntry (node->ownerDocument->ids, attr->nodeValue); if (h) { - tmp = Tcl_GetHashValue (h); + tmp = (domNode *)Tcl_GetHashValue (h); Tcl_DeleteHashEntry (h); h = Tcl_CreateHashEntry (node->ownerDocument->ids, attributeValue, &hnew); /* XXX what to do, if hnew = 0 ??? */ Tcl_SetHashValue (h, tmp); @@ -3218,13 +3392,14 @@ EXTERN int tcldom_returnDocumentObj (Tcl_Interp *interp, domDocument *document, int setVariable, Tcl_Obj *var_name); void -tdom_freeProc (interp, userData) - Tcl_Interp *interp; - void *userData; +tdom_freeProc ( + Tcl_Interp *interp, + void *userData +) { char objCmdName[40]; Tcl_CmdInfo cmd_info; domReadInfo *info = (domReadInfo *) userData; @@ -3237,13 +3412,14 @@ } Tcl_Free ( (char *) info); } void -tdom_resetProc (interp, userData) - Tcl_Interp *interp; - void *userData; +tdom_resetProc ( + Tcl_Interp *interp, + void *userData +) { char objCmdName[40]; Tcl_CmdInfo cmd_info; domReadInfo *info = (domReadInfo *) userData; domDocument *doc; @@ -3259,13 +3435,15 @@ doc->nodeType = DOCUMENT_NODE; doc->documentNumber = ++TSDPTR(domUniqueDocNr); doc->ids = (Tcl_HashTable *)Tcl_Alloc (sizeof (Tcl_HashTable)); doc->unparsedEntities = (Tcl_HashTable *)Tcl_Alloc (sizeof (Tcl_HashTable)); doc->baseURIs = (Tcl_HashTable *)Tcl_Alloc (sizeof (Tcl_HashTable)); + doc->NSscopes = (Tcl_HashTable *)Tcl_Alloc (sizeof (Tcl_HashTable)); Tcl_InitHashTable (doc->ids, TCL_STRING_KEYS); Tcl_InitHashTable (doc->unparsedEntities, TCL_STRING_KEYS); Tcl_InitHashTable (doc->baseURIs, TCL_ONE_WORD_KEYS); + Tcl_InitHashTable (doc->NSscopes, TCL_ONE_WORD_KEYS); info->document = doc; info->currentNode = NULL; info->depth = 0; info->ignoreWhiteSpaces = 1; @@ -3277,13 +3455,14 @@ info->currentNSIndex = 0; info->activeNSpos = -1; } void -tdom_parserResetProc (parser, userData) - XML_Parser parser; - void *userData; +tdom_parserResetProc ( + XML_Parser parser, + void *userData +) { domReadInfo *info = (domReadInfo *) userData; info->parser = parser; } @@ -3304,12 +3483,10 @@ domLineColumn *lc; Tcl_HashEntry *h; TclGenExpatInfo *expat; Tcl_Obj *newObjName = NULL; TEncoding *encoding; - GetTDomTSD(); - static char *tdomMethods[] = { "enable", "getdoc", "setResultEncoding", "setStoreLineColumn", "setExternalEntityResolver", "keepEmpties", @@ -3320,10 +3497,11 @@ m_enable, m_getdoc, m_setResultEncoding, m_setStoreLineColumn, m_setExternalEntityResolver, m_keepEmpties, m_remove, }; + GetTDomTSD(); if (objc < 3 || objc > 4) { Tcl_WrongNumArgs (interp, 1, objv, tdom_usage); return TCL_ERROR; } @@ -3364,13 +3542,15 @@ doc->nodeType = DOCUMENT_NODE; doc->documentNumber = ++TSDPTR(domUniqueDocNr); doc->ids = (Tcl_HashTable *)Tcl_Alloc (sizeof (Tcl_HashTable)); doc->unparsedEntities = (Tcl_HashTable *)Tcl_Alloc (sizeof (Tcl_HashTable)); doc->baseURIs = (Tcl_HashTable *)Tcl_Alloc (sizeof (Tcl_HashTable)); + doc->NSscopes = (Tcl_HashTable *)Tcl_Alloc (sizeof (Tcl_HashTable)); Tcl_InitHashTable (doc->ids, TCL_STRING_KEYS); Tcl_InitHashTable (doc->unparsedEntities, TCL_STRING_KEYS); Tcl_InitHashTable (doc->baseURIs, TCL_ONE_WORD_KEYS); + Tcl_InitHashTable (doc->NSscopes, TCL_ONE_WORD_KEYS); info = (domReadInfo *) Tcl_Alloc (sizeof (domReadInfo)); info->document = doc; info->currentNode = NULL; info->depth = 0; Index: generic/dom.h ================================================================== --- generic/dom.h +++ generic/dom.h @@ -27,12 +27,22 @@ | | Contributor(s): | | | $Log$ -| Revision 1.1 2002/02/22 01:05:35 rolf -| Initial revision +| Revision 1.4 2002/03/10 01:14:57 rolf +| Introduced distinction between XML Name and XML NC Name. +| +| Revision 1.3 2002/03/07 22:09:46 rolf +| Added infrastructur to be able to do NCNAME tests. +| Freeze of actual state, befor feeding stuff to Jochen. +| +| Revision 1.2 2002/02/24 02:31:27 rolf +| Fixed UTF-8 char byte length determination +| +| Revision 1.1.1.1 2002/02/22 01:05:35 rolf +| tDOM0.7test with Jochens first set of patches | | | | written by Jochen Loewer | April 5, 1999 @@ -102,10 +112,198 @@ #define TclOnly8Bits 1 #else #define TclOnly8Bits 0 #endif +#define UTF8_1BYTE_CHAR(c) ( 0 == ((c) & 0x80)) +#define UTF8_2BYTE_CHAR(c) ( 0xC0 == ((c) & 0xE0)) +#define UTF8_3BYTE_CHAR(c) ( 0xE0 == ((c) & 0xF0)) +#define UTF8_4BYTE_CHAR(c) ( 0xF0 == ((c) & 0xF8)) + +#if TclOnly8Bits +#define UTF8_CHAR_LEN(c) 1 +#else +#define UTF8_CHAR_LEN(c) \ + UTF8_1BYTE_CHAR((c)) ? 1 : \ + (UTF8_2BYTE_CHAR((c)) ? 2 : \ + (UTF8_3BYTE_CHAR((c)) ? 3 : 0)) +#endif + +/* The following 2 defines are out of the expat code */ + +/* A 2 byte UTF-8 representation splits the characters 11 bits +between the bottom 5 and 6 bits of the bytes. +We need 8 bits to index into pages, 3 bits to add to that index and +5 bits to generate the mask. */ +#define UTF8_GET_NAMING2(pages, byte) \ + (namingBitmap[((pages)[(((byte)[0]) >> 2) & 7] << 3) \ + + ((((byte)[0]) & 3) << 1) \ + + ((((byte)[1]) >> 5) & 1)] \ + & (1 << (((byte)[1]) & 0x1F))) + +/* A 3 byte UTF-8 representation splits the characters 16 bits +between the bottom 4, 6 and 6 bits of the bytes. +We need 8 bits to index into pages, 3 bits to add to that index and +5 bits to generate the mask. */ +#define UTF8_GET_NAMING3(pages, byte) \ + (namingBitmap[((pages)[((((byte)[0]) & 0xF) << 4) \ + + ((((byte)[1]) >> 2) & 0xF)] \ + << 3) \ + + ((((byte)[1]) & 3) << 1) \ + + ((((byte)[2]) >> 5) & 1)] \ + & (1 << (((byte)[2]) & 0x1F))) + +#define UTF8_GET_NAMING_NMTOKEN(p, n) \ + ((n) == 1 \ + ? nameChar7Bit[(int)(*(p))] \ + : ((n) == 2 \ + ? UTF8_GET_NAMING2(nmstrtPages, (const unsigned char *)(p)) \ + : ((n) == 3 \ + ? UTF8_GET_NAMING3(nmstrtPages, (const unsigned char *)(p)) \ + : 0))) + +#define UTF8_GET_NAMING_NCNMTOKEN(p, n) \ + ((n) == 1 \ + ? NCnameChar7Bit[(int)(*(p))] \ + : ((n) == 2 \ + ? UTF8_GET_NAMING2(nmstrtPages, (const unsigned char *)(p)) \ + : ((n) == 3 \ + ? UTF8_GET_NAMING3(nmstrtPages, (const unsigned char *)(p)) \ + : 0))) + + +#define UTF8_GET_NAMING_NAME(p, n) \ + ((n) == 1 \ + ? nameStart7Bit[(int)(*(p))] \ + : ((n) == 2 \ + ? UTF8_GET_NAMING2(namePages, (const unsigned char *)(p)) \ + : ((n) == 3 \ + ? UTF8_GET_NAMING3(namePages, (const unsigned char *)(p)) \ + : 0))) + +#define UTF8_GET_NAMING_NCNAME(p, n) \ + ((n) == 1 \ + ? NCnameStart7Bit[(int)(*(p))] \ + : ((n) == 2 \ + ? UTF8_GET_NAMING2(namePages, (const unsigned char *)(p)) \ + : ((n) == 3 \ + ? UTF8_GET_NAMING3(namePages, (const unsigned char *)(p)) \ + : 0))) + + +#include "../expat-1.95.1/nametab.h" + +static const unsigned char nameChar7Bit[] = { +/* 0x00 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +/* 0x08 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +/* 0x10 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +/* 0x18 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +/* 0x20 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +/* 0x28 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, +/* 0x30 */ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, +/* 0x38 */ 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, +/* 0x40 */ 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, +/* 0x48 */ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, +/* 0x50 */ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, +/* 0x58 */ 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, +/* 0x60 */ 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, +/* 0x68 */ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, +/* 0x70 */ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, +/* 0x78 */ 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, +}; + +static const unsigned char NCnameChar7Bit[] = { +/* 0x00 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +/* 0x08 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +/* 0x10 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +/* 0x18 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +/* 0x20 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +/* 0x28 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, +/* 0x30 */ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, +/* 0x38 */ 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +/* 0x40 */ 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, +/* 0x48 */ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, +/* 0x50 */ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, +/* 0x58 */ 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, +/* 0x60 */ 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, +/* 0x68 */ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, +/* 0x70 */ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, +/* 0x78 */ 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, +}; + + +static const unsigned char nameStart7Bit[] = { +/* 0x00 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +/* 0x08 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +/* 0x10 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +/* 0x18 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +/* 0x20 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +/* 0x28 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +/* 0x30 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +/* 0x38 */ 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, +/* 0x40 */ 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, +/* 0x48 */ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, +/* 0x50 */ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, +/* 0x58 */ 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, +/* 0x60 */ 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, +/* 0x68 */ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, +/* 0x70 */ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, +/* 0x78 */ 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, +}; + + +static const unsigned char NCnameStart7Bit[] = { +/* 0x00 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +/* 0x08 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +/* 0x10 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +/* 0x18 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +/* 0x20 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +/* 0x28 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +/* 0x30 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +/* 0x38 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +/* 0x40 */ 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, +/* 0x48 */ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, +/* 0x50 */ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, +/* 0x58 */ 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, +/* 0x60 */ 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, +/* 0x68 */ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, +/* 0x70 */ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, +/* 0x78 */ 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, +}; + + +#if TclOnly8Bits == 1 +# define isNameStart(x) (isalpha(*x) || ((*x)=='_') || ((*x)==':')) +# define isNameChar(x) (isalnum(*x) || ((*x)=='_') || ((*x)=='-') || ((*x)=='.') || ((*x)==':')) +# define isNCNameStart(x) (isalpha(*x) || ((*x)=='_')) +# define isNCNameChar(x) (isalnum(*x) || ((*x)=='_') || ((*x)=='-') || ((*x)=='.')) +#else +static int isNameStart(char *c) +{ + int clen; + clen = UTF8_CHAR_LEN (*c); + return (UTF8_GET_NAMING_NAME(c, clen)); +} +static int isNCNameStart(char *c) +{ + int clen; + clen = UTF8_CHAR_LEN (*c); + return (UTF8_GET_NAMING_NCNAME(c, clen)); +} +static int isNameChar(char *c) +{ + int clen; + clen = UTF8_CHAR_LEN (*c); + return (UTF8_GET_NAMING_NMTOKEN(c, clen)); +} +static int isNCNameChar(char *c) +{ + int clen; + clen = UTF8_CHAR_LEN (*c); + return (UTF8_GET_NAMING_NCNMTOKEN(c, clen)); +} +#endif /*-------------------------------------------------------------------------- | DOMString | \-------------------------------------------------------------------------*/ @@ -143,11 +341,11 @@ #define HAS_LINE_COLUMN 1 #define VISIBLE_IN_TCL 2 #define IS_ID_ATTRIBUTE 4 #define HAS_BASEURI 8 #define DISABLE_OUTPUT_ESCAPING 16 - +#define HAS_NS_INFO 32 typedef int domDocFlags; #define OUTPUT_DEFAULT_XML 1 #define OUTPUT_DEFAULT_HTML 2 @@ -201,10 +399,11 @@ struct domNS *namespaces; struct domNode *rootNode; Tcl_HashTable *ids; Tcl_HashTable *unparsedEntities; Tcl_HashTable *baseURIs; + Tcl_HashTable *NSscopes; Tcl_Obj *extResolver; } domDocument; /*-------------------------------------------------------------------------- @@ -344,11 +543,13 @@ \-------------------------------------------------------------------------*/ char * domException2String (domException expection); void domModuleInitialize (void); domDocument * domCreateDoc (); -domDocument * domCreateDocument (char *documentElementTagName); +domDocument * domCreateDocument (Tcl_Interp *interp, + char *documentElementTagName, + char *uri); domDocument * domReadDocument (XML_Parser parser, char *xml, int length, int ignoreWhiteSpaces, Index: generic/domalloc.c ================================================================== --- generic/domalloc.c +++ generic/domalloc.c @@ -29,12 +29,15 @@ | Contributor(s): | | | | $Log$ -| Revision 1.1 2002/02/22 01:05:35 rolf -| Initial revision +| Revision 1.2 2002/03/08 23:49:25 rolf +| Fixed small, obvious bug in DBG code. +| +| Revision 1.1.1.1 2002/02/22 01:05:35 rolf +| tDOM0.7test with Jochens first set of patches | | | | written by Jochen Loewer | October, 2000 @@ -210,11 +213,11 @@ unsigned int mask; char * mem; unsigned int * usedBitmap; - DBG(fprintf(stderr, "\ndomAlloc %d ... %d \n", size, ++invokes);) + DBG(fprintf(stderr, "\ndomAlloc %d \n", size);) if (size >= MAX_BINS) { fprintf(stderr, "\nSize too large as used for bin!\n"); return NULL; } Index: generic/domxpath.c ================================================================== --- generic/domxpath.c +++ generic/domxpath.c @@ -36,12 +36,20 @@ | bug fixes/reports | | Aug01 Rolf Ade id(), unparsed-entity(), lang(), fixes | | $Log$ -| Revision 1.1 2002/02/22 01:05:35 rolf -| Initial revision +| Revision 1.3 2002/03/07 22:16:21 rolf +| Improved recognition of REALs (of the form .123). Made variable name +| recognition UTF-8 save. +| Freeze of actual state, befor feeding stuff to Jochen. +| +| Revision 1.2 2002/02/23 01:13:33 rolf +| Some code tweaking for a mostly warning free MS build +| +| Revision 1.1.1.1 2002/02/22 01:05:35 rolf +| tDOM0.7test with Jochens first set of patches | | | | written by Jochen Loewer | July, 1999 @@ -134,12 +142,10 @@ strcat(*errMsg, ": Expected " #msg); \ return NULL; #define CHECK_RC if (rc) return rc - - /*---------------------------------------------------------------------------- | Types for Lexer | \---------------------------------------------------------------------------*/ typedef enum { @@ -189,11 +195,11 @@ "AxisFollowingSibling", "AxisNamespace", "AxisParent", "AxisPreceding", "AxisPrecedingSilbing", "AxisSelf", "GetContextNode", "GetParentNode", "CombinePath", "IsRoot", "ToParent", "ToAncestors", "FillNodeList", - "FillWithCurrentNode", + "FillWithCurrentNode", "ExecIdKey" }; @@ -207,11 +213,13 @@ DeclProduction(RelativeLocationPath); DeclProduction(AbsoluteLocationPath); char *xpathFuncString (xpathResultSet *rs ); - +#ifndef isnan +int isnan(double number) { return (!(number == number)); } +#endif /*---------------------------------------------------------------------------- | xpath result set functions | \---------------------------------------------------------------------------*/ @@ -680,17 +688,10 @@ case '(': token = LPAR; break; case ')': token = RPAR; break; case '[': token = LBRACKET; break; case ']': token = RBRACKET; break; - case '.': if (xpath[i+1] == '.') { - token = DOTDOT; - i++; - } else { - token = DOT; - }; break; - case '@': i++; if ( (isalpha(xpath[i])) || (xpath[i]== '_') ) { ps = &(xpath[i++]); while (xpath[i] && (isalnum(xpath[i]) || @@ -787,29 +788,36 @@ token = WCARDNAME; tokens[l].strvalue = (char*)strdup("*"); }; break; case '$': i++; - if (isalpha(xpath[i]) || xpath[i] == '_') { - ps = &(xpath[i++]); - while (xpath[i] && - (isalnum(xpath[i]) || - (xpath[i]== '_') || - (xpath[i]== '-') || - (xpath[i]== '.') - ) - ) i++; + if ( isNameStart (&xpath[i])) { + ps = &(xpath[i]); + i += UTF8_CHAR_LEN (xpath[i]); + while (xpath[i] && isNameChar(&xpath[i])) + i += UTF8_CHAR_LEN(xpath[i]); save = xpath[i]; xpath[i] = '\0'; tokens[l].strvalue = (char*)strdup(ps); xpath[i--] = save; token = VARIABLE; } else { free(tokens); *errMsg = (char*)strdup("Expected variable name"); return NULL; - } break; + }; break; + + case '.': if (xpath[i+1] == '.') { + token = DOTDOT; + i++; + break; + } else if (!isdigit(xpath[i+1])) { + token = DOT; + break; + } + /* DOT followed by digit, ie a REAL. + Handled by default. Fall throu */ default: if ( (isalpha(xpath[i])) || (xpath[i]== '_') ) { ps = &(xpath[i++]); while (xpath[i] && (isalnum(xpath[i]) || @@ -926,13 +934,17 @@ tokens[l].strvalue = (char*)strdup(ps); } xpath[i] = save; } i--; - } else if (isdigit(xpath[i])) { + } else if (isdigit(xpath[i]) || (xpath[i] == '.')) { + if (xpath[i] == '.') { + token = REALNUMBER; + } else { + token = INTNUMBER; + } ps = &(xpath[i++]); - token = INTNUMBER; while (xpath[i] && isdigit(xpath[i])) i++; if (xpath[i]=='.') { token = REALNUMBER; i++; while (xpath[i] && isdigit(xpath[i])) i++; @@ -1843,10 +1855,12 @@ step->child->strvalue)==0); } return 0; } else if (step->child->type == IsFQElement) { + if (node->nodeType != ELEMENT_NODE && node->nodeType != ATTRIBUTE_NODE) + return 0; contextNS = domLookupPrefix (exprContext, step->child->strvalue); if (!contextNS) return 0; /* Hmmm, that's more an error, than a not match */ nodeUri = domNamespaceURI (node); if (!nodeUri) return 0; if (strcmp (contextNS->uri, nodeUri) != 0) return 0; @@ -2227,11 +2241,11 @@ char **errMsg ) { xpathResultSet leftResult, rightResult, replaceResult; xpathResultSet pleftResult, prightResult, tResult; - int i, j, k, rc, res, pwhite, from, len, NaN; + int i, j, k, rc, res, pwhite, len, NaN; char *replaceStr, *pfrom, *pto, tmp[80], tmp1[80], *uri; domNode *node, *child, *startingNode, *ancestor; domDocument *doc; domAttrNode *attr; double leftReal; @@ -2238,11 +2252,11 @@ ast nextStep; int argc, savedDocOrder; xpathResultSets *args; xpathResultSet *arg; Tcl_HashEntry *entryPtr; - unsigned int leftNodeNr, rightNodeNr; + unsigned int from, leftNodeNr, rightNodeNr; int left = 0, right = 0; double dLeft = 0.0, dRight = 0.0; char *leftStr = NULL, *rightStr = NULL; #if TclOnly8Bits char *fStr; @@ -2292,33 +2306,33 @@ rsAddNode( result, ctxNode); } startingNode = ctxNode; node = ctxNode->firstChild; while (node && node != startingNode) { - if (xpathNodeTest(node, exprContext, step)) rsAddNode( result, node); - if ((node->nodeType == ELEMENT_NODE) && (node->firstChild)) { - node = node->firstChild; - continue; - } - if (node->nextSibling) { - node = node->nextSibling; - continue; - } - while ( node->parentNode && - (node->parentNode != startingNode) && - (node->parentNode->nextSibling == NULL) ) { - - node = node->parentNode; - } - if ((node != startingNode) && - (node->parentNode) && - (node->parentNode != startingNode) - ) { - node = node->parentNode->nextSibling; - } else { - break; - } + if (xpathNodeTest(node, exprContext, step)) rsAddNode( result, node); + if ((node->nodeType == ELEMENT_NODE) && (node->firstChild)) { + node = node->firstChild; + continue; + } + if (node->nextSibling) { + node = node->nextSibling; + continue; + } + while ( node->parentNode && + (node->parentNode != startingNode) && + (node->parentNode->nextSibling == NULL) ) { + + node = node->parentNode; + } + if ((node != startingNode) && + (node->parentNode) && + (node->parentNode != startingNode) + ) { + node = node->parentNode->nextSibling; + } else { + break; + } } } else if (step->type == AxisSelf) { *docOrder = 1; Index: generic/domxslt.c ================================================================== --- generic/domxslt.c +++ generic/domxslt.c @@ -34,16 +34,42 @@ | handling of toplevel var/parameter, | plenty of fixes and enhancements all | over the place. | | $Log$ -| Revision 1.1 2002/02/22 01:05:35 rolf -| Initial revision +| Revision 1.6 2002/03/16 13:06:06 rolf +| Optimised xsl:sort: the string or numeric value of the nodes to sort +| are computed only once and cached for further comparisons in the sort +| process. +| +| Revision 1.5 2002/03/03 20:08:25 rolf +| Improved detection of improper stylesheets: now triggers error if any +| mandatory attribute of a xsl element is missing. The mandatory +| attributes are: xsl:stylesheet: version; xsl:transform: version; +| xsl:include: href; xsl:import: href; xsl:strip-space: elements; +| xsl:preserve-space: elements; xsl:call-template: name; +| xsl:namespace-alias: stylesheet-prefix result-prefix; xsl:element: +| name; xsl:attribute: name; xsl:attribute-set: name; +| xsl:processing-instruction: name; xsl:value-of: select; xsl:for-each: +| select; xsl:if: test; xsl:when: test; xsl:variable: name; xsl:param: +| name; xsl:copy-of: select; xsl:with-param: name; xsl:key: name match +| use +| +| Revision 1.4 2002/03/01 04:06:59 rolf +| Improved detection of improper stylesheets: xsl:include xsl:import +| xsl:strip-space xsl:preserve-space xsl:apply-imports +| xsl:namespace-alias xsl:value-of xsl:number xsl:sort xsl:copy-of +| xsl:key xsl:decimal-format and xsl:output must be emtpy. +| +| Revision 1.3 2002/02/24 02:31:27 rolf +| Fixed UTF-8 char byte length determination | -| Revision 1.5 2002/02/04 22:10:59 jolo -| rolf 04022002 +| Revision 1.2 2002/02/23 01:13:33 rolf +| Some code tweaking for a mostly warning free MS build | +| Revision 1.1.1.1 2002/02/22 01:05:35 rolf +| tDOM0.7test with Jochens first set of patches | | | written by Jochen Loewer | June, 2000 | @@ -128,11 +154,11 @@ a_format, a_from, a_href, a_lang, a_level, a_match, a_mediaType, a_method, a_mode, a_name, a_namespace, a_order, a_prio, a_select, a_space, a_terminate, a_test, a_use, a_useAttributeSets, a_value, a_groupingSeparator, a_groupingSize, a_decimalSeparator, a_infinity, a_minusSign, a_nan, a_percent, - a_perMille, a_zeroDigit, a_digit, a_patternSeparator + a_perMille, a_zeroDigit, a_digit, a_patternSeparator, a_version } xsltAttr; @@ -389,25 +415,10 @@ static domDocument * getExternalDocument (Tcl_Interp *interp, xsltState *xs, domDocument *xsltDoc, char *baseURI, char *href, char **errMsg); -/*---------------------------------------------------------------------------- -| UtfCount -| -\---------------------------------------------------------------------------*/ -static int -UtfCount( - int ch -) -{ - if ((ch > 0) && (ch < 0x80)) { return 1; } - if (ch <= 0x7FF) { return 2; } - if (ch <= 0xFFFF) { return 3; } - return 0; -} - /*---------------------------------------------------------------------------- | printXML | \---------------------------------------------------------------------------*/ static void printXML (domNode *node, int level, int maxlevel) { @@ -734,11 +745,11 @@ format->maxtokens = 20; Tcl_SetHashValue (h, format); format->formatStr = p = Tcl_GetHashKey (&(xs->formats), h); } while (*p) { - clen = UtfCount (*p); + clen = UTF8_CHAR_LEN(*p); if (!clen) { *errMsg = strdup("xsl:number: UTF-8 form of character longer than 3 Byte"); return NULL; } @@ -763,11 +774,11 @@ p++; \ if (*p) { \ format->tokens[nrOfTokens].sepStart = p; \ } \ while (*p) { \ - clen = UtfCount (*p); \ + clen = UTF8_CHAR_LEN(*p); \ if (!clen) { \ *errMsg = strdup("xsl:number: UTF-8 form of character longer than 3 Byte"); \ return NULL; \ } \ if (clen > 1) { \ @@ -1356,11 +1367,11 @@ nsname = domNamespaceURI (node); if (nsname) { h = Tcl_FindHashEntry (&(xs->stripInfo.NSWildcards), nsname); if (h) { strip = 1; - f = Tcl_GetHashValue (h); + f = (double *)Tcl_GetHashValue (h); if (*f >= stripPrecedence) { stripPrecedence = *f; stripPrio = -0.25; } } @@ -1370,21 +1381,21 @@ Tcl_DStringAppend (&dStr, localName, -1); h = Tcl_FindHashEntry (&(xs->stripInfo.FQNames), Tcl_DStringValue (&dStr)); if (h) { strip = 1; - f = Tcl_GetHashValue (h); + f = (double *)Tcl_GetHashValue (h); if (*f >= stripPrecedence) { stripPrecedence = *f; stripPrio = 0.0; } } } else { h = Tcl_FindHashEntry (&(xs->stripInfo.NCNames), node->nodeName); if (h) { strip = 1; - f = Tcl_GetHashValue (h); + f = (double *)Tcl_GetHashValue (h); if (*f >= stripPrecedence) { stripPrecedence = *f; stripPrio = 0.0; } } @@ -1395,11 +1406,11 @@ } else { if (nsname) { h = Tcl_FindHashEntry (&(xs->preserveInfo.FQNames), Tcl_DStringValue (&dStr)); if (h) { - f = Tcl_GetHashValue (h); + f = (double *)Tcl_GetHashValue (h); if (*f > stripPrecedence) { strip = 0; } else if (*f == stripPrecedence && stripPrio < 0.0) { strip = 0; @@ -1407,11 +1418,11 @@ } if (strip) { h = Tcl_FindHashEntry (&(xs->preserveInfo.NSWildcards), nsname); if (h) { - f = Tcl_GetHashValue (h); + f = (double *)Tcl_GetHashValue (h); if (*f > stripPrecedence) { strip = 0; } else if (*f == stripPrecedence && stripPrio < -0.25) { strip = 0; @@ -1420,11 +1431,11 @@ } } else { h = Tcl_FindHashEntry (&(xs->preserveInfo.NCNames), node->nodeName); if (h) { - f = Tcl_GetHashValue (h); + f = (double *)Tcl_GetHashValue (h); if (*f > stripPrecedence) { strip = 0; } else if (*f == stripPrecedence && stripPrio < 0.0) { strip = 0; @@ -1878,10 +1889,96 @@ xpathRSFree( &inA ); xpathRSFree( &rsA ); xpathRSFree( &inB ); xpathRSFree( &rsB ); return 0; } +static int myNodeGreater ( + int typeText, + int asc, + int upperFirst, + char * strA, + char * strB, + double realA, + double realB, + int * greater +) +{ + int rc; +#if TclOnly8Bits == 0 + char *strAptr, *strBptr; + int lenA, lenB, len; + Tcl_UniChar unicharA, unicharB; +#endif + + *greater = 0; + + if (typeText) { + +#if TclOnly8Bits + /* TODO: this only works for 7 bit ASCII */ + rc = STRCASECMP(strA, strB); + if (rc == 0) { + rc = strcmp (strA, strB); + if (!upperFirst) { + rc *= -1; + } + } +DBG( fprintf(stderr, "nodeGreater %d <-- strA='%s' strB='%s'\n", rc, strA, strB);) +#else + lenA = Tcl_NumUtfChars (strA, -1); + lenB = Tcl_NumUtfChars (strB, -1); + len = (lenA < lenB ? lenA : lenB); + rc = Tcl_UtfNcasecmp (strA, strB, len); + if (rc == 0) { + if (lenA > lenB) { + rc = 1; + } else if (lenA < lenB) { + rc = -1; + } + } + if (rc == 0) { + strAptr = strA; + strBptr = strB; + while (len-- > 0) { + strAptr += Tcl_UtfToUniChar(strAptr, &unicharA); + strBptr += Tcl_UtfToUniChar(strBptr, &unicharB); + if (unicharA != unicharB) { + rc = unicharA - unicharB; + break; + } + } + if (!upperFirst) { + rc *= -1; + } + } +#endif + if (asc) *greater = (rc > 0); + else *greater = (rc < 0); + + } else { +DBG( fprintf(stderr, "nodeGreater realA='%f' realB='%f'\n",realA, realB);) + if (isnan (realA) || isnan (realB)) { + if (asc) { + if (isnan (realA) && !isnan (realB)) { + *greater = 0; + } else { + if (isnan (realB) && !isnan (realA)) *greater = 1; + } + } else { + if (isnan (realA) && !isnan(realB)) { + *greater = 1; + } else { + if (isnan (realB) && !isnan(realA)) *greater = 0; + } + } + } else { + if (asc) *greater = (realA > realB); + else *greater = (realA < realB); + } + } + return 0; +} /*---------------------------------------------------------------------------- | sortNodeSetFastMerge - use FastMergeSort of fast sorting | \---------------------------------------------------------------------------*/ @@ -1998,10 +2095,151 @@ free(posb); free(b); return 0; } + +static int myFastMergeSort ( + int txt, + int asc, + int upperFirst, + domNode * a[], + int * posa, + domNode * b[], + int * posb, + char ** vs, + double * vd, + char ** vstmp, + double * vdtmp, + int size, + char ** errMsg +) { + domNode *tmp; + int tmpPos, lptr, rptr, middle, i, j, gt, rc; + char *tmpVs; + double tmpVd; + + if (size < 10) { + /* use simple and fast insertion for small sizes ! */ + for (i = 1; i < size; i++) { + tmp = a [i]; + tmpPos = posa [i]; + tmpVs = vs [i]; + tmpVd = vd [i]; + j = i; + if (j>0) { + rc = myNodeGreater(txt, asc, upperFirst, vs[j-1], tmpVs, + vd[j-1], tmpVd, >); + CHECK_RC; + } + while ( j > 0 && gt) { + a [j] = a [j-1]; + posa[j] = posa[j-1]; + vs [j] = vs [j-1]; + vd [j] = vd [j-1]; + j--; + if (j>0) { + rc = myNodeGreater(txt, asc, upperFirst, vs[j-1], tmpVs, + vd[j-1], tmpVd, >); + CHECK_RC; + } + } + a [j] = tmp; + posa[j] = tmpPos; + vs [j] = tmpVs; + vd [j] = tmpVd; + } + return 0; + } + middle = size/2; + + rc = myFastMergeSort(txt, asc, upperFirst, a, posa, b, posb, vs, vd, + vstmp, vdtmp, middle, errMsg); + CHECK_RC; + rc = myFastMergeSort(txt, asc, upperFirst, a+middle, posa+middle, b+middle, + posb+middle, vs+middle, vd+middle, vstmp+middle, + vdtmp+middle, size-middle, errMsg); + CHECK_RC; + + lptr = 0; + rptr = middle; + + for (i = 0; i < size; i++) { + if (lptr == middle) { + b [i] = a [rptr ]; + posb [i] = posa[rptr ]; + vstmp[i] = vs [rptr ]; + vdtmp[i] = vd [rptr++]; + } else if (rptr < size) { + rc = myNodeGreater(txt, asc, upperFirst, vs[lptr], vs[rptr], + vd[lptr], vd[rptr], >); + if (gt) { + b [i] = a [rptr ]; + posb [i] = posa[rptr ]; + vstmp[i] = vs [rptr ]; + vdtmp[i] = vd [rptr++]; + } else { + b [i] = a [lptr ]; + posb [i] = posa[lptr ]; + vstmp[i] = vs [lptr ]; + vdtmp[i] = vd [lptr++]; + } + } else { + b [i] = a [lptr ]; + posb [i] = posa[lptr ]; + vstmp[i] = vs [lptr ]; + vdtmp[i] = vd [lptr++]; + } + } + memcpy(a, b, size*sizeof(domNode*)); + memcpy(posa, posb, size*sizeof(int*)); + memcpy(vs, vstmp, size*sizeof(char*)); + memcpy(vd, vdtmp, size*sizeof(double)); + return 0; +} + +static int mysortNodeSetFastMerge( + xsltState * xs, + int txt, + int asc, + int upperFirst, + domNode * nodes[], + int n, + char ** vs, + double * vd, + int * pos, + char ** errMsg +) +{ + domNode **b; + int *posb; + char **vstmp; + double *vdtmp; + int rc; + + b = (domNode **) malloc( n * sizeof(domNode *) ); + if (b == NULL) { + perror("malloc in sortNodeSetMergeSort"); + exit(1); + } + posb = (int *) malloc( n * sizeof(int) ); + if (posb == NULL) { + perror("malloc in sortNodeSetMergeSort"); + exit(1); + } + vstmp = (char **) malloc (sizeof (char *) * n); + vdtmp = (double *)malloc (sizeof (double) * n); + + rc = myFastMergeSort(txt, asc, upperFirst, nodes, pos, b, posb, vs, vd, + vstmp, vdtmp, n, errMsg); + free (posb); + free (b); + free (vstmp); + free (vdtmp); + CHECK_RC; + return 0; +} /*---------------------------------------------------------------------------- | xsltSetVar | \---------------------------------------------------------------------------*/ @@ -2144,14 +2382,13 @@ Tcl_HashEntry *h; xsltTopLevelVar *topLevelVar; xsltVarInProcess *varInProcess, thisVarInProcess; xpathResultSet nodeList; domNode *savedCurrentXSLTNode; - -#if 0 + Tcl_DString dErrMsg; + TRACE1("xsltGetVar variableName='%s' \n", variableName); -#endif frame = xs->varFrames; d = 0; while (frame) { var = frame->vars; @@ -2223,15 +2460,17 @@ xs->varsInProcess = varInProcess->next; xs->currentXSLTNode = savedCurrentXSLTNode; return XPATH_OK; } } - *errMsg = strdup ("Undefined Variable requested."); + Tcl_DStringInit (&dErrMsg); + Tcl_DStringAppend (&dErrMsg, "Variable \"", -1); + Tcl_DStringAppend (&dErrMsg, variableName, -1); + Tcl_DStringAppend (&dErrMsg, "\" has not been declared.", -1); + reportError (xs->currentXSLTNode, Tcl_DStringValue (&dErrMsg), errMsg); + Tcl_DStringFree (&dErrMsg); return XPATH_EVAL_ERR; - -/* rsSetString(result, ""); */ -/* return XPATH_OK; */ } /*---------------------------------------------------------------------------- @@ -2466,10 +2705,13 @@ } else { rc = xsltSetVar(xs, 1, str, context, currentNode, currentPos, NULL, child->firstChild, 0, errMsg); } CHECK_RC; + } else { + reportError (child, "xsl:with-param: missing mandatory attribute \"name\".", errMsg); + return -1; } } } child = child->nextSibling; } @@ -2486,29 +2728,34 @@ xpathResultSet * nodelist, domNode * actionNode, xpathResultSet * context, domNode * currentNode, int currentPos, - int * sorted, char ** errMsg ) { - domNode *child; - char *str, *evStr, *select, *lang; - int rc, typeText, ascending, upperFirst = 1, *pos, i; - ast t; + domNode *child; + char *str, *evStr, *select, *lang; + char **vs = NULL; + double *vd = NULL; + int rc = 0, typeText, ascending, upperFirst, *pos = NULL, i, NaN; + xpathResultSet rs; + ast t; - *sorted = 0; - child = actionNode->lastChild; /* do it backwards, so that multiple sort levels are correctly processed */ while (child) { if (child->nodeType == ELEMENT_NODE) { TRACE1("doSortActions child '%s' \n", child->nodeName); if (getTag(child) == sort) { + if (child->firstChild) { + reportError (child, "xsl:sort has to be empty.", errMsg); + return -1; + } typeText = 1; ascending = 1; + upperFirst = 1; select = getAttr(child, "select", a_select); if (!select) select = "."; xs->currentXSLTNode = child; str = getAttr(child, "data-type", a_dataType); if (str) { @@ -2539,26 +2786,59 @@ TRACE4("sorting with '%s' typeText %d ascending %d nodeSetLen=%d\n", select, typeText, ascending, nodelist->nr_nodes); rc = xpathParse(select, errMsg, &t, 0); CHECK_RC; - pos = (int*) malloc( sizeof(int) * nodelist->nr_nodes); + if (!pos) + pos = (int*) malloc( sizeof(int) * nodelist->nr_nodes); for (i=0; inr_nodes;i++) pos[i] = i; xs->currentXSLTNode = child; - rc = sortNodeSetFastMerge(xs, t, select, typeText, ascending, - upperFirst, nodelist->nodes, - nodelist->nr_nodes, pos, errMsg); - free(pos); - CHECK_RC; - xpathFreeAst( t ); - *sorted = 1; + + if (!vs) { + vs = (char **) malloc (sizeof (char *) * nodelist->nr_nodes); + for (i=0; inr_nodes;i++) vs[i] = NULL; + vd = (double *)malloc (sizeof (double) * nodelist->nr_nodes); + } + for (i = 0; i < nodelist->nr_nodes; i++) { + xpathRSInit (&rs); + rc = evalXPath (xs, nodelist, nodelist->nodes[i], i, + select, &rs, errMsg); + if (rc < 0) + goto doSortActionCleanUp; + + if (typeText) { + vs[i] = xpathFuncString (&rs); + } else { + vd[i] = xpathFuncNumber (&rs, &NaN); + } + xpathRSFree (&rs); + } + rc = mysortNodeSetFastMerge (xs, typeText, ascending, + upperFirst, nodelist->nodes, + nodelist->nr_nodes, vs, vd, + pos, errMsg); +/* rc = sortNodeSetFastMerge(xs, t, select, typeText, ascending, */ +/* upperFirst, nodelist->nodes, */ +/* nodelist->nr_nodes, pos, errMsg); */ +/* free(pos); */ + if (typeText) { + for (i = 0; i < nodelist->nr_nodes; i++) { + free (vs[i]); + } + } + if (rc < 0) + goto doSortActionCleanUp; } } child = child->previousSibling; } - return 0; + doSortActionCleanUp: + if (pos) free (pos); + if (vs) free (vs); + if (vd) free (vd); + return rc; } /*---------------------------------------------------------------------------- | xsltNumber @@ -2832,11 +3112,11 @@ domProcessingInstructionNode *pi; xpathResultSet rs, nodeList; char *str, *str2, *mode, *select, *pc; char *nsAT, *ns; char *uri, *localName, prefix[MAX_PREFIX_LEN]; - int rc, b, i, len, sorted, disableEsc = 0; + int rc, b, i, len, disableEsc = 0; double currentPrio, currentPrec; if (actionNode->nodeType == TEXT_NODE) { domAppendNewTextNode(xs->lastNode, ((domTextNode*)actionNode)->nodeValue, @@ -2975,11 +3255,11 @@ TRACE1("applyTemplates: evalXPath for select = '%s' gave back:\n", select); DBG(rsPrint(&rs)); } rc = doSortActions (xs, &rs, actionNode, context, currentNode, - currentPos, &sorted, errMsg); + currentPos, errMsg); CHECK_RC; /* should not be necessary, because every node set is returned already in doc Order */ /* if (!sorted) sortByDocOrder(&rs); */ @@ -2999,54 +3279,57 @@ Ignore the attribute. */ break; } nsAT = getAttr(actionNode, "namespace", a_namespace); str = getAttr(actionNode, "name", a_name); - if (str) { - rc = evalAttrTemplates( xs, context, currentNode, currentPos, - str, &str2, errMsg); - CHECK_RC; - ns = NULL; - Tcl_DStringInit (&dStr); - if (nsAT) { - rc = evalAttrTemplates( xs, context, currentNode, currentPos, - nsAT, &ns, errMsg); - CHECK_RC; - NS = domLookupURI (actionNode, ns); - if (NS) { - Tcl_DStringAppend (&dStr, NS->prefix, -1); - Tcl_DStringAppend (&dStr, ":", 1); - Tcl_DStringAppend (&dStr, str2, -1); - } else goto ignoreAttribute; - } else { - domSplitQName (str2, prefix, &localName); - if (prefix[0] != '\0') { - if (strcmp (prefix, "xmlns")==0) goto ignoreAttribute; - NS = domLookupPrefix (actionNode, prefix); - if (NS) ns = NS->uri; - else goto ignoreAttribute; - } else { - if (strcmp (str2, "xmlns")==0) goto ignoreAttribute; - } - Tcl_DStringAppend (&dStr, str2, -1); - } - - savedLastNode = xs->lastNode; - xs->lastNode = domNewElementNode (xs->resultDoc, - "container", ELEMENT_NODE); - rc = ExecActions(xs, context, currentNode, currentPos, - actionNode->firstChild, errMsg); - CHECK_RC; - pc = xpathGetTextValue (xs->lastNode, &len); - domSetAttributeNS (savedLastNode, Tcl_DStringValue (&dStr), pc, ns); - free(pc); - Tcl_DStringFree (&dStr); - domDeleteNode (xs->lastNode, NULL, NULL); - xs->lastNode = savedLastNode; - ignoreAttribute: - free(str2); - } + if (!str) { + reportError (actionNode, "xsl:attribute: missing mandatory attribute \"name\".", errMsg); + return -1; + } + + rc = evalAttrTemplates( xs, context, currentNode, currentPos, + str, &str2, errMsg); + CHECK_RC; + ns = NULL; + Tcl_DStringInit (&dStr); + if (nsAT) { + rc = evalAttrTemplates( xs, context, currentNode, currentPos, + nsAT, &ns, errMsg); + CHECK_RC; + NS = domLookupURI (actionNode, ns); + if (NS) { + Tcl_DStringAppend (&dStr, NS->prefix, -1); + Tcl_DStringAppend (&dStr, ":", 1); + Tcl_DStringAppend (&dStr, str2, -1); + } else goto ignoreAttribute; + } else { + domSplitQName (str2, prefix, &localName); + if (prefix[0] != '\0') { + if (strcmp (prefix, "xmlns")==0) goto ignoreAttribute; + NS = domLookupPrefix (actionNode, prefix); + if (NS) ns = NS->uri; + else goto ignoreAttribute; + } else { + if (strcmp (str2, "xmlns")==0) goto ignoreAttribute; + } + Tcl_DStringAppend (&dStr, str2, -1); + } + + savedLastNode = xs->lastNode; + xs->lastNode = domNewElementNode (xs->resultDoc, + "container", ELEMENT_NODE); + rc = ExecActions(xs, context, currentNode, currentPos, + actionNode->firstChild, errMsg); + CHECK_RC; + pc = xpathGetTextValue (xs->lastNode, &len); + domSetAttributeNS (savedLastNode, Tcl_DStringValue (&dStr), pc, ns); + free(pc); + Tcl_DStringFree (&dStr); + domDeleteNode (xs->lastNode, NULL, NULL); + xs->lastNode = savedLastNode; + ignoreAttribute: + free(str2); break; case attributeSet: return 0; case callTemplate: @@ -3111,10 +3394,13 @@ currentNode, currentPos, child->firstChild, errMsg); CHECK_RC; return 0; } + } else { + reportError (child, "xsl:when: missing mandatory attribute \"test\".", errMsg); + return -1; } break; case otherwise: /* process the children as well */ @@ -3241,44 +3527,52 @@ CHECK_RC; } break; case copyOf: + if (actionNode->firstChild) { + reportError (actionNode, "xsl:copy-of has to be empty.", errMsg); + return -1; + } select = getAttr(actionNode, "select", a_select); - if (select != NULL) { - xs->current = currentNode; - rc = evalXPath(xs, context, currentNode, currentPos, select, - &rs, errMsg); - CHECK_RC; - TRACE1(" copyOf select='%s':\n", select); - DBG(rsPrint(&rs)); - if (rs.type == NodeSetResult) { - for (i=0; inodeType == DOCUMENT_NODE) { + if (!select) { + reportError (actionNode, "xsl:copy-of: missing mandatory attribute \"select\".", errMsg); + return -1; + } + + xs->current = currentNode; + rc = evalXPath(xs, context, currentNode, currentPos, select, + &rs, errMsg); + CHECK_RC; + TRACE1(" copyOf select='%s':\n", select); + DBG(rsPrint(&rs)); + if (rs.type == NodeSetResult) { + for (i=0; inodeType == DOCUMENT_NODE) { + child = rs.nodes[i]->firstChild; + while (child) { + n = domCloneNode(child, 1); + domAppendChild(xs->lastNode, n); + child = child->nextSibling; + } + } else { + if (*(rs.nodes[i]->nodeName) == '(' && + ((strcmp(rs.nodes[i]->nodeName,"(fragment)")==0) + || (strcmp(rs.nodes[i]->nodeName,"(rootNode)")==0))) { child = rs.nodes[i]->firstChild; while (child) { n = domCloneNode(child, 1); domAppendChild(xs->lastNode, n); child = child->nextSibling; } } else { - if (*(rs.nodes[i]->nodeName) == '(' && - ((strcmp(rs.nodes[i]->nodeName,"(fragment)")==0) - || (strcmp(rs.nodes[i]->nodeName,"(rootNode)")==0))) { - child = rs.nodes[i]->firstChild; - while (child) { - n = domCloneNode(child, 1); - domAppendChild(xs->lastNode, n); - child = child->nextSibling; - } - } else { - n = domCloneNode(rs.nodes[i], 1); - domAppendChild(xs->lastNode, n); - } - } - } - } else + n = domCloneNode(rs.nodes[i], 1); + domAppendChild(xs->lastNode, n); + } + } + } + } else if ((rs.type == AttrNodeSetResult) || (rs.type == AttrValueSetResult)) { for (i=0; ilastNode, @@ -3290,48 +3584,50 @@ TRACE1("copyOf: xpathString='%s' \n", str); domAppendNewTextNode(xs->lastNode, str, strlen(str), TEXT_NODE, 0); free(str); } - xpathRSFree( &rs ); - } + xpathRSFree( &rs ); break; case decimalFormat: return 0; case element: nsAT = getAttr(actionNode, "namespace", a_namespace); str = getAttr(actionNode, "name", a_name); - if (str) { - rc = evalAttrTemplates( xs, context, currentNode, currentPos, - str, &str2, errMsg); - CHECK_RC; - - ns = NULL; - if (nsAT) { - rc = evalAttrTemplates( xs, context, currentNode, currentPos, - nsAT, &ns, errMsg); - CHECK_RC; - } - savedLastNode = xs->lastNode; - xs->lastNode = domAppendNewElementNode (xs->lastNode, str2, ns); - free(str2); - str = getAttr(actionNode, "use-attribute-sets", a_useAttributeSets); - if (str) { - TRACE1("use-attribute-sets = '%s' \n", str); - rc = ExecUseAttributeSets (xs, context, currentNode, currentPos, - str, errMsg); - CHECK_RC; - } - /* process the children as well */ - if (actionNode->firstChild) { - rc = ExecActions(xs, context, currentNode, currentPos, - actionNode->firstChild, errMsg); - } - xs->lastNode = savedLastNode; - CHECK_RC; - } + if (!str) { + reportError (actionNode, "xsl:element: missing mandatory attribute \"name\".", errMsg); + return -1; + } + + rc = evalAttrTemplates( xs, context, currentNode, currentPos, + str, &str2, errMsg); + CHECK_RC; + + ns = NULL; + if (nsAT) { + rc = evalAttrTemplates( xs, context, currentNode, currentPos, + nsAT, &ns, errMsg); + CHECK_RC; + } + savedLastNode = xs->lastNode; + xs->lastNode = domAppendNewElementNode (xs->lastNode, str2, ns); + free(str2); + str = getAttr(actionNode, "use-attribute-sets", a_useAttributeSets); + if (str) { + TRACE1("use-attribute-sets = '%s' \n", str); + rc = ExecUseAttributeSets (xs, context, currentNode, currentPos, + str, errMsg); + CHECK_RC; + } + /* process the children as well */ + if (actionNode->firstChild) { + rc = ExecActions(xs, context, currentNode, currentPos, + actionNode->firstChild, errMsg); + } + xs->lastNode = savedLastNode; + CHECK_RC; break; case fallback: return 0; case forEach: @@ -3363,11 +3659,11 @@ || (rs.type == AttrNodeSetResult) || (rs.type == AttrValueSetResult) || (rs.type == MixedSetResult)) { rc = doSortActions (xs, &rs, actionNode, context, currentNode, - currentPos, &sorted, errMsg); + currentPos, errMsg); CHECK_RC; /* should not be necessary, because every node set is returned already in doc Order */ /* if (!sorted) sortByDocOrder(&rs); */ @@ -3405,10 +3701,13 @@ /* process the children as well */ rc = ExecActions(xs, context, currentNode, currentPos, actionNode->firstChild, errMsg); CHECK_RC; } + } else { + reportError (actionNode, "xsl:if: missing mandatory attribute \"test\".", errMsg); + return -1; } break; case import: case include: @@ -3436,10 +3735,14 @@ return 0; case namespaceAlias: return 0; case number: + if (actionNode->firstChild) { + reportError (actionNode, "xsl:number has to be empty.", errMsg); + return -1; + } rc = xsltNumber(xs, context, currentNode, currentPos, actionNode, errMsg); CHECK_RC; break; @@ -3468,11 +3771,14 @@ currentPos, "", NULL, 0, errMsg); } } CHECK_RC; } - } + } else { + reportError (actionNode, "xsl:param: missing mandatory attribute \"name\".", errMsg); + return -1; + } break; case preserveSpace: return 0; case procinstr: @@ -3483,10 +3789,13 @@ pc = xpathGetTextValue (actionNode, &len); n = (domNode*)domNewProcessingInstructionNode( xs->resultDoc, str2, strlen(str), pc, len); domAppendChild(xs->lastNode, n); free(str2); + } else { + reportError (actionNode, "xsl:processing-instruction: missing mandatory attribute \"name\".", errMsg); + return -1; } break; case sort: case stylesheet: @@ -3505,10 +3814,14 @@ break; case transform: return 0; case valueOf: + if (actionNode->firstChild) { + reportError (actionNode, "xsl:value-of has to be empty.", errMsg); + return -1; + } str = getAttr(actionNode, "disable-output-escaping", a_disableOutputEscaping); if (str) { if (strcmp (str, "yes")==0) disableEsc = 1; } str = getAttr(actionNode, "select", a_select); @@ -3851,11 +4164,11 @@ if (ns) { if (strcmp (localName, "*")==0) { h = Tcl_CreateHashEntry (&(wsinfo->NSWildcards), ns->uri, &hnew); if (!hnew) { - f = Tcl_GetHashValue (h); + f = (double *)Tcl_GetHashValue (h); if (*f < precedence) { *f = precedence; } } else { f = (double *) Tcl_Alloc (sizeof (double)); *f = precedence; Tcl_SetHashValue (h, f); @@ -3865,11 +4178,11 @@ Tcl_DStringAppend (&dStr, ns->uri, -1); Tcl_DStringAppend (&dStr, localName, -1); h = Tcl_CreateHashEntry (&(wsinfo->FQNames), Tcl_DStringValue (&dStr), &hnew); if (!hnew) { - f = Tcl_GetHashValue (h); + f = (double *)Tcl_GetHashValue (h); if (*f < precedence) { *f = precedence; } } else { f = (double *) Tcl_Alloc (sizeof (double)); *f = precedence; Tcl_SetHashValue (h, f); @@ -3880,11 +4193,11 @@ /* ??? error? */ } } else { h = Tcl_CreateHashEntry (&(wsinfo->NCNames), start, &hnew); if (!hnew) { - f = Tcl_GetHashValue (h); + f = (double *)Tcl_GetHashValue (h); if (*f < precedence) { *f = precedence; } } else { f = (double *) Tcl_Alloc (sizeof (double)); *f = precedence; Tcl_SetHashValue (h, f); @@ -4164,23 +4477,26 @@ attrSet = (xsltAttrSet*)malloc(sizeof(xsltAttrSet)); attrSet->next = xs->attrSets; attrSet->content = node; attrSet->name = str; xs->attrSets = attrSet; + } else { + reportError (node, "xsl:attribute-set: missing mandatory attribute \"name\".", errMsg); + return -1; } break; case param: str = getAttr(node, "name", a_name); if (!str) { - reportError (node, "xsl:param must have a \"name\" attribute.", + reportError (node, "xsl:param: missing mandatory attribute \"name\".", errMsg); return -1; } h = Tcl_CreateHashEntry (&(xs->topLevelVars), str, &hnew); if (!hnew) { - topLevelVar = Tcl_GetHashValue (h); + topLevelVar = (xsltTopLevelVar *)Tcl_GetHashValue (h); /* Since imported stylesheets are processed at the point at which they encounters the definitions are already in increasing order of import precedence. Therefor we have only to check, if there is a top level var or parm with the same precedence */ @@ -4197,10 +4513,14 @@ topLevelVar->precedence = precedence; break; case decimalFormat: + if (node->firstChild) { + reportError (node, "xsl:decimal-format has to be empty.", errMsg); + return -1; + } str = getAttr(node, "name", a_name); if (str) { /* a named decimal format */ df = xs->decimalFormats->next; while (df) { @@ -4234,23 +4554,23 @@ str = getAttr(node, "pattern-separator", a_patternSeparator); break; case import: if (node->firstChild) { - reportError (node, "xsl:include has to empty!", errMsg); + reportError (node, "xsl:import has to empty!", errMsg); return -1; } if (!node->ownerDocument->extResolver) { reportError (node, "need resolver Script to include Stylesheet! (use \"-externalentitycommand\")", errMsg); - return -11; + return -1; } baseURI = findBaseURI (node); href = getAttr (node, "href", a_href); if (!href) { - reportError (node, "xsl:include must have a \"href\" attribute!", + reportError (node, "xsl:import: missing mandatory attribute \"href\".", errMsg); - return -11; + return -1; } extStyleSheet = getExternalDocument (interp, xs, node->ownerDocument, baseURI, href, errMsg); if (!extStyleSheet) { @@ -4267,21 +4587,21 @@ } break; case include: if (node->firstChild) { - reportError (node, "xsl:include has to be empty!", errMsg); + reportError (node, "xsl:include has to be empty.", errMsg); return -1; } if (!node->ownerDocument->extResolver) { - reportError (node, "need resolver Script to include Stylesheet! (use \"-externalentitycommand\")", errMsg); + reportError (node, "need resolver Script to include Stylesheet. (use \"-externalentitycommand\")", errMsg); return -1; } baseURI = findBaseURI (node); href = getAttr (node, "href", a_href); if (!href) { - reportError (node, "xsl:include must have a \"href\" attribute!", + reportError (node, "xsl:include: missing mandatory attribute \"href\".", errMsg); return -1; } extStyleSheet = getExternalDocument (interp, xs, node->ownerDocument, @@ -4297,23 +4617,27 @@ return rc; } break; case key: + if (node->firstChild) { + reportError (node, "xsl:key has to be empty.", errMsg); + return -1; + } name = getAttr(node, "name", a_name); if (!name) { - reportError (node, "xsl:key requires name attribute!", errMsg); + reportError (node, "xsl:key: missing mandatory attribute \"name\".", errMsg); return -1; } match = getAttr(node, "match", a_match); if (!match) { - reportError (node, "xsl:key requires match attribute!", errMsg); + reportError (node, "xsl:key: missing mandatory attribute \"match\".", errMsg); return -1; } use = getAttr(node, "use", a_use); if (!use) { - reportError (node, "xsl:key requires use attribute!", errMsg); + reportError (node, "xsl:key: missing mandatory attribute \"use\".", errMsg); return -1; } keyInfo = (xsltKeyInfo *) ckalloc(sizeof(xsltKeyInfo)); keyInfo->node = node; @@ -4324,19 +4648,29 @@ CHECK_RC1(keyInfo); h = Tcl_CreateHashEntry (&(xs->keyInfos), name, &hnew); if (hnew) { keyInfo->next = NULL; } else { - keyInfo->next = Tcl_GetHashValue (h); + keyInfo->next = (xsltKeyInfo *)Tcl_GetHashValue (h); } Tcl_SetHashValue (h, keyInfo); break; case namespaceAlias: + if (node->firstChild) { + reportError (node, "xsl:namespace-alias has to be empty.", + errMsg); + return -1; + } + /* mandatory attributes: stylesheet-prefix result-prefix */ break; case output: + if (node->firstChild) { + reportError (node, "xsl:output has to be empty.", errMsg); + return -1; + } str = getAttr(node, "method", a_method); if (str) { xs->outputMethod = strdup(str); } str = getAttr(node, "encoding", a_encoding); if (str) { xs->outputEncoding = strdup(str); } str = getAttr(node, "media-type", a_mediaType); @@ -4344,20 +4678,34 @@ str = getAttr(node, "doctype-public", a_doctypePublic); str = getAttr(node, "doctype-system", a_doctypeSystem); break; case preserveSpace: + if (node->firstChild) { + reportError (node, "xsl:preserve-space has to be empty.", errMsg); + return -1; + } str = getAttr(node, "elements", a_elements); if (str) { fillElementList(&(xs->preserveInfo), precedence, node, str); + } else { + reportError (node, "xsl:preserve-space: missing required attribute \"elements\".", errMsg); + return -1; } break; case stripSpace: + if (node->firstChild) { + reportError (node, "xsl:strip-space has to be empty.", errMsg); + return -1; + } str = getAttr(node, "elements", a_elements); if (str) { fillElementList(&(xs->stripInfo), precedence, node, str); + } else { + reportError (node, "xsl:strip-space: missing required attribute \"elements\".", errMsg); + return -1; } break; case template: rc = xsltAddTemplate (xs, node, precedence, errMsg); @@ -4371,11 +4719,11 @@ errMsg); return -1; } h = Tcl_CreateHashEntry (&(xs->topLevelVars), str, &hnew); if (!hnew) { - topLevelVar = Tcl_GetHashValue (h); + topLevelVar = (xsltTopLevelVar *)Tcl_GetHashValue (h); /* Since imported stylesheets are processed at the point at which they encounters the definitions are already in increasing order of import precedence. Therefor we have only to check, if there is a top level var or parm with the same precedence */ @@ -4612,10 +4960,11 @@ ) { xpathResultSet nodeList; domNode *node; int rc; + char *str; double precedence, precedenceLowBound; xsltState xs; xsltSubDoc *sdoc; *errMsg = NULL; @@ -4704,11 +5053,17 @@ xs.resultDoc->documentElement = xs.resultDoc->rootNode->firstChild; xs.resultDoc->nodeFlags |= OUTPUT_DEFAULT_HTML; *resultDoc = xs.resultDoc; xsltFreeState (&xs); return 0; - } + } else { + str = getAttr (node, "version", a_version); + if (!str) { + reportError (node, "missing mandatory attribute \"version\".", errMsg); + return -1; + } + } precedence = 1.0; precedenceLowBound = 0.0; rc = processTopLevel (clientData, node, xmlNode, &xs, precedence, &precedenceLowBound,errMsg); Index: generic/tcldom.c ================================================================== --- generic/tcldom.c +++ generic/tcldom.c @@ -30,12 +30,28 @@ | June00 Zoran Vasiljevic Made thread-safe. | July00 Zoran Vasiljevic Added "domNode appendFromScript" | | | $Log$ -| Revision 1.1 2002/02/22 01:05:35 rolf -| Initial revision +| Revision 1.6 2002/03/10 01:17:21 rolf +| Added method createDocumentNS to the dom command. +| +| Revision 1.5 2002/03/07 22:12:10 rolf +| Freeze of actual state, befor feeding stuff to Jochen. +| +| Revision 1.4 2002/03/01 01:22:14 rolf +| Changed parsing. [dom parse ..] now uses Tcl_GetStringFromObj(), +| dom parse -channel now respects the encoding of the channel. +| +| Revision 1.3 2002/02/24 02:31:27 rolf +| Fixed UTF-8 char byte length determination +| +| Revision 1.2 2002/02/23 01:13:33 rolf +| Some code tweaking for a mostly warning free MS build +| +| Revision 1.1.1.1 2002/02/22 01:05:35 rolf +| tDOM0.7test with Jochens first set of patches | | | | written by Jochen Loewer | April, 1999 @@ -99,16 +115,14 @@ } #if TclOnly8Bits #define writeChars(var,chan,buf,len) (chan) ? \ ((void)Tcl_Write ((chan), (buf), (len) )) : \ (Tcl_AppendToObj ((var), (buf), (len) )); -#define TCLGETBYTES(obj,pLen) Tcl_GetStringFromObj(obj,pLen) #else #define writeChars(var,chan,buf,len) (chan) ? \ ((void)Tcl_WriteChars ((chan), (buf), (len) )) : \ (Tcl_AppendToObj ((var), (buf), (len) )); -#define TCLGETBYTES(obj,pLen) Tcl_GetByteArrayFromObj(obj,pLen) #endif /*---------------------------------------------------------------------------- | Module Globals | \---------------------------------------------------------------------------*/ @@ -257,29 +271,10 @@ Tcl_Interp *interp, char *name1, char *name2, int flags ); - -/*---------------------------------------------------------------------------- -| UtfCount -| -\---------------------------------------------------------------------------*/ -#if !TclOnly8Bits -static int -UtfCount( - int ch -) -{ - if ((ch > 0) && (ch < 0x80)) { return 1; } - if (ch <= 0x7FF) { return 2; } - if (ch <= 0xFFFF) { return 3; } - return 0; -} -#endif - - /*---------------------------------------------------------------------------- | tcldom_docDeleteNode | \---------------------------------------------------------------------------*/ static void @@ -532,11 +527,11 @@ Tcl_CreateObjCommand ( interp, objCmdName, (Tcl_ObjCmdProc *) tcldom_DocObjCmd, (ClientData) dinfo, (Tcl_CmdDeleteProc*)tcldom_docCmdDeleteProc ); } else { - /* reuse old information */ + /* reuse old informaion */ dinfo = (TcldomDocDeleteInfo*)cmd_info.objClientData; } if (setVariable) { objVar = Tcl_GetStringFromObj (var_name, NULL); dinfo->traceVarName = strdup(objVar); @@ -880,11 +875,11 @@ int xml_string_len; domDocument *doc; XML_Parser parser; - xml_string = TCLGETBYTES( obj, &xml_string_len); + xml_string = Tcl_GetStringFromObj( obj, &xml_string_len); #ifdef TDOM_NO_EXPAT Tcl_AppendResult(interp, "tDOM was compiled without Expat!", NULL); return TCL_ERROR; #else @@ -1472,11 +1467,11 @@ AP(*pc) } #else } else { if ((unsigned char)*pc > 127) { - clen = UtfCount (*pc); + clen = UTF8_CHAR_LEN(*pc); if (!clen) { fprintf (stderr, "can only handle UTF-8 chars up to 3 bytes long."); exit(1); } for (i = 0; i < clen; i++) { @@ -1857,11 +1852,11 @@ do { if (node->nodeFlags & HAS_BASEURI) { entryPtr = Tcl_FindHashEntry (node->ownerDocument->baseURIs, (char*)node->nodeNumber); - baseURI = Tcl_GetHashValue (entryPtr); + baseURI = (char *)Tcl_GetHashValue (entryPtr); break; } if (node->previousSibling) { node = node->previousSibling; } @@ -2997,11 +2992,46 @@ if (objc == 3) { newObjName = objv[2]; setVariable = 1; } - doc = domCreateDocument ( Tcl_GetStringFromObj (objv[1], NULL) ); + doc = domCreateDocument ( interp, Tcl_GetStringFromObj (objv[1], NULL), + NULL); + if (!doc) return TCL_ERROR; + return tcldom_returnDocumentObj( + interp, doc, setVariable, newObjName + ); +} + + + +/*---------------------------------------------------------------------------- +| tcldom_createDocumentNS +| +\---------------------------------------------------------------------------*/ +static +int tcldom_createDocumentNS ( + ClientData clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj * const objv[] +) +{ + int setVariable = 0; + domDocument *doc; + Tcl_Obj *newObjName = NULL; + + + CheckArgs (3,4,1,"docElemName uri ?newObjVar?"); + + if (objc == 4) { + newObjName = objv[3]; + setVariable = 1; + } + doc = domCreateDocument ( interp, Tcl_GetStringFromObj (objv[1], NULL), + Tcl_GetStringFromObj (objv[2], NULL) ); + if (!doc) return TCL_ERROR; return tcldom_returnDocumentObj( interp, doc, setVariable, newObjName ); } @@ -3155,11 +3185,11 @@ if (chan == NULL) { if (objc < 2) { SetResult (dom_usage); return TCL_ERROR; } - xml_string = TCLGETBYTES( objv[1], &xml_string_len); + xml_string = Tcl_GetStringFromObj( objv[1], &xml_string_len); if (objc == 3) { newObjName = objv[2]; setVariable = 1; } } else { @@ -3302,17 +3332,17 @@ int methodIndex, result, i, bool; Tcl_CmdInfo cmdInfo; Tcl_Obj * mobjv[MAX_REWRITE_ARGS]; static char *domMethods[] = { - "createDocument", "createNodeCmd", "parse", - "setResultEncoding", "setStoreLineColumn", + "createDocument", "createDocumentNS", "createNodeCmd", + "parse", "setResultEncoding", "setStoreLineColumn", NULL }; enum domMethod { - m_createDocument, m_createNodeCmd, m_parse, - m_setResultEncoding, m_setStoreLineColumn + m_createDocument, m_createDocumentNS, m_createNodeCmd, + m_parse, m_setResultEncoding, m_setStoreLineColumn }; if (objc < 2) { @@ -3354,10 +3384,14 @@ switch ((enum domMethod) methodIndex ) { case m_createDocument: return tcldom_createDocument (clientData, interp, --objc, objv+1 ); + case m_createDocumentNS: + return tcldom_createDocumentNS (clientData, interp, --objc, + objv+1); + case m_createNodeCmd: return nodecmd_createNodeCmd (clientData, interp, --objc, objv+1 ); case m_parse: return tcldom_parse (clientData, interp, --objc, objv+1 ); Index: generic/tcldom.h ================================================================== --- generic/tcldom.h +++ generic/tcldom.h @@ -26,12 +26,15 @@ | | Contributor(s): | | | $Log$ -| Revision 1.1 2002/02/22 01:05:34 rolf -| Initial revision +| Revision 1.2 2002/02/23 01:13:33 rolf +| Some code tweaking for a mostly warning free MS build +| +| Revision 1.1.1.1 2002/02/22 01:05:34 rolf +| tDOM0.7test with Jochens first set of patches | | | | written by Jochen Loewer | April, 1999 @@ -49,26 +52,17 @@ Tcl_ObjCmdProc tcldom_NodeObjCmd; Tcl_ObjCmdProc TclExpatObjCmd; Tcl_ObjCmdProc tcldom_unknownCmd; Tcl_ObjCmdProc TclTdomObjCmd; -#define STR_TDOM_VERSION(v) ("0.7") -#ifndef DLLEXPORT -#define DLL_BUILD +#if defined(_MSC_VER) # undef TCL_STORAGE_CLASS -# define TCL_STORAGE_CLASS -# define DLLEXPORT __declspec(dllexport) -#ifndef STATIC_BUILD -#if defined(_MSC_VER) -# define EXPORT(a,b) __declspec(dllexport) a b -# define DllEntryPoint DllMain -#else -# if defined(__BORLANDC__) -# define EXPORT(a,b) a _export b -# else -# define EXPORT(a,b) a b -# endif -#endif -#endif -#endif -#endif +# define TCL_STORAGE_CLASS DLLEXPORT +#endif + +#define STR_TDOM_VERSION(v) ("0.7") + +EXTERN int Tdom_Init _ANSI_ARGS_((Tcl_Interp *interp)); + +#endif + Index: generic/tclexpat.c ================================================================== --- generic/tclexpat.c +++ generic/tclexpat.c @@ -44,13 +44,10 @@ #ifdef _POSIX_SOURCE #include #endif -#undef TCL_STORAGE_CLASS -#define TCL_STORAGE_CLASS DLLEXPORT - /* Used internal als status, like TCL_OK, TCL_ERROR etc. As a consequent, application specific error codes must be at least greater than 5 */ #define ERROR_IN_EXTREFHANDLER 5 @@ -84,17 +81,10 @@ if (!activeTclHandlerSet) { \ activeTclHandlerSet = CreateTclHandlerSet("default");\ expat->firstTclHandlerSet = activeTclHandlerSet; \ } -#if ((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION > 0)) -#define TCLGETBYTES(obj,pLen) Tcl_GetByteArrayFromObj(obj,pLen) -#define TCLNEWBYTES(obj,len) Tcl_NewByteArrayObj(obj,len) -#else -#define TCLGETBYTES(obj,pLen) Tcl_GetStringFromObj(obj,pLen) -#define TCLNEWBYTES(obj,len) Tcl_NewStringObj(obj,len) -#endif /*---------------------------------------------------------------------------- | typedefs | \---------------------------------------------------------------------------*/ @@ -601,11 +591,11 @@ int objc; Tcl_Obj *CONST objv[]; { TclGenExpatInfo *expat = (TclGenExpatInfo *) clientData; char *data; - int len, optionIndex, result = TCL_OK; + int len = 0, optionIndex, result = TCL_OK; CHandlerSet *activeCHandlerSet; static char *options[] = { "configure", "cget", "free", "get", "parse", "parsechannel", "parsefile", "reset", NULL @@ -647,11 +637,11 @@ break; case EXPAT_PARSE: CheckArgs (3,3,2,""); - data = TCLGETBYTES(objv[2], &len); + data = Tcl_GetStringFromObj(objv[2], &len); result = TclExpatParse(interp, expat, data, len, EXPAT_INPUT_STRING); break; case EXPAT_PARSECHANNEL: @@ -726,11 +716,16 @@ char s[255], buf[8*1024]; int fd; XML_Parser *parser; Tcl_Channel channel = NULL; CHandlerSet *activeCHandlerSet; - +#if !TclOnly8Bits + Tcl_Obj *bufObj; + Tcl_DString dStr; + int useBinary; + char *str; +#endif expat->status = TCL_OK; if (expat->result != NULL) { Tcl_DecrRefCount(expat->result); } @@ -765,24 +760,63 @@ if (!(mode & TCL_READABLE)) { Tcl_ResetResult (interp); Tcl_AppendResult (interp, "channel \"", data, "wasn't opened for reading", (char *) NULL); return TCL_ERROR; } +#if !TclOnly8Bits + Tcl_DStringInit (&dStr); + if (Tcl_GetChannelOption (interp, channel, "-encoding", &dStr) != TCL_OK) { + return TCL_ERROR; + } + if (strcmp (Tcl_DStringValue (&dStr), "binary")==0 ) useBinary = 1; + else useBinary = 0; + Tcl_DStringFree (&dStr); + if (useBinary) { + do { + bytesread = Tcl_Read (channel, buf, sizeof (buf)); + done = bytesread < sizeof (buf); + if (done) { + result = XML_Parse (expat->parser, buf, bytesread, done); + } else { + if (!XML_Parse (expat->parser, buf, bytesread, done)) { + result = 0; + break; + } + } + } while (!done); + } else { + bufObj = Tcl_NewObj(); + Tcl_IncrRefCount (bufObj); + Tcl_SetObjLength (bufObj, 6144); + do { + len = Tcl_ReadChars (channel, bufObj, 1024, 0); + done = (len < 1024); + str = Tcl_GetStringFromObj (bufObj, &len); + if (!XML_Parse (expat->parser, str, len, done)) { + result = 0; + break; + } + } while (!done); + result = 1; + Tcl_DecrRefCount (bufObj); + } +#else do { bytesread = Tcl_Read (channel, buf, sizeof (buf)); done = bytesread < sizeof (buf); if (done) { result = XML_Parse (expat->parser, buf, bytesread, done); } else { if (!XML_Parse (expat->parser, buf, bytesread, done)) { result = 0; - break; + break; } } } while (!done); +#endif break; - + case EXPAT_INPUT_FILENAME: fd = open(data, O_BINARY|O_RDONLY); if (fd < 0) { Tcl_ResetResult (interp); Tcl_AppendResult (interp, "error opening file \"", @@ -1999,11 +2033,10 @@ goto nextTcl; } vector[0] = activeTclHandlerSet->datacommand; vector[1] = Tcl_NewStringObj ((char *)s, len); -/* vector[1] = TCLNEWBYTES((char *)s, len); */ Tcl_Preserve((ClientData) expat->interp); result = activeTclHandlerSet->datacommandObjProc(activeTclHandlerSet->datacommandclientData, expat->interp, 2, vector); Tcl_Release((ClientData) expat->interp); TclExpatHandlerResult(expat, activeTclHandlerSet, result); @@ -3195,11 +3228,11 @@ Tcl_Interp *interp; Tcl_Obj *rep; XML_Content *model; { Tcl_Obj *cp, *detail; - int i; + unsigned int i; switch (model->type) { case XML_CTYPE_EMPTY: Tcl_ListObjAppendElement (interp, rep, Tcl_NewStringObj ("EMPTY", 5)); Index: generic/tclexpat.h ================================================================== --- generic/tclexpat.h +++ generic/tclexpat.h @@ -127,10 +127,14 @@ /*-------------------------------------------------------------------------- | Function prototypes | \-------------------------------------------------------------------------*/ +#if defined(_MSC_VER) +# undef TCL_STORAGE_CLASS +# define TCL_STORAGE_CLASS DLLEXPORT +#endif EXTERN int TclExpatObjCmd _ANSI_ARGS_((ClientData dummy, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[])); EXTERN int CheckExpatParserObj _ANSI_ARGS_((Tcl_Interp *interp, Index: generic/tdominit.c ================================================================== --- generic/tdominit.c +++ generic/tdominit.c @@ -26,12 +26,15 @@ | | Contributor(s): | | | $Log$ -| Revision 1.1 2002/02/22 01:05:35 rolf -| Initial revision +| Revision 1.2 2002/02/23 01:13:33 rolf +| Some code tweaking for a mostly warning free MS build +| +| Revision 1.1.1.1 2002/02/22 01:05:35 rolf +| tDOM0.7test with Jochens first set of patches | | | | written by Jochen Loewer | April, 1999 @@ -46,16 +49,10 @@ \---------------------------------------------------------------------------*/ #include #include #include - -/*JCL #define STR_TDOM_VERSION(v) (#v) - -EXTERN EXPORT(int,Tdom_Init) _ANSI_ARGS_((Tcl_Interp *interp)); -*/ - /* *---------------------------------------------------------------------------- * * Tdom_Init -- @@ -97,16 +94,10 @@ #endif Tcl_PkgProvide (interp, "stackedtdom", "0.1"); Tcl_PkgProvide (interp, "tdom", STR_TDOM_VERSION(TDOM_VERSION)); - /* - fprintf(stderr, "sizeof(domNode) = %d \n", sizeof(domNode) ); - fprintf(stderr, "sizeof(domTextNode) = %d \n", sizeof(domTextNode) ); - fprintf(stderr, "sizeof(domAttrNode) = %d \n", sizeof(domAttrNode) ); - */ - return TCL_OK; } int Tdom_SafeInit (interp) Index: lib/tdom.tcl ================================================================== --- lib/tdom.tcl +++ lib/tdom.tcl @@ -27,22 +27,29 @@ # Contributor(s): # Rolf Ade (rolf@pointsman.de): 'fake' nodelists/live childNodes # # # $Log$ -# Revision 1.1 2002/02/22 01:05:35 rolf -# Initial revision +# Revision 1.3 2002/02/28 00:39:00 rolf +# Added tcl coded xpath function element-avaliable. Changed +# function-avaliable accordingly. +# +# Revision 1.2 2002/02/26 14:04:16 rolf +# Updated the [package provide] to the new version number 0.7 +# +# Revision 1.1.1.1 2002/02/22 01:05:35 rolf +# tDOM0.7test with Jochens first set of patches # # # # written by Jochen Loewer # April, 1999 # #---------------------------------------------------------------------------- -package provide tdom 0.6 +package provide tdom 0.7 #---------------------------------------------------------------------------- # setup namespaces for additional Tcl level methods, etc. # @@ -514,10 +521,11 @@ concat - contains - count - current - document - + element-avaliable - false - floor - generate-id - id - key - @@ -541,12 +549,11 @@ translate - true - unparsed-entity-uri { return [list bool true] } - format-number - - element-avaliable { + format-number { return [list bool false] } default { set TclXpathFuncs [info procs ::dom::xpathFunc::*] if {[lsearch -exact $TclXpathFuncs $str] != -1} { @@ -557,11 +564,68 @@ } } } #---------------------------------------------------------------------------- -# functions-available +# element-avaliable +# +#---------------------------------------------------------------------------- +proc ::dom::xpathFunc::element-avaliable { ctxNode pos + nodeListType nodeList args} { + + if {[llength $args] != 2} { + error "element-avaliable(); wrong # of args!" + } + foreach { arg1Typ arg1Value } $args break + set str [::dom::xpathFuncHelper::coerce2string $arg1Typ $arg1Value ] + switch $str { + stylesheet - + transform - + include - + import - + strip-space - + preserve-space - + template - + apply-templates - + apply-imports - + call-template - + element - + attribute - + attribute-set - + text - + processing-instruction - + comment - + copy - + value-of - + number - + for-each - + if - + choose - + when - + otherwise - + sort - + variable - + param - + copy-of - + with-param - + key - + message { + return [list bool true] + } + decimal-format - + output - + namespace-alias - + fallback - + default { + return [list bool false] + } + } +} + + +#---------------------------------------------------------------------------- +# system-property # #---------------------------------------------------------------------------- proc ::dom::xpathFunc::system-property { ctxNode pos nodeListType nodeList args } { @@ -579,9 +643,9 @@ } xsl:vendor-url { return [list string "http://sdf.lonestar.org/~loewerj/tdom.cgi"] } default { - return [string ""] + return [list string ""] } } } ADDED tests/all Index: tests/all ================================================================== --- /dev/null +++ tests/all @@ -0,0 +1,14 @@ +# expat.tcl -- +# +# This scripts runs the tests using the expat parser. +# +# Copyright (c) 1999-2000 by Zveno Pty Ltd +# All rights reserved. +# +# $Id$ + +set auto_path [linsert $auto_path 0 [file dirname [file dirname [file join [pwd] [info script]]]]] + +package require expat 2.0 + +source all.tcl ADDED tests/all.tcl Index: tests/all.tcl ================================================================== --- /dev/null +++ tests/all.tcl @@ -0,0 +1,76 @@ +# all.tcl -- +# +# This file contains a top-level script to run all of the Tcl +# tests. Execute it by invoking "source all.test" when running tcltest +# in this directory. +# +# Copyright (c) 1998-1999 by Scriptics Corporation. +# All rights reserved. +# +# RCS: @(#) $Id$ + +if {[lsearch [namespace children] ::tcltest] == -1} { + source [file join [pwd] [file dirname [info script]] defs.tcl] +} +set ::tcltest::testSingleFile false + +puts stdout "Tcl $tcl_patchLevel tests running in interp: [info nameofexecutable]" +puts stdout "Tests running in working dir: $::tcltest::workingDir" +if {[llength $::tcltest::skip] > 0} { + puts stdout "Skipping tests that match: $::tcltest::skip" +} +if {[llength $::tcltest::match] > 0} { + puts stdout "Only running tests that match: $::tcltest::match" +} +source ../lib/tdom.tcl +# Use command line specified glob pattern (specified by -file or -f) +# if one exists. Otherwise use *.test. If given, the file pattern +# should be specified relative to the dir containing this file. If no +# files are found to match the pattern, print an error message and exit. +set fileIndex [expr {[lsearch $argv "-file"] + 1}] +set fIndex [expr {[lsearch $argv "-f"] + 1}] +if {($fileIndex < 1) || ($fIndex > $fileIndex)} { + set fileIndex $fIndex +} +if {$fileIndex > 0} { + set globPattern [file join $::tcltest::testsDir [lindex $argv $fileIndex]] + puts stdout "Sourcing files that match: $globPattern" +} else { + set globPattern [file join $::tcltest::testsDir *.test] +} +set fileList [glob -nocomplain $globPattern] +if {[llength $fileList] < 1} { + puts "Error: no files found matching $globPattern" + exit +} +set timeCmd {clock format [clock seconds]} +puts stdout "Tests began at [eval $timeCmd]" + +# source each of the specified tests +foreach file [lsort $fileList] { + set tail [file tail $file] + if {[string match l.*.test $tail]} { + # This is an SCCS lockfile; ignore it + continue + } + puts stdout $tail + if {[catch {source $file} msg]} { + puts stdout $msg + } +} + +# cleanup +puts stdout "\nTests ended at [eval $timeCmd]" +::tcltest::cleanupTests 1 +return + + + + + + + + + + + ADDED tests/attribute.test Index: tests/attribute.test ================================================================== --- /dev/null +++ tests/attribute.test @@ -0,0 +1,313 @@ +# Features covered: Attribute Lists +# +# This file contains a collection of tests for the TclXML parser. +# This file tests the parser's performance on Attribute Lists. +# Sourcing this file into Tcl runs the tests and generates output +# for errors. No output means no errors were found. +# +# Copyright (c) 1998-2000 Zveno Pty Ltd. +# +# $Id$ + +if {[lsearch [namespace children] ::tcltest] == -1} { + source [file join [pwd] [file dirname [info script]] defs.tcl] +} + +if {[lsearch $auto_path [file dirname [file dirname [info script]]]] == -1} { + set auto_path [linsert $auto_path 0 [file dirname [file dirname [file join [pwd] [info script]]]]] +} + +if {[catch {package require expat 2.0}]} { + catch {puts stderr "Cannot load expat 2.0 package"} + return +} + +catch {unset result} +proc EStart {tagName attrList} { + global result countAttributesOnly + + if {![llength $attrList] && !$countAttributesOnly} { + if {[info exists result($tagName)]} { + set count 0 + while {[info exists result($tagName/[incr count])]} {} + set result($tagName/$count) {} + } else { + set result($tagName) {} + } + return {} + } + + foreach {name value} $attrList { + if {[info exists result($tagName,$name)]} { + set count 0 + while {[info exists result($tagName,$name,[incr count])]} {} + set result($tagName,$name,$count) $value + } else { + set result($tagName,$name) $value + } + } +} +catch {unset ::pcdata} +proc pcdata t { + append ::pcdata $t +} + +test attrList-1.1 {empty attribute list} { + catch {unset ::result} + set ::countAttributesOnly 1 + + catch {rename xml::attrList-1.1 {}} + set parser [xml::parser attrList-1.1 \ + -elementstartcommand EStart] + $parser parse { + + +} + array size result +} 0 + +test attrList-1.2 {single attribute} { + catch {unset ::result} + set ::countAttributesOnly 1 + + catch {rename xml::attrList-1.2 {}} + set parser [xml::parser attrList-1.2 \ + -elementstartcommand EStart] + $parser parse { + + +} + + array get result +} {Test,attr 1} + +test attrList-1.3 {multiple distinct attributes} { + catch {unset ::result} + set ::countAttributesOnly 1 + + catch {rename xml::attrList-1.3 {}} + set parser [xml::parser attrList-1.3 \ + -elementstartcommand EStart] + $parser parse { + + +} + + list [array size result] $result(Test,first) $result(Test,second) +} {2 1 2} + +test attrList-1.4 {hyphen in attribute name} { + catch {unset ::result} + set ::countAttributesOnly 1 + + catch {rename xml::attrList-1.4 {}} + set parser [xml::parser attrList-1.4 \ + -elementstartcommand EStart] + $parser parse { + + +} + + array get result +} {Test,first-attr 1} + +test attrList-2.1 {right angle bracket in attribute value} { + catch {unset ::result} + set ::countAttributesOnly 1 + + catch {rename xml::attrList-2.1 {}} + set parser [xml::parser attrList-2.1 \ + -elementstartcommand EStart] + $parser parse { + + +} + + array get result +} {Test,attr value>} + +test attrList-2.2 {right angle bracket in attribute value} { + catch {unset ::result} + set ::countAttributesOnly 1 + + catch {rename xml::attrList-2.2 {}} + set parser [xml::parser attrList-2.2 \ + -elementstartcommand EStart] + $parser parse { + + +} + + array get result +} {Test,attr value1>value2} + +test attrList-2.3 {right angle bracket in attribute value} { + catch {unset ::result} + set ::countAttributesOnly 1 + + catch {rename xml::attrList-2.3 {}} + set parser [xml::parser attrList-2.3 \ + -elementstartcommand EStart] + $parser parse { + + +} + + array get result +} {Test,attr1 value1 Test,attr2 value2>} + +test attrList-2.4 {right angle bracket in attribute value} { + catch {unset ::result} + set ::countAttributesOnly 1 + + catch {rename xml::attrList-2.4 {}} + set parser [xml::parser attrList-2.4 \ + -elementstartcommand EStart] + $parser parse { + + +} + + array get result +} {Test,attr1 value1 Test,attr2 value2>} + +test attrList-2.5 {right angle brackets in attribute values} { + catch {unset ::result} + set ::countAttributesOnly 1 + + catch {rename xml::attrList-2.5 {}} + set parser [xml::parser attrList-2.5 \ + -elementstartcommand EStart] + $parser parse { + + +} + + array get result +} {Test,attr1 value>1 Test,attr2 value>2} + +test attrList-2.6 {right angle brackets in attribute values} { + catch {unset ::result} + catch {unset ::pcdata} + set ::countAttributesOnly 1 + + catch {rename xml::attrList-2.6 {}} + set parser [xml::parser attrList-2.6 \ + -elementstartcommand EStart \ + -characterdatacommand pcdata] + $parser parse { + +some text +} + + list [array get result] $::pcdata +} {{Test,attr1 value>1} {some text}} + +test attrList-3.1 {unnested left brace in attribute value} { + catch {unset ::result} + set ::countAttributesOnly 1 + + catch {rename xml::attrList-3.1 {}} + set parser [xml::parser attrList-3.1 \ + -elementstartcommand EStart] + $parser parse [format { + + +} \{] + + array get result +} [list Test,attr [format {%svalue} \{]] + +test attrList-3.2 {unnested right brace in attribute value} { + catch {unset ::result} + set ::countAttributesOnly 1 + + catch {rename xml::attrList-3.2 {}} + set parser [xml::parser attrList-3.2 \ + -elementstartcommand EStart] + $parser parse [format { + + +} \}] + + array get result +} [list Test,attr [format {value%s} \}]] + +test attrList-3.3 {Tcl special characters in attribute value} { + catch {unset ::result} + set ::countAttributesOnly 1 + + catch {rename xml::attrList-3.3 {}} + set parser [xml::parser attrList-3.3 \ + -elementstartcommand EStart] + $parser parse { + + +} + + array get result +} {Test,attr {dollar $ backslash \ brackets [puts hello]}} + +test attrList-4.1 {Unquoted attribute value} { + catch {unset ::result} + set ::countAttributesOnly 1 + + catch {rename xml::attrList-4.1 {}} + set parser [xml::parser attrList-4.1 \ + -elementstartcommand EStart] + set retval [catch { + $parser parse { + + +}} msg] + + list $retval $msg +} [list 1 {error "not well-formed (invalid token)" at line 3 character 11}] + +# Test case contributed by David Sutton +test attrList-5.0 {Complicated attribute list} { + catch {unset ::result} + catch {unset ::pcdata} + set ::countAttributesOnly 0 + + catch {rename xml::attrList-5.0 {}} + set parser [xml::parser attrList-5.0 \ + -elementstartcommand EStart \ + -characterdatacommand pcdata] + set retval [catch { + $parser parse { + + + LightState = LightCtl + LOG(AlarmSwitch) + DISABLE(BlinkLight) + NOTIFY( AlarmSwitch,"Alarm has been reset") + +}} msg] + + regsub -all "\[ \t\n\]+" $::pcdata { } ::pcdata + set sortedResult {} + foreach key [lsort -dictionary [array names ::result]] { + lappend sortedResult $key $::result($key) + } + list $retval $sortedResult $::pcdata +} [list 0 {event,deleteOnCompletion no event,endDateTime {} event,ID 22 event,name LogAlarmReset event,startDateTime {} event,startDisabled no eventAction {} eventAction/1 {} eventAction/2 {} eventAction/3 {} stateChangeTrigger,condition {AlarmSwitch = FALSE} stateChangeTrigger,initialState true} { LightState = LightCtl LOG(AlarmSwitch) DISABLE(BlinkLight) NOTIFY( AlarmSwitch,"Alarm has been reset") }] + +# Test case contributed by Marshall Rose +test attrList-5.1 {Attribute list with quoted value} { + catch {unset ::result} + set ::countAttributesOnly 0 + + catch {rename xml::attrList-5.1 {}} + set parser [xml::parser attrList-5.1 \ + -elementstartcommand EStart] + set retval [catch { + $parser parse {} + }] + + list $retval [array get ::result] +} {0 {test,example {isn't this legal?}}} + +# cleanup +::tcltest::cleanupTests +return ADDED tests/cdata.test Index: tests/cdata.test ================================================================== --- /dev/null +++ tests/cdata.test @@ -0,0 +1,319 @@ +# Features covered: CDATA sections +# +# This file contains a collection of tests for the TclXML parser. +# This file tests the parser's performance on CDATA sections. +# Sourcing this file into Tcl runs the tests and generates output +# for errors. No output means no errors were found. +# +# Copyright (c) 1998-2000 Zveno Pty Ltd. +# +# $Id$ + +if {[lsearch [namespace children] ::tcltest] == -1} { + source [file join [pwd] [file dirname [info script]] defs.tcl] +} + +if {[lsearch $auto_path [file dirname [file dirname [info script]]]] == -1} { + set auto_path [linsert $auto_path 0 [file dirname [file dirname [file join [pwd] [info script]]]]] +} + +if {[catch {package require expat 2.0}]} { + catch {puts stderr "Cannot load expat 2.0 package"} + return +} + +catch {unset result} +proc pcdata data { + append ::result $data +} +proc Estart {tagName attrList} { + switch -- $tagName { + Test { + } + default { + incr ::element + } + } +} + +proc EStop tagname { +} + +test cdata-1.1 {Simple CDATA section} { + set ::result {} + set ::element 0 + + catch {rename xml::cdata-1.1 {}} + set parser [xml::parser cdata-1.1 \ + -elementstartcommand Estart \ + -elementendcommand EStop \ + -characterdatacommand pcdata] + $parser parse { + + +} + list $::result $::element +} {{This is CDATA} 0} + +test cdata-1.2 {CDATA test contributed by Richard Emberson (emberson@netintouch.com)} { + set ::result {} + set ::element 0 + + catch {rename xml::cdata-1.2 {}} + set parser [xml::parser cdata-1.2 \ + -elementstartcommand Estart \ + -elementendcommand EStop \ + -characterdatacommand pcdata] + $parser parse { + + + +} + list $::result $::element +} {{ + 928806871035 + } 1} + +# Test bug: ']]>' is not valid character data (XML Rec. subsect. 2.4) - +# this should produce a warning. +test cdata-2.0 {CDATA section interpersed with comment} { + set ::result {} + set ::element 0 + + catch {rename xml::cdata-2.0 {}} + set parser [xml::parser cdata-2.0 \ + -elementstartcommand Estart \ + -elementendcommand EStop \ + -characterdatacommand pcdata] + $parser parse { + +]]> + +-->]]>} + list $::result $::element +} {{ + +]]>} 0} + +test cdata-2.1 {CDATA section with an angle bracket} { + set ::result {} + set ::element 0 + + catch {rename xml::cdata-2.1 {}} + set parser [xml::parser cdata-2.1 \ + -elementstartcommand Estart \ + -elementendcommand EStop \ + -characterdatacommand pcdata] + $parser parse { + + greater than sign]]> +} + list $::result $::element +} {{This is a > greater than sign} 0} + +# Test case contributed by Marshall Rose (mrose@dbc.mtview.ca.us) +test cdata-2.2 {CDATA section with multiple angle brackets} { + set ::result {} + set ::element 0 + + catch {rename xml::cdata-2.2 {}} + set parser [xml::parser cdata-2.2 \ + -elementstartcommand Estart \ + -elementendcommand EStop \ + -characterdatacommand pcdata] + $parser parse { + + + + + + ... + + +]]> +} + list $::result $::element +} {{ + + + + + ... + + + +} 1} + +# Test case contributed by J. Linnenkohl (jlinnen@c-s-k.de) +test cdata-2.3 {CDATA section with square brackets and curly braces} { + set ::result {} + set ::element 0 + + catch {rename xml::cdata-2.3 {}} + set parser [xml::parser cdata-2.3 \ + -elementstartcommand Estart \ + -elementendcommand EStop \ + -characterdatacommand pcdata] + $parser parse { + + + +} + list $::result $::element +} {{ + +proc arithmetic_add {groups inputs outputs atts} { + set ret_val "" + set t "Hello World" + set l [string length $t] + return $ret_val +} + +} 1} + +test cdata-2.4 {CDATA section with angle brackets and curly braces} { + set ::result {} + set ::element 0 + + catch {rename xml::cdata-2.4 {}} + set parser [xml::parser cdata-2.4 \ + -elementstartcommand Estart \ + -elementendcommand EStop \ + -characterdatacommand pcdata] + $parser parse { + + 2] + set t "Hello World" + set l [string length $t] + return $ret_val +} +]]> + +} + list $::result $::element +} {{ + +proc arithmetic_add {groups inputs outputs atts} { + set ret_val [expr 1 > 2] + set t "Hello World" + set l [string length $t] + return $ret_val +} + +} 1} + +test cdata-2.5 {CDATA section with angle brackets, Tcl specials trailing CDATA section} { + set ::result {} + set ::element 0 + + catch {rename xml::cdata-2.5 {}} + set parser [xml::parser cdata-2.5 \ + -elementstartcommand Estart \ + -elementendcommand EStop \ + -characterdatacommand pcdata] + $parser parse { + + 2] + set t "Hello World" + set l [string length $t] + return $ret_val +} +]]> that is {jolly} $good + +} + list $::result $::element +} {{ + +proc arithmetic_add {groups inputs outputs atts} { + set ret_val [expr 1 > 2] + set t "Hello World" + set l [string length $t] + return $ret_val +} + that is {jolly} $good +} 1} + +# Test case contributed by Marshall Rose (mrose@dbc.mtview.ca.us) +test cdata-3.1 {CDATA section with PI and Tcl special characters} { + set ::result {} + set ::element 0 + + catch {rename xml::cdata-3.1 {}} + set parser [xml::parser cdata-3.1 \ + -elementstartcommand Estart \ + -elementendcommand EStop \ + -characterdatacommand pcdata] + $parser parse { + + + +this is a \ test + +]]> +} + list $::result $::element +} {{ +this is a \ test + + + +} 2} + +test cdata-4.1 {CDATA section with entity reference} { + set ::result {} + set ::element 0 + + catch {rename xml::cdata-4.1 {}} + set parser [xml::parser cdata-4.1 \ + -elementstartcommand Estart \ + -elementendcommand EStop \ + -characterdatacommand pcdata] + $parser parse { + +} + list $::result $::element +} {{no entity <references>} 0} + + +test cdata-5.1 {CDATA section whiteCDATAs} { + set ::result {} + set ::cdata {} + + catch {rename xml::cdata-5.1 {}} + set parser [xml::parser cdata-5.1 \ + -ignorewhitecdata 1 \ + -characterdatacommand pcdata] + $parser parse { +foo +} + list $::result +} {foo} + +test cdata-5.2 {CDATA section whiteCDATAs} { + set ::result {} + set ::cdata {} + + catch {rename xml::cdata-5.2 {}} + set parser [xml::parser cdata-5.2 \ + -characterdatacommand pcdata] + $parser parse { foo } + list $::result +} {{ foo }} + + +# cleanup +::tcltest::cleanupTests +return ADDED tests/comment.test Index: tests/comment.test ================================================================== --- /dev/null +++ tests/comment.test @@ -0,0 +1,218 @@ +# Features covered: comments +# +# This file contains a collection of tests for the TclXML parser. +# This file tests the parser's performance on comments. +# Sourcing this file into Tcl runs the tests and generates output +# for errors. No output means no errors were found. +# +# Copyright (c) 1998-2000 Zveno Pty Ltd. +# +# $Id$ + +if {[lsearch [namespace children] ::tcltest] == -1} { + source [file join [pwd] [file dirname [info script]] defs.tcl] +} + +if {[lsearch $auto_path [file dirname [file dirname [info script]]]] == -1} { + set auto_path [linsert $auto_path 0 [file dirname [file dirname [file join [pwd] [info script]]]]] +} + +if {[catch {package require expat 2.0}]} { + catch {puts stderr "Cannot load expat 2.0 package"} + return +} + +catch {unset result} +proc pcdata data { + append ::result $data +} +proc comment data { + append ::comment $data +} +proc Estart {tagName attrList} { + switch -- $tagName { + test - + Test { + } + default { + incr ::element + } + } +} + +proc EStop tagname { +} + +test comment-1.1 {Simple comment} { + set ::result {} + set ::comment {} + set ::element 0 + + catch {rename xml::comment-1.1 {}} + set parser [xml::parser comment-1.1 \ + -elementstartcommand Estart \ + -elementendcommand EStop \ + -characterdatacommand pcdata \ + -commentcommand comment] + $parser parse { + + +} + list $::comment $::result $::element +} {{ This is a comment } {} 0} + +test comment-1.2 {Simple comment, no white space} { + set ::result {} + set ::comment {} + set ::element 0 + + catch {rename xml::comment-1.2 {}} + set parser [xml::parser comment-1.2 \ + -elementstartcommand Estart \ + -elementendcommand EStop \ + -characterdatacommand pcdata \ + -commentcommand comment] + $parser parse { + + +} + list $::comment $::result $::element +} {{This is a comment} {} 0} + +test comment-1.3 {Simple comment, within PCDATA} { + set ::result {} + set ::comment {} + set ::element 0 + + catch {rename xml::comment-1.3 {}} + set parser [xml::parser comment-1.3 \ + -elementstartcommand Estart \ + -elementendcommand EStop \ + -characterdatacommand pcdata \ + -commentcommand comment] + $parser parse { + +surrounding PCDATA +} + list $::comment $::result $::element +} {{This is a comment} {surrounding PCDATA} 0} + +test comment-1.3 {Simple comment, no white space} { + set ::result {} + set ::comment {} + set ::element 0 + + catch {rename xml::comment-1.3 {}} + set parser [xml::parser comment-1.3 \ + -elementstartcommand Estart \ + -elementendcommand EStop \ + -characterdatacommand pcdata \ + -commentcommand comment] + $parser parse { + + +} + list $::comment $::result $::element +} {comment {} 0} + +test comment-1.4 {comment, with nested element} { + set ::result {} + set ::comment {} + set ::element 0 + + catch {rename xml::comment-1.4 {}} + set parser [xml::parser comment-1.4 \ + -elementstartcommand Estart \ + -elementendcommand EStop \ + -characterdatacommand pcdata \ + -commentcommand comment] + $parser parse { + + +} + list $::comment $::result $::element +} {{ comment } {} 0} + +test comment-2.1 {comment with an angle bracket} { + set ::result {} + set ::comment {} + set ::element 0 + + catch {rename xml::comment-2.1 {}} + set parser [xml::parser comment-2.1 \ + -elementstartcommand Estart \ + -elementendcommand EStop \ + -characterdatacommand pcdata \ + -commentcommand comment] + $parser parse { + + +} + list $::comment $::result $::element +} {{ This is a > greater than sign } {} 0} + +test comment-2.2 {comment with multiple angle brackets} { + set ::result {} + set ::comment {} + set ::element 0 + + catch {rename xml::comment-2.2 {}} + set parser [xml::parser comment-2.2 \ + -elementstartcommand Estart \ + -elementendcommand EStop \ + -characterdatacommand pcdata \ + -commentcommand comment] + $parser parse { + + +} + list $::comment $::result $::element +} {{ + + + + + + } {} 0} + +set comment_2_3 [format { + + +
+ } \}] + +test comment-2.3 {comment with entities} { + set ::result {} + set ::comment {} + set ::element 0 + + catch {rename xml::comment-2.3 {}} + set parser [xml::parser comment-2.3 \ + -elementstartcommand Estart \ + -elementendcommand EStop \ + -characterdatacommand pcdata \ + -commentcommand comment] + $parser parse " + + + + + + + + +" + +list [string compare $::comment ${comment_2_3}] [string trim $::result] $::element +} [list 0 {} 2] + + +# cleanup +::tcltest::cleanupTests +return ADDED tests/data/REC-xslt-19991116.xml Index: tests/data/REC-xslt-19991116.xml ================================================================== --- /dev/null +++ tests/data/REC-xslt-19991116.xml @@ -0,0 +1,6363 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +]> + +
+XSL Transformations (XSLT) +Version 1.0 +&LEV;-xslt-&YYYYMMDD; +W3C Recommendation +&day;&month;&year; + +http://www.w3.org/TR/&year;/&LEV;-xslt-&YYYYMMDD; +XML +HTML + + + +http://www.w3.org/TR/xslt + + +http://www.w3.org/TR/1999/PR-xslt-19991008 +http://www.w3.org/1999/08/WD-xslt-19990813 +http://www.w3.org/1999/07/WD-xslt-19990709 +http://www.w3.org/TR/1999/WD-xslt-19990421 +http://www.w3.org/TR/1998/WD-xsl-19981216 +http://www.w3.org/TR/1998/WD-xsl-19980818 + + + +James Clark +jjc@jclark.com + + + + + +

This document has been reviewed by W3C Members and other interested +parties and has been endorsed by the Director as a W3C Recommendation. It +is a stable document and may be used as reference material or cited as +a normative reference from other documents. W3C's role in making the +Recommendation is to draw attention to the specification and to +promote its widespread deployment. This enhances the functionality and +interoperability of the Web.

+ +

The list of known errors in this specification is available at +http://www.w3.org/&year;/&MM;/&LEV;-xslt-&YYYYMMDD;-errata.

+ +

Comments on this specification may be sent to xsl-editors@w3.org; archives +of the comments are available. Public discussion of XSL, including +XSL Transformations, takes place on the XSL-List +mailing list.

+ +

The English version of this specification is the only normative +version. However, for translations of this document, see http://www.w3.org/Style/XSL/translations.html.

+ +

A list of current W3C Recommendations and other technical documents +can be found at http://www.w3.org/TR.

+ +

This specification has been produced as part of the W3C Style activity.

+ +
+ + + +

This specification defines the syntax and semantics of XSLT, which +is a language for transforming XML documents into other XML +documents.

+ +

XSLT is designed for use as part of XSL, which is a stylesheet +language for XML. In addition to XSLT, XSL includes an XML vocabulary +for specifying formatting. XSL specifies the styling of an XML +document by using XSLT to describe how the document is transformed +into another XML document that uses the formatting vocabulary.

+ +

XSLT is also designed to be used independently of XSL. However, +XSLT is not intended as a completely general-purpose XML +transformation language. Rather it is designed primarily for the +kinds of transformations that are needed when XSLT is used as part of +XSL.

+ +
+ + +English +EBNF + + + +See RCS log for revision history. + + +
+ + +Introduction + +

This specification defines the syntax and semantics of the XSLT +language. A transformation in the XSLT language is expressed as a +well-formed XML document conforming to the +Namespaces in XML Recommendation , which may +include both elements that are defined by XSLT and elements that are +not defined by XSLT. XSLT-defined elements are distinguished by belonging to a +specific XML namespace (see ), which is +referred to in this specification as the XSLT +namespace. Thus this specification is a definition of +the syntax and semantics of the XSLT namespace.

+ +

A transformation expressed in XSLT describes rules for transforming +a source tree into a result tree. The transformation is achieved by +associating patterns with templates. A pattern is matched against +elements in the source tree. A template is instantiated to create +part of the result tree. The result tree is separate from the source +tree. The structure of the result tree can be completely different +from the structure of the source tree. In constructing the result +tree, elements from the source tree can be filtered and reordered, and +arbitrary structure can be added.

+ +

A transformation expressed in XSLT is called a stylesheet. This is +because, in the case when XSLT is transforming into the XSL formatting +vocabulary, the transformation functions as a stylesheet.

+ +

This document does not specify how an XSLT stylesheet is associated +with an XML document. It is recommended that XSL processors support +the mechanism described in . When this or any +other mechanism yields a sequence of more than one XSLT stylesheet to +be applied simultaneously to a XML document, then the effect +should be the same as applying a single stylesheet that imports each +member of the sequence in order (see ).

+ +

A stylesheet contains a set of template rules. A template rule has +two parts: a pattern which is matched against nodes in the source tree +and a template which can be instantiated to form part of the result +tree. This allows a stylesheet to be applicable to a wide class of +documents that have similar source tree structures.

+ +

A template is instantiated for a particular source element +to create part of the result tree. A template can contain elements +that specify literal result element structure. A template can also +contain elements from the XSLT namespace +that are instructions for creating result tree +fragments. When a template is instantiated, each instruction is +executed and replaced by the result tree fragment that it creates. +Instructions can select and process descendant source elements. Processing a +descendant element creates a result tree fragment by finding the +applicable template rule and instantiating its template. Note +that elements are only processed when they have been selected by the +execution of an instruction. The result tree is constructed by +finding the template rule for the root node and instantiating +its template.

+ +

In the process of finding the applicable template rule, more +than one template rule may have a pattern that matches a given +element. However, only one template rule will be applied. The +method for deciding which template rule to apply is described +in .

+ +

A single template by itself has considerable power: it can create +structures of arbitrary complexity; it can pull string values out of +arbitrary locations in the source tree; it can generate structures +that are repeated according to the occurrence of elements in the +source tree. For simple transformations where the structure of the +result tree is independent of the structure of the source tree, a +stylesheet can often consist of only a single template, which +functions as a template for the complete result tree. Transformations +on XML documents that represent data are often of this kind (see +). XSLT allows a simplified syntax for +such stylesheets (see ).

+ +

When a template is instantiated, it is always instantiated with +respect to a current node and a current node +list. The current node is always a member of the +current node list. Many operations in XSLT are relative to the +current node. Only a few instructions change the current node list or +the current node (see and ); during the instantiation of one of these +instructions, the current node list changes to a new list of nodes and +each member of this new list becomes the current node in turn; after +the instantiation of the instruction is complete, the current node and +current node list revert to what they were before the instruction was +instantiated.

+ +

XSLT makes use of the expression language defined by for selecting elements for processing, for conditional +processing and for generating text.

+ +

XSLT provides two hooks for extending the language, +one hook for extending the set of instruction elements used in +templates and one hook for extending the set of functions used in +XPath expressions. These hooks are both based on XML namespaces. +This version of XSLT does not define a mechanism for implementing the +hooks. See .

+ +

The XSL WG intends to define such a mechanism in a future +version of this specification or in a separate +specification.

+ +

The element syntax summary notation used to describe the syntax of +XSLT-defined elements is described in .

+ +

The MIME media types text/xml and +application/xml should be used +for XSLT stylesheets. It is possible that a media type will be +registered specifically for XSLT stylesheets; if and when it is, that +media type may also be used.

+ +
+ + +Stylesheet Structure + + +XSLT Namespace + +

The XSLT namespace has the URI &XSLT.ns;.

+ +

The 1999 in the URI indicates the year in which +the URI was allocated by the W3C. It does not indicate the version of +XSLT being used, which is specified by attributes (see and ).

+ +

XSLT processors must use the XML namespaces mechanism to recognize elements and attributes from this +namespace. Elements from the XSLT namespace are recognized only in the +stylesheet not in the source document. The complete list of +XSLT-defined elements is specified in . Vendors must not extend the XSLT +namespace with additional elements or attributes. Instead, any +extension must be in a separate namespace. Any namespace that is used +for additional instruction elements must be identified by means of the +extension element mechanism specified in .

+ +

This specification uses a prefix of xsl: for referring +to elements in the XSLT namespace. However, XSLT stylesheets are free +to use any prefix, provided that there is a namespace declaration that +binds the prefix to the URI of the XSLT namespace.

+ +

An element from the XSLT namespace may have any attribute not from +the XSLT namespace, provided that the expanded-name of the +attribute has a non-null namespace URI. The presence of such +attributes must not change the behavior of XSLT elements and functions +defined in this document. Thus, an XSLT processor is always free to +ignore such attributes, and must ignore such attributes without giving +an error if it does not recognize the namespace URI. Such attributes +can provide, for example, unique identifiers, optimization hints, or +documentation.

+ +

It is an error for an element from the XSLT namespace to have +attributes with expanded-names that have null namespace URIs +(i.e. attributes with unprefixed names) other than attributes defined +for the element in this document.

+ +

The conventions used for the names of XSLT elements, +attributes and functions are that names are all lower-case, use +hyphens to separate words, and use abbreviations only if they already +appear in the syntax of a related language such as XML or +HTML.

+ + +
+ + +Stylesheet Element + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

A stylesheet is represented by an xsl:stylesheet +element in an XML document. xsl:transform is allowed as +a synonym for xsl:stylesheet.

+ +

An xsl:stylesheet element must have a +version attribute, indicating the version of XSLT that +the stylesheet requires. For this version of XSLT, the value should +be 1.0. When the value is not equal to 1.0, +forwards-compatible processing mode is enabled (see ).

+ +

The xsl:stylesheet element may contain the following types +of elements:

+ +

xsl:import

+

xsl:include

+

xsl:strip-space

+

xsl:preserve-space

+

xsl:output

+

xsl:key

+

xsl:decimal-format

+

xsl:namespace-alias

+

xsl:attribute-set

+

xsl:variable

+

xsl:param

+

xsl:template

+
+ +

An element occurring as +a child of an xsl:stylesheet element is called a +top-level element.

+ +

This example shows the structure of a stylesheet. Ellipses +(...) indicate where attribute values or content have +been omitted. Although this example shows one of each type of allowed +element, stylesheets may contain zero or more of each of these +elements.

+ +<xsl:stylesheet version="1.0" + xmlns:xsl="&XSLT.ns;"> + + + + + + + + + + + + + + + + + ... + + + ... + + ... + + + ... + + + + ... + + +]]> + +

The order in which the children of the xsl:stylesheet +element occur is not significant except for xsl:import +elements and for error recovery. Users are free to order the elements +as they prefer, and stylesheet creation tools need not provide control +over the order in which the elements occur.

+ +

In addition, the xsl:stylesheet element may contain +any element not from the XSLT namespace, provided that the +expanded-name of the element has a non-null namespace URI. The presence of +such top-level elements must not change the behavior of XSLT elements +and functions defined in this document; for example, it would not be +permitted for such a top-level element to specify that +xsl:apply-templates was to use different rules to resolve +conflicts. Thus, an XSLT processor is always free to ignore such +top-level elements, and must ignore a top-level element without giving +an error if it does not recognize the namespace URI. Such elements can +provide, for example,

+ + + +

information used by extension elements or extension functions +(see ),

+ +

information about what to do with the result tree,

+ +

information about how to obtain the source tree,

+ +

metadata about the stylesheet,

+ +

structured documentation for the stylesheet.

+ +
+ +
+ + +Literal Result Element as Stylesheet + +

A simplified syntax is allowed for stylesheets that consist of only +a single template for the root node. The stylesheet may consist of +just a literal result element (see ). Such a stylesheet is equivalent to a +stylesheet with an xsl:stylesheet element containing a +template rule containing the literal result element; the template rule +has a match pattern of /. For example

+ +<html xsl:version="1.0" + xmlns:xsl="&XSLT.ns;" + xmlns="&XHTML.ns;"> + Expense Report Summary + + +

Total Amount:

+ +]]>
+ +

has the same meaning as

+ +<xsl:stylesheet version="1.0" + xmlns:xsl="&XSLT.ns;" + xmlns="&XHTML.ns;"> + + + Expense Report Summary + + +

Total Amount:

+ + + +]]>
+ +

A literal result element that is the document element of a +stylesheet must have an xsl:version attribute, which +indicates the version of XSLT that the stylesheet requires. For this +version of XSLT, the value should be 1.0; the value must +be a Number. Other literal result +elements may also have an xsl:version attribute. When the +xsl:version attribute is not equal to 1.0, +forwards-compatible processing mode is enabled (see ).

+ +

The allowed content of a literal result element when used as a +stylesheet is no different from when it occurs within a +stylesheet. Thus, a literal result element used as a stylesheet cannot +contain top-level elements.

+ +

In some situations, the only way that a system can recognize that an +XML document needs to be processed by an XSLT processor as an XSLT +stylesheet is by examining the XML document itself. Using the +simplified syntax makes this harder.

+ +

For example, another XML language (AXL) might also use an +axl:version on the document element to indicate that an +XML document was an AXL document that required processing by an AXL +processor; if a document had both an axl:version +attribute and an xsl:version attribute, it would be +unclear whether the document should be processed by an XSLT processor +or an AXL processor.

+ +

Therefore, the simplified syntax should not be used for XSLT +stylesheets that may be used in such a situation. This situation can, +for example, arise when an XSLT stylesheet is transmitted as a message +with a MIME media type of text/xml or +application/xml to a recipient that will use the MIME +media type to determine how the message is processed.

+ +
+ + +Qualified Names + +

The name of an internal XSLT object, specifically a named template +(see ), a mode (see ), an attribute set (see ), a key (see ), a +decimal-format (see ), a variable or a +parameter (see ) is specified as a QName. If it has a prefix, then the +prefix is expanded into a URI reference using the namespace +declarations in effect on the attribute in which the name occurs. The +expanded-name +consisting of the local part of the name and the possibly null URI +reference is used as the name of the object. The default namespace is +not used for unprefixed names.

+ +
+ + +Forwards-Compatible Processing + +

An element enables forwards-compatible mode for itself, its +attributes, its descendants and their attributes if either it is an +xsl:stylesheet element whose version +attribute is not equal to 1.0, or it is a literal result +element that has an xsl:version attribute whose value is +not equal to 1.0, or it is a literal result element that +does not have an xsl:version attribute and that is the +document element of a stylesheet using the simplified syntax (see +). A literal result element +that has an xsl:version attribute whose value is equal to +1.0 disables forwards-compatible mode for itself, its +attributes, its descendants and their attributes.

+ +

If an element is processed in forwards-compatible mode, then:

+ + + +

if it is a top-level +element and XSLT 1.0 does not allow such elements as top-level +elements, then the element must be ignored along with its +content;

+ +

if it is an element in a template and XSLT 1.0 does not allow +such elements to occur in templates, then if the element is not +instantiated, an error must not be signaled, and if the element is +instantiated, the XSLT must perform fallback for the element as +specified in ;

+ +

if the element has an attribute that XSLT 1.0 does not allow +the element to have or if the element has an optional attribute with a +value that the XSLT 1.0 does not allow the attribute to have, then the +attribute must be ignored.

+ +
+ +

Thus, any XSLT 1.0 processor must be able to process the following +stylesheet without error, although the stylesheet includes elements +from the XSLT namespace that are not defined in this +specification:

+ +<xsl:stylesheet version="1.1" + xmlns:xsl="&XSLT.ns;"> + + + + + + + + XSLT 1.1 required + + +

Sorry, this stylesheet requires XSLT 1.1.

+ + +
+
+ +]]>
+ +

If a stylesheet depends crucially on a top-level element +introduced by a version of XSL after 1.0, then the stylesheet can use +an xsl:message element with terminate="yes" +(see ) to ensure that XSLT processors +implementing earlier versions of XSL will not silently ignore the +top-level element. For example,

+ +<xsl:stylesheet version="1.5" + xmlns:xsl="&XSLT.ns;"> + + + + + + Sorry, this stylesheet requires XSLT 1.1. + + + + ... + + + + ... +]]> +
+ +

If an expression occurs in +an attribute that is processed in forwards-compatible mode, then an +XSLT processor must recover from errors in the expression as +follows:

+ + + +

if the expression does not match the syntax allowed by the +XPath grammar, then an error must not be signaled unless the +expression is actually evaluated;

+ +

if the expression calls a function with an unprefixed name +that is not part of the XSLT library, then an error must not be +signaled unless the function is actually called;

+ +

if the expression calls a function with a number of arguments +that XSLT does not allow or with arguments of types that XSLT does not +allow, then an error must not be signaled unless the function is +actually called.

+ +
+ + +
+ + +Combining Stylesheets + +

XSLT provides two mechanisms to combine stylesheets:

+ + + +an inclusion mechanism that allows stylesheets to be combined +without changing the semantics of the stylesheets being combined, +and + +an import mechanism that allows stylesheets to override each +other. + + + + +Stylesheet Inclusion + + + + + + + + + +

An XSLT stylesheet may include another XSLT stylesheet using an +xsl:include element. The xsl:include element +has an href attribute whose value is a URI reference +identifying the stylesheet to be included. A relative URI is resolved +relative to the base URI of the xsl:include element (see +).

+ +

The xsl:include element is only allowed as a top-level element.

+ +

The inclusion works at the XML tree level. The resource located by +the href attribute value is parsed as an XML document, +and the children of the xsl:stylesheet element in this +document replace the xsl:include element in the including +document. The fact that template rules or definitions are included +does not affect the way they are processed.

+ +

The included stylesheet may use the simplified syntax described in +. The included stylesheet +is treated the same as the equivalent xsl:stylesheet +element.

+ +

It is an error if a stylesheet directly or indirectly includes +itself.

+ +

Including a stylesheet multiple times can cause errors +because of duplicate definitions. Such multiple inclusions are less +obvious when they are indirect. For example, if stylesheet +B includes stylesheet A, stylesheet C +includes stylesheet A, and stylesheet D includes +both stylesheet B and stylesheet C, then +A will be included indirectly by D twice. If +all of B, C and D are used as +independent stylesheets, then the error can be avoided by separating +everything in B other than the inclusion of A +into a separate stylesheet B' and changing B to +contain just inclusions of B' and A, similarly +for C, and then changing D to include +A, B', C'.

+ +
+ + +Stylesheet Import + + + + + + + + +

An XSLT stylesheet may import another XSLT stylesheet using an +xsl:import element. Importing a stylesheet is the same +as including it (see ) except that definitions +and template rules in the importing stylesheet take precedence over +template rules and definitions in the imported stylesheet; this is +described in more detail below. The xsl:import element +has an href attribute whose value is a URI reference +identifying the stylesheet to be imported. A relative URI is resolved +relative to the base URI of the xsl:import element (see +).

+ +

The xsl:import element is only allowed as a top-level element. The +xsl:import element children must precede all other +element children of an xsl:stylesheet element, including +any xsl:include element children. When +xsl:include is used to include a stylesheet, any +xsl:import elements in the included document are moved up +in the including document to after any existing +xsl:import elements in the including document.

+ +

For example,

+ +<xsl:stylesheet version="1.0" + xmlns:xsl="&XSLT.ns;"> + + + italic + +]]> + +

The +xsl:stylesheet elements encountered during processing of +a stylesheet that contains xsl:import elements are +treated as forming an import tree. In the import tree, +each xsl:stylesheet element has one import child for each +xsl:import element that it contains. Any +xsl:include elements are resolved before constructing the +import tree. An xsl:stylesheet element in the import tree +is defined to have lower import precedence than another +xsl:stylesheet element in the import tree if it would be +visited before that xsl:stylesheet element in a +post-order traversal of the import tree (i.e. a traversal of the +import tree in which an xsl:stylesheet element is visited +after its import children). Each definition and template +rule has import precedence determined by the +xsl:stylesheet element that contains it.

+ +

For example, suppose

+ + + +

stylesheet A imports stylesheets B +and C in that order;

+ +

stylesheet B imports stylesheet +D;

+ +

stylesheet C imports stylesheet +E.

+ +
+ +

Then the order of import precedence (lowest first) is +D, B, E, C, +A.

+ +

Since xsl:import elements are required to occur +before any definitions or template rules, an implementation that +processes imported stylesheets at the point at which it encounters the +xsl:import element will encounter definitions and +template rules in increasing order of import precedence.

+ +

In general, a definition or template rule with higher import +precedence takes precedence over a definition or template rule with +lower import precedence. This is defined in detail for each kind of +definition and for template rules.

+ +

It is an error if a stylesheet directly or indirectly imports +itself. Apart from this, the case where a stylesheet with a particular +URI is imported in multiple places is not treated specially. The +import tree will have a +separate xsl:stylesheet for each place that it is +imported.

+ +

If xsl:apply-imports is used (see ), the behavior may be different from the +behavior if the stylesheet had been imported only at the place with +the highest import +precedence.

+ +
+ +
+ + +Embedding Stylesheets + +

Normally an XSLT stylesheet is a complete XML document with the +xsl:stylesheet element as the document element. However, +an XSLT stylesheet may also be embedded in another resource. Two forms +of embedding are possible:

+ + + +the XSLT stylesheet may be textually embedded in a non-XML +resource, or + +the xsl:stylesheet element may occur in an XML +document other than as the document element. + + + +

To facilitate the second form of embedding, the +xsl:stylesheet element is allowed to have an ID attribute +that specifies a unique identifier.

+ +

In order for such an attribute to be used with the XPath +id function, it must actually be declared in +the DTD as being an ID.

+ +

The following example shows how the xml-stylesheet +processing instruction can be used to allow a +document to contain its own stylesheet. The URI reference uses a +relative URI with a fragment identifier to locate the +xsl:stylesheet element:

+ + + + + + + xmlns:xsl="&XSLT.ns;" + xmlns:fo="&XSLFO.ns;"> + + + + + + + + + + +... + + + +]]> + +

A stylesheet that is embedded in the document to which it is +to be applied or that may be included or imported into an stylesheet +that is so embedded typically needs to contain a template rule that +specifies that xsl:stylesheet elements are to be +ignored.

+ +
+ +
+ + +Data Model + +

The data model used by XSLT is the same as that used by XPath with the additions +described in this section. XSLT operates on source, result and +stylesheet documents using the same data model. Any two XML documents +that have the same tree will be treated the same by XSLT.

+ +

Processing instructions and comments in the stylesheet are ignored: +the stylesheet is treated as if neither processing instruction nodes +nor comment nodes were included in the tree that represents the +stylesheet.

+ + +Root Node Children + +

The normal restrictions on the children of the root node are +relaxed for the result tree. The result tree may have any sequence of +nodes as children that would be possible for an element node. In +particular, it may have text node children, and any number of element +node children. When written out using the XML output method (see +), it is possible that a result tree will not +be a well-formed XML document; however, it will always be a +well-formed external general parsed entity.

+ +

When the source tree is created by parsing a well-formed XML +document, the root node of the source tree will automatically satisfy +the normal restrictions of having no text node children and exactly +one element child. When the source tree is created in some other way, +for example by using the DOM, the usual restrictions are relaxed for +the source tree as for the result tree.

+ +
+ + +Base URI + +

Every node also has an associated URI called its base URI, which is +used for resolving attribute values that represent relative URIs into +absolute URIs. If an element or processing instruction occurs in an +external entity, the base URI of that element or processing +instruction is the URI of the external entity; otherwise, the base URI +is the base URI of the document. The base URI of the document node is +the URI of the document entity. The base URI for a text node, a +comment node, an attribute node or a namespace node is the base URI of +the parent of the node.

+ +
+ + +Unparsed Entities + +

The root node has a mapping that gives the URI for each unparsed +entity declared in the document's DTD. The URI is generated from the +system identifier and public identifier specified in the entity +declaration. The XSLT processor may use the public identifier to +generate a URI for the entity instead of the URI specified in the +system identifier. If the XSLT processor does not use the public +identifier to generate the URI, it must use the system identifier; if +the system identifier is a relative URI, it must be resolved into an +absolute URI using the URI of the resource containing the entity +declaration as the base URI .

+ +
+ + +Whitespace Stripping + +

After the tree for a source document or stylesheet document has +been constructed, but before it is otherwise processed by XSLT, +some text nodes are stripped. A text node is never stripped +unless it contains only whitespace characters. Stripping the text +node removes the text node from the tree. The stripping process takes +as input a set of element names for which whitespace must be +preserved. The stripping process is applied to both stylesheets and +source documents, but the set of whitespace-preserving element names +is determined differently for stylesheets and for source +documents.

+ +

A text node is preserved if any of the following apply:

+ + + +

The element name of the parent of the text node is in the set +of whitespace-preserving element names.

+ +

The text node contains at least one non-whitespace character. +As in XML, a whitespace character is #x20, #x9, #xD or #xA.

+ +

An ancestor element of the text node has an +xml:space attribute with a value of +preserve, and no closer ancestor element has +xml:space with a value of +default.

+ +
+ +

Otherwise, the text node is stripped.

+ +

The xml:space attributes are not stripped from the +tree.

+ +

This implies that if an xml:space attribute is +specified on a literal result element, it will be included in the +result.

+ +

For stylesheets, the set of whitespace-preserving element names +consists of just xsl:text.

+ + + + + + + + + + + + + + + + + +

For source documents, the set of whitespace-preserving element +names is specified by xsl:strip-space and +xsl:preserve-space top-level elements. These elements each +have an elements attribute whose value is a +whitespace-separated list of NameTests. Initially, the +set of whitespace-preserving element names contains all element names. +If an element name matches a NameTest in an +xsl:strip-space element, then it is removed from the set +of whitespace-preserving element names. If an element name matches a +NameTest in an +xsl:preserve-space element, then it is added to the set +of whitespace-preserving element names. An element matches a NameTest if and only if the +NameTest would be true +for the element as an XPath node +test. Conflicts between matches to +xsl:strip-space and xsl:preserve-space +elements are resolved the same way as conflicts between template rules +(see ). Thus, the applicable match for a +particular element name is determined as follows:

+ + + +

First, any match with lower import precedence than another +match is ignored.

+ +

Next, any match with a NameTest that has a lower +default priority than the +default priority of the +NameTest of another +match is ignored.

+ +
+ +

It is an error if this leaves more than one match. An XSLT +processor may signal the error; if it does not signal the error, it +must recover by choosing, from amongst the matches that are left, the +one that occurs last in the stylesheet.

+ +
+ +
+ + +Expressions + +

XSLT uses the expression language defined by XPath . Expressions are used in XSLT for a variety of purposes +including:

+ + +selecting nodes for processing; +specifying conditions for different ways of processing a node; +generating text to be inserted in the result tree. + + +

An +expression must match the XPath production Expr.

+ +

Expressions occur as the value of certain attributes on +XSLT-defined elements and within curly braces in attribute value +templates.

+ +

In XSLT, an outermost expression (i.e. an expression that is not +part of another expression) gets its context as follows:

+ + + +

the context node comes from the current node

+ +

the context position comes from the position of the current node in the current node list; the first +position is 1

+ +

the context size comes from the size of the current node list

+ +

the variable bindings are the bindings in scope on the +element which has the attribute in which the expression occurs (see +)

+ +

the set of namespace declarations are those in scope on the +element which has the attribute in which the expression occurs; +this includes the implicit declaration of the prefix xml +required by the the XML Namespaces Recommendation ; +the default +namespace (as declared by xmlns) is not part of this +set

+ +

the function library consists of the core function library +together with the additional functions defined in and extension functions as described in ; it is an error for an expression to include a call +to any other function

+ +
+ +
+ + +Template Rules + + +Processing Model + +

A list of source nodes is processed to create a result tree +fragment. The result tree is constructed by processing a list +containing just the root node. A list of source nodes is processed by +appending the result tree structure created by processing each of the +members of the list in order. A node is processed by finding all the +template rules with patterns that match the node, and choosing the +best amongst them; the chosen rule's template is then instantiated +with the node as the current +node and with the list of source nodes as the current node list. A template +typically contains instructions that select an additional list of +source nodes for processing. The process of matching, instantiation +and selection is continued recursively until no new source nodes are +selected for processing.

+ +

Implementations are free to process the source document in any way +that produces the same result as if it were processed using this +processing model.

+ +
+ + +Patterns + +

Template rules identify the +nodes to which they apply by using a pattern. As well as +being used in template rules, patterns are used for numbering (see +) and for declaring keys (see ). A pattern specifies a set of conditions on a node. A +node that satisfies the conditions matches the pattern; a node that +does not satisfy the conditions does not match the pattern. The +syntax for patterns is a subset of the syntax for expressions. In +particular, location paths that meet certain restrictions can be used +as patterns. An expression that is also a pattern always evaluates to +an object of type node-set. A node matches a pattern if the node is a +member of the result of evaluating the pattern as an expression with +respect to some possible context; the possible contexts are those +whose context node is the node being matched or one of its +ancestors.

+ +

Here are some examples of patterns:

+ + + +

para matches any para element

+ +

* matches any element

+ +

chapter|appendix matches any +chapter element and any appendix +element

+ +

olist/item matches any item element with +an olist parent

+ +

appendix//para matches any para element with +an appendix ancestor element

+ +

/ matches the root node

+ +

text() matches any text node

+ +

processing-instruction() matches any processing +instruction

+ +

node() matches any node other than an attribute +node and the root node

+ +

id("W11") matches the element with unique ID +W11

+ +

para[1] matches any para element +that is the first para child element of its +parent

+ +

*[position()=1 and self::para] matches any +para element that is the first child element of its +parent

+ +

para[last()=1] matches any para +element that is the only para child element of its +parent

+ +

items/item[position()>1] matches any +item element that has a items parent and +that is not the first item child of its parent

+ +

item[position() mod 2 = 1] would be true for any +item element that is an odd-numbered item +child of its parent.

+ +

div[@class="appendix"]//p matches any +p element with a div ancestor element that +has a class attribute with value +appendix

+ +

@class matches any class attribute +(not any element that has a class +attribute)

+ +

@* matches any attribute

+ +
+ +

A pattern must match the grammar for Pattern. A Pattern is +a set of location path patterns separated by |. A +location path pattern is a location path whose steps all use only the +child or attribute axes. Although patterns +must not use the descendant-or-self axis, patterns may +use the // operator as well as the / +operator. Location path patterns can also start with an +id or key function call +with a literal argument. Predicates in a pattern can use arbitrary +expressions just like predicates in a location path.

+ + +Patterns + + +Pattern +LocationPathPattern +| Pattern '|' LocationPathPattern + + +LocationPathPattern +'/' RelativePathPattern? +| IdKeyPattern (('/' | '//') RelativePathPattern)? +| '//'? RelativePathPattern + + +IdKeyPattern +'id' '(' Literal ')' +| 'key' '(' Literal ',' Literal ')' + + +RelativePathPattern +StepPattern +| RelativePathPattern '/' StepPattern +| RelativePathPattern '//' StepPattern + + +StepPattern + +ChildOrAttributeAxisSpecifier +NodeTest +Predicate* + + + +ChildOrAttributeAxisSpecifier +AbbreviatedAxisSpecifier +| ('child' | 'attribute') '::' + + + + +

A pattern is defined to match a node if and only if there is +possible context such that when the pattern is evaluated as an +expression with that context, the node is a member of the resulting +node-set. When a node is being matched, the possible contexts have a +context node that is the node being matched or any ancestor of that +node, and a context node list containing just the context node.

+ +

For example, p matches any p element, +because for any p if the expression p is +evaluated with the parent of the p element as context the +resulting node-set will contain that p element as one of +its members.

+ +

This matches even a p element that is the +document element, since the document root is the parent of the +document element.

+ +

Although the semantics of patterns are specified indirectly in +terms of expression evaluation, it is easy to understand the meaning +of a pattern directly without thinking in terms of expression +evaluation. In a pattern, | indicates alternatives; a +pattern with one or more | separated alternatives matches +if any one of the alternative matches. A pattern that consists of a +sequence of StepPatterns separated by +/ or // is matched from right to left. The +pattern only matches if the rightmost StepPattern matches and a suitable element +matches the rest of the pattern; if the separator is / +then only the parent is a suitable element; if the separator is +//, then any ancestor is a suitable element. A StepPattern that uses the child axis matches +if the NodeTest is true for the +node and the node is not an attribute node. A StepPattern that uses the attribute axis +matches if the NodeTest is true +for the node and the node is an attribute node. When [] +is present, then the first PredicateExpr in a StepPattern is evaluated with the node being +matched as the context node and the siblings of the context node that +match the NodeTest as the +context node list, unless the node being matched is an attribute node, +in which case the context node list is all the attributes that have +the same parent as the attribute being matched and that match the NameTest.

+ +

For example

+ +appendix//ulist/item[position()=1] + +

matches a node if and only if all of the following are true:

+ + + +

the NodeTest item is +true for the node and the node is not an attribute; in other words the +node is an item element

+ +

evaluating the PredicateExpr +position()=1 with the node as context node and the +siblings of the node that are item elements as the +context node list yields true

+ +

the node has a parent that matches +appendix//ulist; this will be true if the parent is a +ulist element that has an appendix ancestor +element.

+ +
+ +
+ + +Defining Template Rules + + + + + + + + + + + + + + + + + + + + + +

A template rule is specified with the xsl:template +element. The match attribute is a Pattern that identifies the source node or nodes +to which the rule applies. The match attribute is +required unless the xsl:template element has a +name attribute (see ). +It is an error for the value of the match attribute to +contain a VariableReference. The +content of the xsl:template element is the template that +is instantiated when the template rule is applied.

+ +

For example, an XML document might contain:

+ +important point.]]> + +

The following template rule matches emph elements and +produces a fo:inline-sequence formatting object with a +font-weight property of bold.

+ + + + + + +]]> + +

Examples in this document use the fo: prefix for +the namespace &XSLFO.ns;, which is +the namespace of the formatting objects defined in .

+ +

As described next, the xsl:apply-templates element +recursively processes the children of the source element.

+ +
+ + +Applying Template Rules + + + + + + + + + + + + + + + +

This example creates a block for a chapter element and +then processes its immediate children.

+ + + + + +]]> + +

In the absence of a select attribute, the +xsl:apply-templates instruction processes all of the +children of the current node, including text nodes. However, text +nodes that have been stripped as specified in +will not be processed. If stripping of whitespace nodes has not been +enabled for an element, then all whitespace in the content of the +element will be processed as text, and thus whitespace +between child elements will count in determining the position of a +child element as returned by the position +function.

+ +

A select attribute can be used to process nodes +selected by an expression instead of processing all children. The +value of the select attribute is an expression. The expression must +evaluate to a node-set. The selected set of nodes is processed in +document order, unless a sorting specification is present (see +). The following example processes all of the +author children of the author-group:

+ + + + + +]]> + +

The following example processes all of the given-names +of the authors that are children of +author-group:

+ + + + + +]]> + +

This example processes all of the heading descendant +elements of the book element.

+ + + + + +]]> + +

It is also possible to process elements that are not descendants of +the current node. This example assumes that a department +element has group children and employee +descendants. It finds an employee's department and then processes +the group children of the department.

+ + + + Employee belongs to group + + +]]> + +

Multiple xsl:apply-templates elements can be used within a +single template to do simple reordering. The following example +creates two HTML tables. The first table is filled with domestic sales +while the second table is filled with foreign sales.

+ + + + +
+ + +
+]]>
+ + + +

It is possible for there to be two matching descendants where one +is a descendant of the other. This case is not treated specially: +both descendants will be processed as usual. For example, given a +source document

+ +
]]>
+ +

the rule

+ + + +]]> + +

will process both the outer div and inner div +elements.

+ +
+ +

Typically, xsl:apply-templates is used to +process only nodes that are descendants of the current node. Such use +of xsl:apply-templates cannot result in non-terminating +processing loops. However, when xsl:apply-templates is +used to process elements that are not descendants of the current node, +the possibility arises of non-terminating loops. For example,

+ + + +]]> + +

Implementations may be able to detect such loops in some cases, but +the possibility exists that a stylesheet may enter a non-terminating +loop that an implementation is unable to detect. This may present a +denial of service security risk.

+ +
+ + +Conflict Resolution for Template Rules + +

It is possible for a source node to match more than one template +rule. The template rule to be used is determined as follows:

+ + + +

First, all matching template rules that have lower import precedence than the +matching template rule or rules with the highest import precedence are +eliminated from consideration.

+ +

Next, all matching template rules that have lower priority +than the matching template rule or rules with the highest priority are +eliminated from consideration. The priority of a template rule is +specified by the priority attribute on the template rule. +The value of this must be a real number (positive or negative), +matching the production Number +with an optional leading minus sign (-). The default +priority is computed as follows:

+ + + +

If the pattern contains multiple alternatives separated by +|, then it is treated equivalently to a set of template +rules, one for each alternative.

+ +

If the pattern has the form of a QName preceded by a ChildOrAttributeAxisSpecifier +or has the form +processing-instruction(Literal) preceded by a ChildOrAttributeAxisSpecifier, +then the priority is 0.

+ +

If the pattern has the form NCName:* preceded by a +ChildOrAttributeAxisSpecifier, +then the priority is -0.25.

+ +

Otherwise, if the pattern consists of just a NodeTest preceded by a ChildOrAttributeAxisSpecifier, +then the priority is -0.5.

+ +

Otherwise, the priority is 0.5.

+ +
+ +

Thus, the most common kind of pattern (a pattern that tests for a +node with a particular type and a particular expanded-name) has +priority 0. The next less specific kind of pattern (a pattern that +tests for a node with a particular type and an expanded-name with a +particular namespace URI) has priority -0.25. Patterns less specific +than this (patterns that just tests for nodes with particular types) +have priority -0.5. Patterns more specific than the most common kind +of pattern have priority 0.5.

+ +
+ +
+ +

It is an error if this leaves more than one matching template +rule. An XSLT processor may signal the error; if it does not signal +the error, it must recover by choosing, from amongst the matching +template rules that are left, the one that occurs last in the +stylesheet.

+ +
+ + +Overriding Template Rules + + + + + + +

A template rule that is being used to override a template rule in +an imported stylesheet (see ) can use the +xsl:apply-imports element to invoke the overridden +template rule.

+ +

At any point in the processing of a stylesheet, there is a +current template rule. Whenever a template rule is +chosen by matching a pattern, the template rule becomes the current +template rule for the instantiation of the rule's template. When an +xsl:for-each element is instantiated, the current +template rule becomes null for the instantiation of the content of the +xsl:for-each element.

+ +

xsl:apply-imports processes the current node using +only template rules that were imported into the stylesheet element +containing the current template rule; the node is processed in the +current template rule's mode. It is an error if +xsl:apply-imports is instantiated when the current +template rule is null.

+ +

For example, suppose the stylesheet doc.xsl contains a +template rule for example elements:

+ + +
+]]>
+ +

Another stylesheet could import doc.xsl and modify the +treatment of example elements as follows:

+ + + + +
+ +
+
]]>
+ +

The combined effect would be to transform an example +into an element of the form:

+ +
...
]]>
+ +
+ + +Modes + +

Modes allow an element to be processed multiple times, each time +producing a different result.

+ +

Both xsl:template and xsl:apply-templates +have an optional mode attribute. The value of the +mode attribute is a QName, which is expanded as described +in . If xsl:template does not have +a match attribute, it must not have a mode +attribute. If an xsl:apply-templates element has a +mode attribute, then it applies only to those template +rules from xsl:template elements that have a +mode attribute with the same value; if an +xsl:apply-templates element does not have a +mode attribute, then it applies only to those template +rules from xsl:template elements that do not have a +mode attribute.

+ +
+ + +Built-in Template Rules + +

There is a built-in template rule to allow recursive processing to +continue in the absence of a successful pattern match by an explicit +template rule in the stylesheet. This template rule applies to both +element nodes and the root node. The following shows the equivalent +of the built-in template rule:

+ + + +]]> + +

There is also a built-in template rule for each mode, which allows +recursive processing to continue in the same mode in the absence of a +successful pattern match by an explicit template rule in the +stylesheet. This template rule applies to both element nodes and the +root node. The following shows the equivalent of the built-in +template rule for mode m.

+ +<xsl:template match="*|/" mode="m"> + <xsl:apply-templates mode="m"/> +</xsl:template> + +

There is also a built-in template rule for text and attribute nodes +that copies text through:

+ + + +]]> + +

The built-in template rule for processing instructions and comments +is to do nothing.

+ +]]> + +

The built-in template rule for namespace nodes is also to do +nothing. There is no pattern that can match a namespace node; so, the +built-in template rule is the only template rule that is applied for +namespace nodes.

+ +

The built-in template rules are treated as if they were imported +implicitly before the stylesheet and so have lower import precedence than all other +template rules. Thus, the author can override a built-in template +rule by including an explicit template rule.

+ +
+ + +
+ + +Named Templates + + + + + + + + + +

Templates can be invoked by name. An xsl:template +element with a name attribute specifies a named template. +The value of the name attribute is a QName, which is expanded as described +in . If an xsl:template element has +a name attribute, it may, but need not, also have a +match attribute. An xsl:call-template +element invokes a template by name; it has a required +name attribute that identifies the template to be +invoked. Unlike xsl:apply-templates, +xsl:call-template does not change the current node or the +current node list.

+ +

The match, mode and priority attributes on an +xsl:template element do not affect whether the template +is invoked by an xsl:call-template element. Similarly, +the name attribute on an xsl:template +element does not affect whether the template is invoked by an +xsl:apply-templates element.

+ +

It is an error if a stylesheet contains more than one template with +the same name and same import +precedence.

+ +
+ + + +Creating the Result Tree + +

This section describes instructions that directly create nodes in +the result tree.

+ + +Creating Elements and Attributes + + +Literal Result Elements + +

In a template, an element in the stylesheet that does not belong to +the XSLT namespace and that is not an extension element (see ) is instantiated to create an element node +with the same expanded-name. The content +of the element is a template, which is instantiated to give the +content of the created element node. The created element node will +have the attribute nodes that were present on the element node in the +stylesheet tree, other than attributes with names in the XSLT +namespace.

+ +

The created element node will also have a copy of the namespace +nodes that were present on the element node in the stylesheet tree +with the exception of any namespace node whose string-value is the +XSLT namespace URI (&XSLT.ns;), a +namespace URI declared as an extension namespace (see ), or a namespace URI designated as an +excluded namespace. A namespace URI is designated as an excluded +namespace by using an exclude-result-prefixes attribute +on an xsl:stylesheet element or an +xsl:exclude-result-prefixes attribute on a literal result +element. The value of both these attributes is a whitespace-separated +list of namespace prefixes. The namespace bound to each of the +prefixes is designated as an excluded namespace. It is an error if +there is no namespace bound to the prefix on the element bearing the +exclude-result-prefixes or +xsl:exclude-result-prefixes attribute. The default +namespace (as declared by xmlns) may be designated as an +excluded namespace by including #default in the list of +namespace prefixes. The designation of a namespace as an excluded +namespace is effective within the subtree of the stylesheet rooted at +the element bearing the exclude-result-prefixes or +xsl:exclude-result-prefixes attribute; +a subtree rooted at an xsl:stylesheet element +does not include any stylesheets imported or included by children +of that xsl:stylesheet element.

+ +

When a stylesheet uses a namespace declaration only for the +purposes of addressing the source tree, specifying the prefix in the +exclude-result-prefixes attribute will avoid superfluous +namespace declarations in the result tree.

+ +

The value of an attribute of a literal result element is +interpreted as an attribute +value template: it can contain expressions contained +in curly braces ({}).

+ +

A namespace URI in the stylesheet tree that is being used to +specify a namespace URI in the result tree is called a literal +namespace URI. This applies to:

+ + + +

the namespace URI in the expanded-name of a literal +result element in the stylesheet

+ +

the namespace URI in the expanded-name of an attribute +specified on a literal result element in the stylesheet

+ +

the string-value of a namespace node on a literal result +element in the stylesheet

+ +
+ + + + + + + + + + + + + + +

A stylesheet can use the +xsl:namespace-alias element to declare that one namespace +URI is an alias for another namespace URI. When +a literal namespace +URI has been declared to be an alias for another namespace +URI, then the namespace URI in the result tree will be the namespace +URI that the literal namespace URI is an alias for, instead of the +literal namespace URI itself. The xsl:namespace-alias +element declares that the namespace URI bound to the prefix specified +by the stylesheet-prefix attribute is an alias for the +namespace URI bound to the prefix specified by the +result-prefix attribute. Thus, the +stylesheet-prefix attribute specifies the namespace URI +that will appear in the stylesheet, and the +result-prefix attribute specifies the corresponding +namespace URI that will appear in the result tree. The default +namespace (as declared by xmlns) may be specified by +using #default instead of a prefix. If a namespace URI +is declared to be an alias for multiple different namespace URIs, then +the declaration with the highest import precedence is used. It is +an error if there is more than one such declaration. An XSLT +processor may signal the error; if it does not signal the error, it +must recover by choosing, from amongst the declarations with the +highest import precedence, the one that occurs last in the +stylesheet.

+ +

When literal result elements are being used to create element, +attribute, or namespace nodes that use the XSLT namespace URI, the +stylesheet must use an alias. For example, the stylesheet

+ +<xsl:stylesheet + version="1.0" + xmlns:xsl="&XSLT.ns;" + xmlns:fo="&XSLFO.ns;" + xmlns:axsl="&XSLTA.ns;"> + + + + + + + + + + + + + +]]> + +

will generate an XSLT stylesheet from a document of the form:

+ + +p +h1 +h2 +h3 +h4 +]]> + +

It may be necessary also to use aliases for namespaces other +than the XSLT namespace URI. For example, literal result elements +belonging to a namespace dealing with digital signatures might cause +XSLT stylesheets to be mishandled by general-purpose security +software; using an alias for the namespace would avoid the possibility +of such mishandling.

+ +
+ + +Creating Elements with xsl:element + + + + + + + + + + + + + + + + + + + +

The xsl:element element allows an element to be +created with a computed name. The expanded-name of the +element to be created is specified by a required name +attribute and an optional namespace attribute. The +content of the xsl:element element is a template for the +attributes and children of the created element.

+ +

The name attribute is interpreted as an attribute value template. +It is an error if the string that results from instantiating the +attribute value template is not a QName. An XSLT processor may signal +the error; if it does not signal the error, then it must recover +by making the the result of instantiating the xsl:element +element be the sequence of nodes created by instantiating +the content of the xsl:element element, excluding +any initial attribute nodes. If the namespace attribute is +not present then the QName is +expanded into an expanded-name using the namespace declarations in +effect for the xsl:element element, including any default +namespace declaration.

+ +

If the namespace attribute is present, then it also is +interpreted as an attribute +value template. The string that results from instantiating +the attribute value template should be a URI reference. It is not an +error if the string is not a syntactically legal URI reference. If +the string is empty, then the expanded-name of the element has a null +namespace URI. Otherwise, the string is used as the namespace URI of +the expanded-name of the element to be created. The local part of the +QName specified by the +name attribute is used as the local part of the +expanded-name of the element to be created.

+ +

XSLT processors may make use of the prefix of the QName specified in the +name attribute when selecting the prefix used for +outputting the created element as XML; however, they are not required +to do so.

+ +
+ + +Creating Attributes with xsl:attribute + + + + + + + + + + + + + + + + +

The xsl:attribute element can be used to add +attributes to result elements whether created by literal result +elements in the stylesheet or by instructions such as +xsl:element. The expanded-name of the +attribute to be created is specified by a required name +attribute and an optional namespace attribute. +Instantiating an xsl:attribute element adds an attribute +node to the containing result element node. The content of the +xsl:attribute element is a template for the value of the +created attribute.

+ +

The name attribute is interpreted as an attribute value template. +It is an error if the string that results from instantiating the +attribute value template is not a QName or is the string +xmlns. An XSLT processor may signal the error; if it +does not signal the error, it must recover by not adding the attribute +to the result tree. If the namespace attribute is not +present, then the QName is +expanded into an expanded-name using the namespace declarations in +effect for the xsl:attribute element, not +including any default namespace declaration.

+ +

If the namespace attribute is present, then it also is +interpreted as an attribute +value template. The string that results from instantiating +it should be a URI reference. It is not an error if the string is not +a syntactically legal URI reference. If the string is empty, then the +expanded-name of the attribute has a null namespace URI. Otherwise, +the string is used as the namespace URI of the expanded-name of the +attribute to be created. The local part of the QName specified by the +name attribute is used as the local part of the +expanded-name of the attribute to be created.

+ +

XSLT processors may make use of the prefix of the QName specified in the +name attribute when selecting the prefix used for +outputting the created attribute as XML; however, they are not +required to do so and, if the prefix is xmlns, they must +not do so. Thus, although it is not an error to do:

+ +<xsl:attribute name="xmlns:xsl" namespace="whatever">&XSLT.ns;</xsl:attribute> + +

it will not result in a namespace declaration being output.

+ +

Adding an attribute to an element replaces any existing attribute +of that element with the same expanded-name.

+ +

The following are all errors:

+ + + +

Adding an attribute to an element after children have been +added to it; implementations may either signal the error or ignore the +attribute.

+ +

Adding an attribute to a node that is not an element; +implementations may either signal the error or ignore the +attribute.

+ +

Creating nodes other than text nodes during the +instantiation of the content of the xsl:attribute +element; implementations may either signal the error or ignore the +offending nodes.

+ +
+ +

When an xsl:attribute contains a text node with +a newline, then the XML output must contain a character reference. +For example,

+ +x +y]]> + +

will result in the output

+ + + +

(or with any equivalent character reference). The XML output cannot +be

+ + + +

This is because XML 1.0 requires newline characters in attribute +values to be normalized into spaces but requires character references +to newline characters not to be normalized. The attribute values in +the data model represent the attribute value after normalization. If +a newline occurring in an attribute value in the tree were output as a +newline character rather than as character reference, then the +attribute value in the tree created by reparsing the XML would contain +a space not a newline, which would mean that the tree had not been +output correctly.

+ +
+ + + +Named Attribute Sets + + + + + + + + + + + + +

The xsl:attribute-set element defines a named set of +attributes. The name attribute specifies the name of the +attribute set. The value of the name attribute is a QName, which is expanded as described +in . The content of the xsl:attribute-set +element consists of zero or more xsl:attribute elements +that specify the attributes in the set.

+ +

Attribute sets are used by specifying a +use-attribute-sets attribute on xsl:element, +xsl:copy (see ) or +xsl:attribute-set elements. The value of the +use-attribute-sets attribute is a whitespace-separated +list of names of attribute sets. Each name is specified as a QName, which is expanded as described +in . Specifying a +use-attribute-sets attribute is equivalent to adding +xsl:attribute elements for each of the attributes in each +of the named attribute sets to the beginning of the content of the +element with the use-attribute-sets attribute, in the +same order in which the names of the attribute sets are specified in +the use-attribute-sets attribute. It is an error if use +of use-attribute-sets attributes on +xsl:attribute-set elements causes an attribute set to +directly or indirectly use itself.

+ +

Attribute sets can also be used by specifying an +xsl:use-attribute-sets attribute on a literal result +element. The value of the xsl:use-attribute-sets +attribute is a whitespace-separated list of names of attribute sets. +The xsl:use-attribute-sets attribute has the same effect +as the use-attribute-sets attribute on +xsl:element with the additional rule that attributes +specified on the literal result element itself are treated as if they +were specified by xsl:attribute elements before any +actual xsl:attribute elements but after any +xsl:attribute elements implied by the +xsl:use-attribute-sets attribute. Thus, for a literal +result element, attributes from attribute sets named in an +xsl:use-attribute-sets attribute will be added first, in +the order listed in the attribute; next, attributes specified on the +literal result element will be added; finally, any attributes +specified by xsl:attribute elements will be added. Since +adding an attribute to an element replaces any existing attribute of +that element with the same name, this means that attributes specified +in attribute sets can be overridden by attributes specified on the +literal result element itself.

+ +

The template within each xsl:attribute element in an +xsl:attribute-set element is instantiated each time the +attribute set is used; it is instantiated using the same current node +and current node list as is used for instantiating the element bearing +the use-attribute-sets or +xsl:use-attribute-sets attribute. However, it is the +position in the stylesheet of the xsl:attribute element +rather than of the element bearing the use-attribute-sets +or xsl:use-attribute-sets attribute that determines which +variable bindings are visible (see ); thus, +only variables and parameters declared by top-level xsl:variable and +xsl:param elements are visible.

+ +

The following example creates a named attribute set +title-style and uses it in a template rule.

+ + + + + + + + + 12pt + bold +]]> + +

Multiple definitions of an attribute set with the same +expanded-name are merged. An attribute from a definition that has +higher import precedence +takes precedence over an attribute from a definition that has lower +import precedence. It +is an error if there are two attribute sets that have the same +expanded-name and equal import precedence and that both contain +the same attribute, unless there is a definition of the attribute set +with higher import +precedence that also contains the attribute. An XSLT +processor may signal the error; if it does not signal the error, it +must recover by choosing from amongst the definitions that specify the +attribute that have the highest import precedence the one that was +specified last in the stylesheet. Where the attributes in an +attribute set were specified is relevant only in merging the +attributes into the attribute set; it makes no difference when the +attribute set is used.

+ +
+ +
+ + + +Creating Text + +

A template can also contain text nodes. Each text node in a +template remaining after whitespace has been stripped as specified in + will create a text node with the same +string-value in the result tree. Adjacent text nodes in the result +tree are automatically merged.

+ +

Note that text is processed at the tree level. Thus, markup of +&lt; in a template will be represented in the +stylesheet tree by a text node that includes the character +<. This will create a text node in the result tree +that contains a < character, which will be represented +by the markup &lt; (or an equivalent character +reference) when the result tree is externalized as an XML document +(unless output escaping is disabled as described in ).

+ + + + + + + + + + +

Literal data characters may also be wrapped in an +xsl:text element. This wrapping may change what +whitespace characters are stripped (see ) but +does not affect how the characters are handled by the XSLT processor +thereafter.

+ +

The xml:lang and xml:space +attributes are not treated specially by XSLT. In particular,

+ + +

it is the responsibility of the stylesheet author explicitly +to generate any xml:lang or xml:space +attributes that are needed in the result;

+ +

specifying an xml:lang or xml:space +attribute on an element in the XSLT namespace will not cause any +xml:lang or xml:space attributes to appear +in the result.

+
+
+ +
+ + + +Creating Processing Instructions + + + + + + + + + + + + +

The xsl:processing-instruction element is instantiated +to create a processing instruction node. The content of the +xsl:processing-instruction element is a template for the +string-value of the processing instruction node. The +xsl:processing-instruction element has a required +name attribute that specifies the name of the processing +instruction node. The value of the name attribute is +interpreted as an attribute +value template.

+ +

For example, this

+ +href="book.css" type="text/css"]]> + +

would create the processing instruction

+ +]]> + +

It is an error if the string that results from instantiating the +name attribute is not both an NCName and a PITarget. An XSLT processor may signal +the error; if it does not signal the error, it must recover by not +adding the processing instruction to the result tree.

+ +

This means that xsl:processing-instruction +cannot be used to output an XML declaration. The +xsl:output element should be used instead (see ).

+ +

It is an error if instantiating the content of +xsl:processing-instruction creates nodes other than +text nodes. An XSLT processor may signal the error; if it does not +signal the error, it must recover by ignoring the offending nodes +together with their content.

+ +

It is an error if the result of instantiating the content of the +xsl:processing-instruction contains the string +?>. An XSLT processor may signal the error; if it does +not signal the error, it must recover by inserting a space after any +occurrence of ? that is followed by a >.

+ +
+ + +Creating Comments + + + + + + +

The xsl:comment element is instantiated to create a +comment node in the result tree. The content of the +xsl:comment element is a template for the string-value of +the comment node.

+ +

For example, this

+ +This file is automatically generated. Do not edit!]]> + +

would create the comment

+ +]]> + +

It is an error if instantiating the content of +xsl:comment creates nodes other than text nodes. An +XSLT processor may signal the error; if it does not signal the error, +it must recover by ignoring the offending nodes together with their +content.

+ +

It is an error if the result of instantiating the content of the +xsl:comment contains the string -- or ends +with -. An XSLT processor may signal the error; if it +does not signal the error, it must recover by inserting a space after +any occurrence of - that is followed by another +- or that ends the comment.

+ +
+ + +Copying + + + + + + + + + +

The xsl:copy element provides an easy way of copying +the current node. Instantiating the xsl:copy element +creates a copy of the current node. The namespace nodes of the +current node are automatically copied as well, but the attributes and +children of the node are not automatically copied. The content of the +xsl:copy element is a template for the attributes and +children of the created node; the content is instantiated only for +nodes of types that can have attributes or children (i.e. root +nodes and element nodes).

+ +

The xsl:copy element may have a +use-attribute-sets attribute (see ). This is used only when copying element +nodes.

+ +

The root node is treated specially because the root node of the +result tree is created implicitly. When the current node is the root +node, xsl:copy will not create a root node, but will just +use the content template.

+ +

For example, the identity transformation can be written using +xsl:copy as follows:

+ + + + + +]]> + +

When the current node is an attribute, then if it would be an error +to use xsl:attribute to create an attribute with the same +name as the current node, then it is also an error to use +xsl:copy (see ).

+ +

The following example shows how xml:lang attributes +can be easily copied through from source to result. If a stylesheet +defines the following named template:

+ + + + + + +]]> + +

then it can simply do

+ +]]> + +

instead of

+ +]]> + +

when it wants to copy the xml:lang attribute.

+ +
+ + +Computing Generated Text + +

Within a template, the xsl:value-of element can be +used to compute generated text, for example by extracting text from +the source tree or by inserting the value of a variable. The +xsl:value-of element does this with an expression that is specified as the +value of the select attribute. Expressions can +also be used inside attribute values of literal result elements by +enclosing the expression in curly braces ({}).

+ + +Generating Text with xsl:value-of + + + + + + + + + + + + + +

The xsl:value-of element is instantiated to create a +text node in the result tree. The required select +attribute is an expression; +this expression is evaluated and the resulting object is converted to +a string as if by a call to the string +function. The string specifies the string-value of the created text +node. If the string is empty, no text node will be created. The +created text node will be merged with any adjacent text nodes.

+ +

The xsl:copy-of element can be used to copy a node-set +over to the result tree without converting it to a string. See .

+ +

For example, the following creates an HTML paragraph from a +person element with given-name and +family-name attributes. The paragraph will contain the value +of the given-name attribute of the current node followed +by a space and the value of the family-name attribute of the +current node.

+ + +

+ + + +

+]]>
+ +

For another example, the following creates an HTML paragraph from a +person element with given-name and +family-name children elements. The paragraph will +contain the string-value of the first given-name child +element of the current node followed by a space and the string-value +of the first family-name child element of the current +node.

+ + +

+ + + +

+]]>
+ +

The following precedes each procedure element with a +paragraph containing the security level of the procedure. It assumes +that the security level that applies to a procedure is determined by a +security attribute on the procedure element or on an +ancestor element of the procedure. It also assumes that if more than +one such element has a security attribute then the +security level is determined by the element that is closest to the +procedure.

+ + + + + + +]]> + +
+ + +Attribute Value Templates + +

In an attribute value that is interpreted as an +attribute value template, such as an attribute of a +literal result element, an expression can be used by surrounding +the expression with curly braces ({}). The +attribute value template is instantiated by replacing the expression +together with surrounding curly braces by the result of evaluating the +expression and converting the resulting object to a string as if by a +call to the string function. Curly braces are +not recognized in an attribute value in an XSLT stylesheet unless the +attribute is specifically stated to be one that is interpreted as an +attribute value template; in an element syntax summary, the value +of such attributes is surrounded by curly braces.

+ +

Not all attributes are interpreted as attribute value +templates. Attributes whose value is an expression or pattern, +attributes of top-level elements +and attributes that refer to named XSLT objects are not interpreted as +attribute value templates. In addition, xmlns attributes +are not interpreted as attribute value templates; it would not be +conformant with the XML Namespaces Recommendation to do +this.

+ +

The following example creates an img result element +from a photograph element in the source; the value of the +src attribute of the img element is computed +from the value of the image-dir variable and the +string-value of the href child of the +photograph element; the value of the width +attribute of the img element is computed from the value +of the width attribute of the size child of +the photograph element:

+ +/images + + + +]]> + +

With this source

+ + + headquarters.jpg + +]]> + +

the result would be

+ +]]> + +

When an attribute value template is instantiated, a double left or +right curly brace outside an expression will be replaced by a single +curly brace. It is an error if a right curly brace occurs in an +attribute value template outside an expression without being followed +by a second right curly brace. A right curly brace inside a Literal in an expression is not +recognized as terminating the expression.

+ +

Curly braces are not recognized recursively inside +expressions. For example:

+ +]]> + +

is not allowed. Instead, use simply:

+ +]]> + +
+ +
+ + +Numbering + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

The xsl:number element is used to insert a formatted +number into the result tree. The number to be inserted may be +specified by an expression. The value attribute contains +an expression. The expression +is evaluated and the resulting object is converted to a number as if +by a call to the number function. The number is +rounded to an integer and then converted to a string using the +attributes specified in ; in this +context, the value of each of these attributes is +interpreted as an attribute +value template. After conversion, the resulting string is +inserted in the result tree. For example, the following example +numbers a sorted list:

+ + + + +

+ + +

+
+]]>
+ +

If no value attribute is specified, then the +xsl:number element inserts a number based on the position +of the current node in the source tree. The following attributes +control how the current node is to be numbered:

+ + + +

The level attribute specifies what levels of the +source tree should be considered; it has the values +single, multiple or any. The +default is single.

+ +

The count attribute is a pattern that specifies +what nodes should be counted at those levels. If count +attribute is not specified, then it defaults to the pattern that +matches any node with the same node type as the current node and, if +the current node has an expanded-name, with the same expanded-name as +the current node.

+ +

The from attribute is a pattern that specifies +where counting starts.

+ +
+ +

In addition, the attributes specified in +are used for number to string conversion, as in the case when the +value attribute is specified.

+ +

The xsl:number element first constructs a list of +positive integers using the level, count and +from attributes:

+ + + +

When level="single", it goes up to the first +node in the ancestor-or-self axis that matches +the count pattern, and constructs a list of length one +containing one plus the number of preceding siblings of that ancestor +that match the count pattern. If there is no such +ancestor, it constructs an empty list. If the from +attribute is specified, then the only ancestors that are searched are +those that are descendants of the nearest ancestor that matches the +from pattern. Preceding siblings has the same meaning +here as with the preceding-sibling axis.

+ +

When level="multiple", it constructs a list of all +ancestors of the current node in document order followed by the +element itself; it then selects from the list those nodes that match +the count pattern; it then maps each node in the list to +one plus the number of preceding siblings of that node that match the +count pattern. If the from attribute is +specified, then the only ancestors that are searched are those that +are descendants of the nearest ancestor that matches the +from pattern. Preceding siblings has the same meaning +here as with the preceding-sibling axis.

+ +

When level="any", it constructs a list of length +one containing the number of nodes that match the count +pattern and belong to the set containing the current node and all +nodes at any level of the document that are before the current node in +document order, excluding any namespace and attribute nodes (in other +words the union of the members of the preceding and +ancestor-or-self axes). If the from +attribute is specified, then only nodes after the first node before +the current node that match the from pattern are +considered.

+ +
+ +

The list of numbers is then converted into a string using the +attributes specified in ; in this +context, the value of each of these attributes is +interpreted as an attribute +value template. After conversion, the resulting string is +inserted in the result tree.

+ +

The following would number the items in an ordered list:

+ + + + . + +]]> + +

The following two rules would number title elements. +This is intended for a document that contains a sequence of chapters +followed by a sequence of appendices, where both chapters and +appendices contain sections, which in turn contain subsections. +Chapters are numbered 1, 2, 3; appendices are numbered A, B, C; +sections in chapters are numbered 1.1, 1.2, 1.3; sections in +appendices are numbered A.1, A.2, A.3.

+ + + + + + + + + + + + + +]]> + +

The following example numbers notes sequentially within a +chapter:

+ + + + + + +]]> + +

The following example would number H4 elements in HTML +with a three-part label:

+ + + + + . + + . + + + + +]]> + + +Number to String Conversion Attributes + +

The following attributes are used to control conversion of a list +of numbers into a string. The numbers are integers greater than +0. The attributes are all optional.

+ +

The main attribute is format. The default value for +the format attribute is 1. The +format attribute is split into a sequence of tokens where +each token is a maximal sequence of alphanumeric characters or a +maximal sequence of non-alphanumeric characters. Alphanumeric means +any character that has a Unicode category of Nd, Nl, No, Lu, Ll, Lt, +Lm or Lo. The alphanumeric tokens (format tokens) specify the format +to be used for each number in the list. If the first token is a +non-alphanumeric token, then the constructed string will start with +that token; if the last token is non-alphanumeric token, then the +constructed string will end with that token. Non-alphanumeric tokens +that occur between two format tokens are separator tokens that are +used to join numbers in the list. The nth format token +will be used to format the nth number in the list. If +there are more numbers than format tokens, then the last format token +will be used to format remaining numbers. If there are no format +tokens, then a format token of 1 is used to format all +numbers. The format token specifies the string to be used to +represent the number 1. Each number after the first will be separated +from the preceding number by the separator token preceding the format +token used to format that number, or, if there are no separator +tokens, then by . (a period character).

+ +

Format tokens are a superset of the allowed values for the +type attribute for the OL element in HTML +4.0 and are interpreted as follows:

+ + + +

Any token where the last character has a decimal digit value +of 1 (as specified in the Unicode character property database), +and the Unicode value of preceding characters is one less than the +Unicode value of the last character generates a decimal +representation of the number where each number is at least as long as +the format token. Thus, a format token 1 generates the +sequence 1 2 ... 10 11 12 ..., and a format token +01 generates the sequence 01 02 ... 09 10 11 12 +... 99 100 101.

+ +

A format token A generates the sequence A +B C ... Z AA AB AC....

+ +

A format token a generates the sequence a +b c ... z aa ab ac....

+ +

A format token i generates the sequence i +ii iii iv v vi vii viii ix x ....

+ +

A format token I generates the sequence I +II III IV V VI VII VIII IX X ....

+ +

Any other format token indicates a numbering sequence that +starts with that token. If an implementation does not support a +numbering sequence that starts with that token, it must use a format +token of 1.

+ +
+ +

When numbering with an alphabetic sequence, the lang +attribute specifies which language's alphabet is to be used; it has +the same range of values as xml:lang ; +if no lang value is specified, the language should be +determined from the system environment. Implementers should document +for which languages they support numbering.

+ +

Implementers should not make any assumptions about how +numbering works in particular languages and should properly research +the languages that they wish to support. The numbering conventions of +many languages are very different from English.

+ +

The letter-value attribute disambiguates between +numbering sequences that use letters. In many languages there are two +commonly used numbering sequences that use letters. One numbering +sequence assigns numeric values to letters in alphabetic sequence, and +the other assigns numeric values to each letter in some other manner +traditional in that language. In English, these would correspond to +the numbering sequences specified by the format tokens a +and i. In some languages, the first member of each +sequence is the same, and so the format token alone would be +ambiguous. A value of alphabetic specifies the +alphabetic sequence; a value of traditional specifies the +other sequence. If the letter-value attribute is not +specified, then it is implementation-dependent how any ambiguity is +resolved.

+ +

It is possible for two conforming XSLT processors not to +convert a number to exactly the same string. Some XSLT processors may not +support some languages. Furthermore, there may be variations possible +in the way conversions are performed for any particular language that +are not specifiable by the attributes on xsl:number. +Future versions of XSLT may provide additional attributes to provide +control over these variations. Implementations may also use +implementation-specific namespaced attributes on +xsl:number for this.

+ +

The grouping-separator attribute gives the separator +used as a grouping (e.g. thousands) separator in decimal numbering +sequences, and the optional grouping-size specifies the +size (normally 3) of the grouping. For example, +grouping-separator="," and grouping-size="3" +would produce numbers of the form 1,000,000. If only one +of the grouping-separator and grouping-size +attributes is specified, then it is ignored.

+ +

Here are some examples of conversion specifications:

+ + + +

format="&#x30A2;" specifies Katakana +numbering

+ +

format="&#x30A4;" specifies Katakana +numbering in the iroha order

+ +

format="&#x0E51;" specifies numbering with +Thai digits

+ +

format="&#x05D0;" letter-value="traditional" +specifies traditional Hebrew numbering

+ +

format="&#x10D0;" letter-value="traditional" +specifies Georgian numbering

+ +

format="&#x03B1;" letter-value="traditional" +specifies classical Greek numbering

+ +

format="&#x0430;" letter-value="traditional" +specifies Old Slavic numbering

+ +
+ +
+
+
+ + + +Repetition + + + + + + + + + + + + +

When the result has a known regular structure, it is useful to be +able to specify directly the template for selected nodes. The +xsl:for-each instruction contains a template, which is +instantiated for each node selected by the expression specified by the +select attribute. The select attribute is +required. The expression must evaluate to a node-set. The template +is instantiated with the selected node as the current node, and with a list of all +of the selected nodes as the current node list. The nodes are +processed in document order, unless a sorting specification is present +(see ).

+ +

For example, given an XML document with this structure

+ + + + ... + ... + ... + + + ... + ... + ... + +]]> + +

the following would create an HTML document containing a table with +a row for each customer element

+ + + + + Customers + + + + + + + + + + + + + +
+ + + +
+ + +]]>
+ +
+ + +Conditional Processing + +

There are two instructions in XSLT that support conditional +processing in a template: xsl:if and +xsl:choose. The xsl:if instruction provides +simple if-then conditionality; the xsl:choose instruction +supports selection of one choice when there are several +possibilities.

+ + +Conditional Processing with xsl:if + + + + + + + + + +

The xsl:if element has a test attribute, +which specifies an expression. +The content is a template. The expression is evaluated and the +resulting object is converted to a boolean as if by a call to the +boolean function. If the result is true, then +the content template is instantiated; otherwise, nothing is created. +In the following example, the names in a group of names are formatted +as a comma separated list:

+ + + + , +]]> + +

The following colors every other table row yellow:

+ + + + + yellow + + + +]]> + +
+ + + +Conditional Processing with xsl:choose + + + + + + + + + + + + + + + + + + + + +

The xsl:choose element selects one among a number of +possible alternatives. It consists of a sequence of +xsl:when elements followed by an optional +xsl:otherwise element. Each xsl:when +element has a single attribute, test, which specifies an +expression. The content of the +xsl:when and xsl:otherwise elements is a +template. When an xsl:choose element is processed, each +of the xsl:when elements is tested in turn, by evaluating +the expression and converting the resulting object to a boolean as if +by a call to the boolean function. The content +of the first, and only the first, xsl:when element whose +test is true is instantiated. If no xsl:when is true, +the content of the xsl:otherwise element is +instantiated. If no xsl:when element is true, and no +xsl:otherwise element is present, nothing is created.

+ +

The following example enumerates items in an ordered list using +arabic numerals, letters, or roman numerals depending on the depth to +which the ordered lists are nested.

+ + + + + + + + + + + + + + + + + . + + + + + +]]> + +
+
+ + +Sorting + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Sorting is specified by adding xsl:sort elements as +children of an xsl:apply-templates or +xsl:for-each element. The first xsl:sort +child specifies the primary sort key, the second xsl:sort +child specifies the secondary sort key and so on. When an +xsl:apply-templates or xsl:for-each element +has one or more xsl:sort children, then instead of +processing the selected nodes in document order, it sorts the nodes +according to the specified sort keys and then processes them in sorted +order. When used in xsl:for-each, xsl:sort +elements must occur first. When a template is instantiated by +xsl:apply-templates and xsl:for-each, the +current node list list +consists of the complete list of nodes being processed in sorted +order.

+ +

xsl:sort has a select attribute whose +value is an expression. For +each node to be processed, the expression is evaluated with that node +as the current node and with the complete list of nodes being +processed in unsorted order as the current node list. +The resulting object is converted to a string as +if by a call to the string function; this string +is used as the sort key for that node. The default value of the +select attribute is ., which will cause the +string-value of the current node to be used as the sort key.

+ +

This string serves as a sort key for the node. The following +optional attributes on xsl:sort control how the list of +sort keys are sorted; the values of all of these attributes are +interpreted as attribute +value templates.

+ + + +

order specifies whether the strings should be +sorted in ascending or descending order; ascending +specifies ascending order; descending specifies +descending order; the default is ascending

+ +

lang specifies the language of the sort keys; it +has the same range of values as xml:lang ; if no lang value is specified, the language +should be determined from the system environment

+ +

data-type specifies the data type of the +strings; the following values are allowed:

+ + + +

text specifies that the sort keys should be +sorted lexicographically in the culturally correct manner for the +language specified by lang

+ +

number specifies that the sort keys should be +converted to numbers and then sorted according to the numeric value; +the sort key is converted to a number as if by a call to the +number function; the lang +attribute is ignored

+ +

a QName with a prefix +is expanded into an expanded-name as described +in ; the expanded-name identifies the data-type; +the behavior in this case is not specified by this document

+ +
+ +

The default value is text.

+ +

The XSL Working Group plans that future versions of XSLT will +leverage XML Schemas to define further values for this +attribute.

+ +
+ +

case-order has the value +upper-first or lower-first; this applies +when data-type="text", and specifies that upper-case +letters should sort before lower-case letters or vice-versa +respectively. For example, if lang="en", then A a B +b are sorted with case-order="upper-first" and +a A b B are sorted with +case-order="lower-first". The default value is language +dependent.

+ +
+ +

It is possible for two conforming XSLT processors not to sort +exactly the same. Some XSLT processors may not support some +languages. Furthermore, there may be variations possible in the +sorting of any particular language that are not specified by the +attributes on xsl:sort, for example, whether Hiragana or +Katakana is sorted first in Japanese. Future versions of XSLT may +provide additional attributes to provide control over these +variations. Implementations may also use implementation-specific +namespaced attributes on xsl:sort for this.

+ +

It is recommended that implementers consult for information on internationalized +sorting.

+ +

The sort must be stable: in the sorted list of nodes, any sub list +that has sort keys that all compare equal must be in document +order.

+ +

For example, suppose an employee database has the form

+ + + + + James + Clark + + ... + + +]]> + +

Then a list of employees sorted by name could be generated +using:

+ + +
    + + + + +
+ + + +
  • + + + +
  • +
    ]]>
    + +
    + + +Variables and Parameters + + + + + + + + + + + + + + + + + + + + + + + + +

    A variable is a name that may be bound to a value. The value to +which a variable is bound (the value of the variable) can +be an object of any of the types that can be returned by expressions. +There are two elements that can be used to bind variables: +xsl:variable and xsl:param. The difference +is that the value specified on the xsl:param variable is +only a default value for the binding; when the template or stylesheet +within which the xsl:param element occurs is invoked, +parameters may be passed that are used in place of the default +values.

    + +

    Both xsl:variable and xsl:param have a +required name attribute, which specifies the name of the +variable. The value of the name attribute is a QName, which is expanded as described +in .

    + +

    For any use of these variable-binding elements, there is a region +of the stylesheet tree within which the binding is visible; within +this region, any binding of the variable that was visible on the +variable-binding element itself is hidden. Thus, only the innermost +binding of a variable is visible. The set of variable bindings in +scope for an expression consists of those bindings that are visible at +the point in the stylesheet where the expression occurs.

    + + +Result Tree Fragments + +

    Variables introduce an additional data-type into the expression +language. This additional data type is called result tree +fragment. A variable may be bound to a result tree fragment +instead of one of the four basic XPath data-types (string, number, +boolean, node-set). A result tree fragment represents a fragment of +the result tree. A result tree fragment is treated equivalently to a +node-set that contains just a single root node. However, the +operations permitted on a result tree fragment are a subset of those +permitted on a node-set. An operation is permitted on a result tree +fragment only if that operation would be permitted on a string (the +operation on the string may involve first converting the string to a +number or boolean). In particular, it is not permitted to use the +/, //, and [] operators on +result tree fragments. When a permitted operation is performed on a +result tree fragment, it is performed exactly as it would be on the +equivalent node-set.

    + +

    When a result tree fragment is copied into the result tree (see +), then all the nodes that are children of the +root node in the equivalent node-set are added in sequence to the +result tree.

    + +

    Expressions can only return values of type result tree fragment by +referencing variables of type result tree fragment or calling +extension functions that return a result tree fragment or getting a +system property whose value is a result tree fragment.

    + +
    + + +Values of Variables and Parameters + +

    A variable-binding element can specify the value of the variable in +three alternative ways.

    + + + +

    If the variable-binding element has a select +attribute, then the value of the attribute must be an expression and the value of the variable +is the object that results from evaluating the expression. In this +case, the content must be empty.

    + + + +

    If the variable-binding element does not have a select +attribute and has non-empty content (i.e. the variable-binding element +has one or more child nodes), then the content of the +variable-binding element specifies the value. The content of the +variable-binding element is a template, which is instantiated to give +the value of the variable. The value is a result tree fragment +equivalent to a node-set containing just a single root node having as +children the sequence of nodes produced by instantiating the template. +The base URI of the nodes in the result tree fragment is the base URI +of the variable-binding element.

    + +

    It is an error if a member of the sequence of nodes created by +instantiating the template is an attribute node or a namespace node, +since a root node cannot have an attribute node or a namespace node as +a child. An XSLT processor may signal the error; if it does not signal +the error, it must recover by not adding the attribute node or +namespace node.

    + +
    + + + +

    If the variable-binding element has empty content and does not have +a select attribute, then the value of the variable is an +empty string. Thus

    + +]]> + +

    is equivalent to

    + +]]> + +
    + +
    + +

    When a variable is used to select nodes by position, be careful +not to do:

    + +2 +... +]]> + +

    This will output the value of the first item element, because the +variable n will be bound to a result tree fragment, not a +number. Instead, do either

    + + +... +]]> + +

    or

    + +2 +... +]]> +
    + +

    One convenient way to specify the empty node-set as the default +value of a parameter is:

    + +]]> +
    + +
    + + +Using Values of Variables and Parameters with +xsl:copy-of + + + + + + + + + +

    The xsl:copy-of element can be used to insert a result +tree fragment into the result tree, without first converting it to a +string as xsl:value-of does (see ). The required select attribute +contains an expression. When +the result of evaluating the expression is a result tree fragment, the +complete fragment is copied into the result tree. When the result is +a node-set, all the nodes in the set are copied in document order into +the result tree; copying an element node copies the attribute nodes, +namespace nodes and children of the element node as well as the +element node itself; a root node is copied by copying its children. +When the result is neither a node-set nor a result tree fragment, the +result is converted to a string and then inserted into the result +tree, as with xsl:value-of.

    + +
    + + +Top-level Variables and Parameters + +

    Both xsl:variable and xsl:param are +allowed as top-level elements. +A top-level variable-binding element declares a global variable that +is visible everywhere. A top-level xsl:param element +declares a parameter to the stylesheet; XSLT does not define the +mechanism by which parameters are passed to the stylesheet. It is an +error if a stylesheet contains more than one binding of a top-level +variable with the same name and same import precedence. At the +top-level, the expression or template specifying the variable value is +evaluated with the same context as that used to process the root node +of the source document: the current node is the root node of the +source document and the current node list is a list containing just +the root node of the source document. If the template or expression +specifying the value of a global variable x references a +global variable y, then the value for y must +be computed before the value of x. It is an error if it +is impossible to do this for all global variable definitions; in other +words, it is an error if the definitions are circular.

    + +

    This example declares a global variable para-font-size, +which it references in an attribute value template.

    + +12pt + + + + + + +]]> + +
    + + +Variables and Parameters within Templates + +

    As well as being allowed at the top-level, both +xsl:variable and xsl:param are also +allowed in templates. xsl:variable is allowed anywhere +within a template that an instruction is allowed. In this case, the +binding is visible for all following siblings and their descendants. +Note that the binding is not visible for the xsl:variable +element itself. xsl:param is allowed as a child +at the beginning of an xsl:template element. In this +context, the binding is visible for all following siblings and their +descendants. Note that the binding is not visible for the +xsl:param element itself.

    + +

    A binding +shadows another binding if the binding occurs at a point +where the other binding is visible, and the bindings have the same +name. It is an error if a binding established by an +xsl:variable or xsl:param element within a +template shadows another binding +established by an xsl:variable or xsl:param +element also within the template. It is not an error if a binding +established by an xsl:variable or xsl:param +element in a template shadows +another binding established by an xsl:variable or +xsl:param top-level +element. Thus, the following is an error:

    + + + + +]]> + +

    However, the following is allowed:

    + + + + +]]> + +

    The nearest equivalent in Java to an xsl:variable +element in a template is a final local variable declaration with an +initializer. For example,

    + +]]> + +

    has similar semantics to

    + +final Object x = "value"; + +

    XSLT does not provide an equivalent to the Java assignment operator

    + +x = "value"; + +

    because this would make it harder to create an implementation that +processes a document other than in a batch-like way, starting at the +beginning and continuing through to the end.

    + +
    + + +Passing Parameters to Templates + + + + + + + + + + + +

    Parameters are passed to templates using the +xsl:with-param element. The required name +attribute specifies the name of the parameter (the variable the value +of whose binding is to be replaced). The value of the +name attribute is a QName, which is expanded as described +in . xsl:with-param is allowed +within both xsl:call-template and +xsl:apply-templates. The value of the parameter is +specified in the same way as for xsl:variable and +xsl:param. The current node and current node list used +for computing the value specified by xsl:with-param +element is the same as that used for the +xsl:apply-templates or xsl:call-template +element within which it occurs. It is not an error to pass a +parameter x to a template that does not have an +xsl:param element for x; the parameter is +simply ignored.

    + +

    This example defines a named template for a +numbered-block with an argument to control the format of +the number.

    + + + 1. + + + + + + + + + a. + +]]> + +
    + +
    + + +Additional Functions + +

    This section describes XSLT-specific additions to the core XPath +function library. Some of these additional functions also make use of +information specified by top-level +elements in the stylesheet; this section also describes these +elements.

    + + + +Multiple Source Documents + + + + +

    The document function allows +access to XML documents other than the main source document.

    + +

    When the document function has exactly one +argument and the argument is a node-set, then the result is the union, +for each node in the argument node-set, of the result of calling the +document function with the first argument being +the string-value +of the node, and the second argument being a node-set with the node as +its only member. When the document function has +two arguments and the first argument is a node-set, then the result is +the union, for each node in the argument node-set, of the result of +calling the document function with the first +argument being the string-value of the node, +and with the second argument being the second argument passed to the +document function.

    + +

    When the first argument to the document +function is not a node-set, the first argument is converted to a +string as if by a call to the string function. +This string is treated as a URI reference; the resource identified by +the URI is retrieved. The data resulting from the retrieval action is +parsed as an XML document and a tree is constructed in accordance with +the data model (see ). If there is an +error retrieving the resource, then the XSLT processor may signal an +error; if it does not signal an error, it must recover by returning an +empty node-set. One possible kind of retrieval error is that the XSLT +processor does not support the URI scheme used by the URI. An XSLT +processor is not required to support any particular URI schemes. The +documentation for an XSLT processor should specify which URI schemes +the XSLT processor supports.

    + +

    If the URI reference does not contain a fragment identifier, then a +node-set containing just the root node of the document is returned. +If the URI reference does contain a fragment identifier, the function +returns a node-set containing the nodes in the tree identified by the +fragment identifier of the URI reference. The semantics of the +fragment identifier is dependent on the media type of the result of +retrieving the URI. If there is an error in processing the fragment +identifier, the XSLT processor may signal the error; if it does not +signal the error, it must recover by returning an empty node-set. +Possible errors include:

    + + + +

    The fragment identifier identifies something that cannot be +represented by an XSLT node-set (such as a range of characters within +a text node).

    + +

    The XSLT processor does not support fragment identifiers for +the media-type of the retrieval result. An XSLT processor is not +required to support any particular media types. The documentation for +an XSLT processor should specify for which media types the XSLT +processor supports fragment identifiers.

    + +
    + +

    The data resulting from the retrieval action is parsed as an XML +document regardless of the media type of the retrieval result; if the +top-level media type is text, then it is parsed in the +same way as if the media type were text/xml; otherwise, +it is parsed in the same way as if the media type were +application/xml.

    + +

    Since there is no top-level xml media type, data +with a media type other than text/xml or +application/xml may in fact be XML.

    + +

    The URI reference may be relative. The base URI (see ) of the node in the second argument node-set that is +first in document order is used as the base URI for resolving the +relative URI into an absolute URI. If the second argument is omitted, +then it defaults to the node in the stylesheet that contains the +expression that includes the call to the document +function. Note that a zero-length URI reference is a reference to the +document relative to which the URI reference is being resolved; thus +document("") refers to the root node of the stylesheet; +the tree representation of the stylesheet is exactly the same as if +the XML document containing the stylesheet was the initial source +document.

    + +

    Two documents are treated as the same document if they are +identified by the same URI. The URI used for the comparison is the +absolute URI into which any relative URI was resolved and does not +include any fragment identifier. One root node is treated as the same +node as another root node if the two nodes are from the same document. +Thus, the following expression will always be true:

    + +generate-id(document("foo.xml"))=generate-id(document("foo.xml")) + +

    The document function gives rise to the +possibility that a node-set may contain nodes from more than one +document. With such a node-set, the relative document order of two +nodes in the same document is the normal document order defined by +XPath . The relative document order of two nodes +in different documents is determined by an implementation-dependent +ordering of the documents containing the two nodes. There are no +constraints on how the implementation orders documents other than that +it must do so consistently: an implementation must always use the same +order for the same set of documents.

    + +
    + + +Keys + +

    Keys provide a way to work with documents that contain an implicit +cross-reference structure. The ID, IDREF +and IDREFS attribute types in XML provide a mechanism to +allow XML documents to make their cross-reference explicit. XSLT +supports this through the XPath id function. +However, this mechanism has a number of limitations:

    + + + +

    ID attributes must be declared as such in the DTD. If an ID +attribute is declared as an ID attribute only in the external DTD +subset, then it will be recognized as an ID attribute only if the XML +processor reads the external DTD subset. However, XML does not require +XML processors to read the external DTD, and they may well choose not +to do so, especially if the document is declared +standalone="yes".

    + +

    A document can contain only a single set of unique IDs. +There cannot be separate independent sets of unique IDs.

    + +

    The ID of an element can only be specified in an attribute; +it cannot be specified by the content of the element, or by a child +element.

    + +

    An ID is constrained to be an XML name. For example, it +cannot contain spaces.

    + +

    An element can have at most one ID.

    + +

    At most one element can have a particular ID.

    + +
    + +

    Because of these limitations XML documents sometimes contain a +cross-reference structure that is not explicitly declared by +ID/IDREF/IDREFS attributes.

    + +

    A key is a triple containing:

    + + + +

    the node which has the key

    + +

    the name of the key (an expanded-name)

    + +

    the value of the key (a string)

    + +
    + +

    A stylesheet declares a set of keys for each document using the +xsl:key element. When this set of keys contains a member +with node x, name y and value +z, we say that node x has a key with name +y and value z.

    + +

    Thus, a key is a kind of generalized ID, which is not subject to the +same limitations as an XML ID:

    + + + +

    Keys are declared in the stylesheet using +xsl:key elements.

    + +

    A key has a name as well as a value; each key name may be +thought of as distinguishing a separate, independent space of +identifiers.

    + +

    The value of a named key for an element may be specified in +any convenient place; for example, in an attribute, in a child element +or in content. An XPath expression is used to specify where to find +the value for a particular named key.

    + +

    The value of a key can be an arbitrary string; it is not +constrained to be a name.

    + +

    There can be multiple keys in a document with the same node, +same key name, but different key values.

    + +

    There can be multiple keys in a document with the same key +name, same key value, but different nodes.

    + +
    + + + + + + + + + + + + + + + +

    The xsl:key element is used to declare keys. The +name attribute specifies the name of the key. The value +of the name attribute is a QName, which is expanded as described +in . The match attribute is a Pattern; an xsl:key element gives +information about the keys of any node that matches the pattern +specified in the match attribute. The use attribute is +an expression specifying the +values of the key; the expression is evaluated once for each node that +matches the pattern. If the result is a node-set, then for each node +in the node-set, the node that matches the pattern has a key of the +specified name whose value is the string-value of the node in the +node-set; otherwise, the result is converted to a string, and the node +that matches the pattern has a key of the specified name with value +equal to that string. Thus, a node x has a key with name +y and value z if and only if there is an +xsl:key element such that:

    + + + +

    x matches the pattern specified in the +match attribute of the xsl:key element;

    + +

    the value of the name attribute of the +xsl:key element is equal to y; +and

    + +

    when the expression specified in the use +attribute of the xsl:key element is evaluated with +x as the current node and with a node list containing +just x as the current node list resulting in an object +u, then either z is equal to the result of +converting u to a string as if by a call to the +string function, or u is a +node-set and z is equal to the string-value of one or +more of the nodes in u.

    + +
    + +

    Note also that there may be more than one xsl:key +element that matches a given node; all of the matching +xsl:key elements are used, even if they do not have the +same import +precedence.

    + +

    It is an error for the value of either the use +attribute or the match attribute to contain a VariableReference.

    + + + +

    The key function does for keys what the +id function does for IDs. The first argument +specifies the name of the key. The value of the argument must be a +QName, which is expanded as +described in . When the second argument to the +key function is of type node-set, then the result +is the union of the result of applying the key +function to the string value of each of the nodes in the +argument node-set. When the second argument to +key is of any other type, the argument is +converted to a string as if by a call to the +string function; it returns a node-set +containing the nodes in the same document as the context node that +have a value for the named key equal to this string.

    + +

    For example, given a declaration

    + +]]> + +

    an expression key("idkey",@ref) will return the same +node-set as id(@ref), assuming that the only ID attribute +declared in the XML source document is:

    + +]]> + +

    and that the ref attribute of the current node +contains no whitespace.

    + +

    Suppose a document describing a function library uses a +prototype element to define functions

    + + + + +]]> + +

    and a function element to refer to function names

    + +key]]> + +

    Then the stylesheet could generate hyperlinks between the +references and definitions as follows:

    + + + + + + + + + + + + +

    +Function: +... +

    +
    ]]>
    + +

    The key can be used to retrieve a key from a +document other than the document containing the context node. For +example, suppose a document contains bibliographic references in the +form XSLT]]>, and there is a +separate XML document bib.xml containing a bibliographic +database with entries in the form:

    + +...]]> + +

    Then the stylesheet could use the following to transform the +bibref elements:

    + + + + + + + + +]]> + +
    + + +Number Formatting + + + +

    The format-number function converts its first +argument to a string using the format pattern string specified by the +second argument and the decimal-format named by the third argument, or +the default decimal-format, if there is no third argument. The format +pattern string is in the syntax specified by the JDK 1.1 DecimalFormat class. The format pattern string is in a +localized notation: the decimal-format determines what characters have +a special meaning in the pattern (with the exception of the quote +character, which is not localized). The format pattern must not +contain the currency sign (#x00A4); support for this feature was added +after the initial release of JDK 1.1. The decimal-format name must be +a QName, which is expanded as +described in . It is an error if the stylesheet +does not contain a declaration of the decimal-format with the specified +expanded-name.

    + +

    Implementations are not required to use the JDK 1.1 +implementation, nor are implementations required to be implemented in +Java.

    + +

    Stylesheets can use other facilities in XPath to control +rounding.

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

    The xsl:decimal-format element declares a +decimal-format, which controls the interpretation of a format pattern +used by the format-number function. If there is +a name attribute, then the element declares a named +decimal-format; otherwise, it declares the default decimal-format. +The value of the name attribute is a QName, which is expanded as described +in . It is an error to declare either the +default decimal-format or a decimal-format with a given name more than +once (even with different import +precedence), unless it is declared every time with the same +value for all attributes (taking into account any default values).

    + +

    The other attributes on xsl:decimal-format correspond +to the methods on the JDK 1.1 DecimalFormatSymbols class. For each +get/set method pair there is an attribute +defined for the xsl:decimal-format element.

    + +

    The following attributes both control the interpretation of +characters in the format pattern and specify characters that may +appear in the result of formatting the number:

    + + + +

    decimal-separator specifies the character used +for the decimal sign; the default value is the period character +(.)

    + +

    grouping-separator specifies the character used +as a grouping (e.g. thousands) separator; the default value is the +comma character (,)

    + +

    percent specifies the character used as a +percent sign; the default value is the percent character +(%)

    + +

    per-mille specifies the character used as a per +mille sign; the default value is the Unicode per-mille character +(#x2030)

    + +

    zero-digit specifies the character used as the +digit zero; the default value is the digit zero +(0)

    + +
    + +

    The following attributes control the interpretation of characters +in the format pattern:

    + + + +

    digit specifies the character used for a digit +in the format pattern; the default value is the number sign character +(#)

    + +

    pattern-separator specifies the character used +to separate positive and negative sub patterns in a pattern; the +default value is the semi-colon character (;)

    + +
    + +

    The following attributes specify characters or strings that may +appear in the result of formatting the number:

    + + + +

    infinity specifies the string used to represent +infinity; the default value is the string +Infinity

    + +

    NaN specifies the string used to represent the +NaN value; the default value is the string NaN

    + +

    minus-sign specifies the character used as the +default minus sign; the default value is the hyphen-minus character +(-, #x2D)

    + +
    + +
    + + +Miscellaneous Additional Functions + + + +

    The current function returns a node-set that +has the current node as its +only member. For an outermost expression (an expression not occurring +within another expression), the current node is always the same as the +context node. Thus,

    + +]]> + +

    means the same as

    + +]]> + +

    However, within square brackets the current node is usually +different from the context node. For example,

    + +]]> + +

    will process all item elements that have a +glossary parent element and that have a name +attribute with value equal to the value of the current node's +ref attribute. This is different from

    + +]]> + +

    which means the same as

    + +]]> + +

    and so would process all item elements that have a +glossary parent element and that have a name +attribute and a ref attribute with the same value.

    + +

    It is an error to use the current function in +a pattern.

    + + + +

    The unparsed-entity-uri returns the URI of the +unparsed entity with the specified name in the same document as the +context node (see ). It returns the +empty string if there is no such entity.

    + + + +

    The generate-id function returns a string that +uniquely identifies the node in the argument node-set that is first in +document order. The unique identifier must consist of ASCII +alphanumeric characters and must start with an alphabetic character. +Thus, the string is syntactically an XML name. An implementation is +free to generate an identifier in any convenient way provided that it +always generates the same identifier for the same node and that +different identifiers are always generated from different nodes. An +implementation is under no obligation to generate the same identifiers +each time a document is transformed. There is no guarantee that a +generated unique identifier will be distinct from any unique IDs +specified in the source document. If the argument node-set is empty, +the empty string is returned. If the argument is omitted, it defaults +to the context node.

    + + + +

    The argument must evaluate to a string that is a QName. The QName is expanded into a name using +the namespace declarations in scope for the expression. The +system-property function returns an object +representing the value of the system property identified by the name. +If there is no such system property, the empty string should be +returned.

    + +

    Implementations must provide the following system properties, which +are all in the XSLT namespace:

    + + + +xsl:version, a number giving the version of XSLT +implemented by the processor; for XSLT processors implementing the +version of XSLT specified by this document, this is the number +1.0 + +xsl:vendor, a string identifying the vendor of the +XSLT processor + +xsl:vendor-url, a string containing a URL +identifying the vendor of the XSLT processor; typically this is the +host page (home page) of the vendor's Web site. + + + +
    + +
    + + +Messages + + + + + + + + + + +

    The xsl:message instruction sends a message in a way +that is dependent on the XSLT processor. The content of the +xsl:message instruction is a template. The +xsl:message is instantiated by instantiating the content +to create an XML fragment. This XML fragment is the content of the +message.

    + +

    An XSLT processor might implement xsl:message by +popping up an alert box or by writing to a log file.

    + +

    If the terminate attribute has the value +yes, then the XSLT processor should terminate processing +after sending the message. The default value is no.

    + +

    One convenient way to do localization is to put the localized +information (message text, etc.) in an XML document, which becomes an +additional input file to the stylesheet. For example, suppose +messages for a language L are stored in an XML +file resources/L.xml in the form:

    + + + A problem was detected. + An error was detected. + +]]> + +

    Then a stylesheet could use the following approach to localize +messages:

    + + + + + + + + + + + + + + problem + +]]> + +
    + + +Extensions + +

    XSLT allows two kinds of extension, extension elements and +extension functions.

    + +

    This version of XSLT does not provide a mechanism for defining +implementations of extensions. Therefore, an XSLT stylesheet that must +be portable between XSLT implementations cannot rely on particular +extensions being available. XSLT provides mechanisms that allow an +XSLT stylesheet to determine whether the XSLT processor by which it is +being processed has implementations of particular extensions +available, and to specify what should happen if those extensions are +not available. If an XSLT stylesheet is careful to make use of these +mechanisms, it is possible for it to take advantage of extensions and +still work with any XSLT implementation.

    + + +Extension Elements + +

    The +element extension mechanism allows namespaces to be designated as +extension namespaces. When a namespace is designated as +an extension namespace and an element with a name from that namespace +occurs in a template, then the element is treated as an instruction +rather than as a literal result element. The namespace +determines the semantics of the instruction.

    + +

    Since an element that is a child of an +xsl:stylesheet element is not occurring in a +template, non-XSLT top-level elements are not extension +elements as defined here, and nothing in this section applies to +them.

    + +

    A namespace is designated as an extension namespace by using an +extension-element-prefixes attribute on an +xsl:stylesheet element or an +xsl:extension-element-prefixes attribute on a literal +result element or extension element. +The value of both these attributes is a +whitespace-separated list of namespace prefixes. The namespace bound +to each of the prefixes is designated as an extension namespace. It +is an error if there is no namespace bound to the prefix on the +element bearing the extension-element-prefixes or +xsl:extension-element-prefixes attribute. The default +namespace (as declared by xmlns) may be designated as an +extension namespace by including #default in the list of +namespace prefixes. The designation of a namespace as an extension +namespace is effective within the subtree of the stylesheet rooted at +the element bearing the extension-element-prefixes or +xsl:extension-element-prefixes attribute; +a subtree rooted at an xsl:stylesheet element +does not include any stylesheets imported or included by children +of that xsl:stylesheet element.

    + +

    If the XSLT processor does not have an implementation of a +particular extension element available, then the +element-available function must return false for +the name of the element. When such an extension element is +instantiated, then the XSLT processor must perform fallback for the +element as specified in . An XSLT processor +must not signal an error merely because a template contains an +extension element for which no implementation is available.

    + +

    If the XSLT processor has an implementation of a particular +extension element available, then the +element-available function must return true for +the name of the element.

    + +
    + + +Extension Functions + +

    If a FunctionName in a +FunctionCall expression is +not an NCName (i.e. if it +contains a colon), then it is treated as a call to an extension +function. The FunctionName +is expanded to a name using the namespace declarations from the +evaluation context.

    + +

    If the XSLT processor does not have an implementation of an +extension function of a particular name available, then the +function-available function must return false for +that name. If such an extension function occurs in an expression and +the extension function is actually called, the XSLT processor must +signal an error. An XSLT processor must not signal an error merely +because an expression contains an extension function for which no +implementation is available.

    + +

    If the XSLT processor has an implementation of an extension +function of a particular name available, then the +function-available function must return +true for that name. If such an extension is called, then the XSLT +processor must call the implementation passing it the function call +arguments; the result returned by the implementation is returned as +the result of the function call.

    + +
    + +
    + + +Fallback + + + + + + +

    Normally, instantiating an xsl:fallback element does +nothing. However, when an XSLT processor performs fallback for an +instruction element, if the instruction element has one or more +xsl:fallback children, then the content of each of the +xsl:fallback children must be instantiated in sequence; +otherwise, an error must be signaled. The content of an +xsl:fallback element is a template.

    + +

    The following functions can be used with the +xsl:choose and xsl:if instructions to +explicitly control how a stylesheet should behave if particular +elements or functions are not available.

    + + + +

    The argument must evaluate to a string that is a QName. The QName is expanded into an expanded-name using the +namespace declarations in scope for the expression. The +element-available function returns true if and +only if the expanded-name is the name of an instruction. If the +expanded-name has a namespace URI equal to the XSLT namespace URI, +then it refers to an element defined by XSLT. Otherwise, it refers to +an extension element. If the expanded-name has a null namespace URI, +the element-available function will return +false.

    + + + +

    The argument must evaluate to a string that is a QName. The QName is expanded into an expanded-name using the +namespace declarations in scope for the expression. The +function-available function returns true if and +only if the expanded-name is the name of a function in the function +library. If the expanded-name has a non-null namespace URI, then it +refers to an extension function; otherwise, it refers to a function +defined by XPath or XSLT.

    + +
    + + +Output + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

    An XSLT processor may output the result tree as a sequence of +bytes, although it is not required to be able to do so (see ). The xsl:output element allows +stylesheet authors to specify how they wish the result tree to be +output. If an XSLT processor outputs the result tree, it should do so +as specified by the xsl:output element; however, it is +not required to do so.

    + +

    The xsl:output element is only allowed as a top-level element.

    + +

    The method attribute on xsl:output +identifies the overall method that should be used for outputting the +result tree. The value must be a QName. If the QName does not have a prefix, then it +identifies a method specified in this document and must be one of +xml, html or text. If the QName has a prefix, then the QName is expanded into an expanded-name as described +in ; the expanded-name identifies the output +method; the behavior in this case is not specified by this +document.

    + +

    The default for the method attribute is chosen as +follows. If

    + + + +

    the root node of the result tree has an element +child,

    + +

    the expanded-name of the first element child of the root node +(i.e. the document element) of the result tree has local part +html (in any combination of upper and lower case) and a +null namespace URI, and

    + +

    any text nodes preceding the first element child of the root +node of the result tree contain only whitespace characters,

    + +
    + +

    then the default output method is html; otherwise, the +default output method is xml. The default output method +should be used if there are no xsl:output elements or if +none of the xsl:output elements specifies a value for the +method attribute.

    + +

    The other attributes on xsl:output provide parameters +for the output method. The following attributes are allowed:

    + + + +

    version specifies the version of the output +method

    + +

    indent specifies whether the XSLT processor may +add additional whitespace when outputting the result tree; the value +must be yes or no

    + +

    encoding specifies the preferred character +encoding that the XSLT processor should use to encode sequences of +characters as sequences of bytes; the value of the attribute should be +treated case-insensitively; the value must contain only characters in +the range #x21 to #x7E (i.e. printable ASCII characters); the value +should either be a charset registered with the Internet +Assigned Numbers Authority , or start with X-

    + +

    media-type specifies the media type (MIME +content type) of the data that results from outputting the result +tree; the charset parameter should not be specified +explicitly; instead, when the top-level media type is +text, a charset parameter should be added +according to the character encoding actually used by the output +method

    + +

    doctype-system specifies the system identifier +to be used in the document type declaration

    + +

    doctype-public specifies the public identifier +to be used in the document type declaration

    + +

    omit-xml-declaration specifies whether the XSLT +processor should output an XML declaration; the value must be +yes or no

    + +

    standalone specifies whether the XSLT processor +should output a standalone document declaration; the value must be +yes or no

    + +

    cdata-section-elements specifies a list of the +names of elements whose text node children should be output using +CDATA sections

    + +
    + +

    The detailed semantics of each attribute will be described +separately for each output method for which it is applicable. If the +semantics of an attribute are not described for an output method, then +it is not applicable to that output method.

    + +

    A stylesheet may contain multiple xsl:output elements +and may include or import stylesheets that also contain +xsl:output elements. All the xsl:output +elements occurring in a stylesheet are merged into a single effective +xsl:output element. For the +cdata-section-elements attribute, the effective value is +the union of the specified values. For other attributes, the +effective value is the specified value with the highest import precedence. It is an error +if there is more than one such value for an attribute. An XSLT +processor may signal the error; if it does not signal the error, if +should recover by using the value that occurs last in the stylesheet. +The values of attributes are defaulted after the +xsl:output elements have been merged; different output +methods may have different default values for an attribute.

    + + +XML Output Method + +

    The xml output method outputs the result tree as a +well-formed XML external general parsed entity. If the root node of +the result tree has a single element node child and no text node +children, then the entity should also be a well-formed XML document +entity. When the entity is referenced within a trivial XML document +wrapper like this

    + +entity-URI +]> +&e;]]> + +

    where entity-URI is a URI for the entity, +then the wrapper +document as a whole should be a well-formed XML document conforming to +the XML Namespaces Recommendation . In +addition, the output should be such that if a new tree was constructed +by parsing the wrapper as an XML document as specified in , and then removing the document element, making its +children instead be children of the root node, then the new tree would +be the same as the result tree, with the following possible +exceptions:

    + + + +

    The order of attributes in the two trees may be +different.

    + +

    The new tree may contain namespace nodes that were not +present in the result tree.

    +

    An XSLT processor may need to add +namespace declarations in the course of outputting the result tree as +XML.

    +
    + +
    + +

    If the XSLT processor generated a document type declaration because +of the doctype-system attribute, then the above +requirements apply to the entity with the generated document type +declaration removed.

    + +

    The version attribute specifies the version of XML to +be used for outputting the result tree. If the XSLT processor does +not support this version of XML, it should use a version of XML that +it does support. The version output in the XML declaration (if an XML +declaration is output) should correspond to the version of XML that +the processor used for outputting the result tree. The value of the +version attribute should match the VersionNum production of the XML +Recommendation . The default value is +1.0.

    + +

    The encoding attribute specifies the preferred +encoding to use for outputting the result tree. XSLT processors are +required to respect values of UTF-8 and +UTF-16. For other values, if the XSLT processor does not +support the specified encoding it may signal an error; if it does not +signal an error it should use UTF-8 or +UTF-16 instead. The XSLT processor must not use an +encoding whose name does not match the EncName production of the XML +Recommendation . If no encoding +attribute is specified, then the XSLT processor should use either +UTF-8 or UTF-16. It is possible that the +result tree will contain a character that cannot be represented in the +encoding that the XSLT processor is using for output. In this case, +if the character occurs in a context where XML recognizes character +references (i.e. in the value of an attribute node or text node), then +the character should be output as a character reference; otherwise +(for example if the character occurs in the name of an element) the +XSLT processor should signal an error.

    + +

    If the indent attribute has the value +yes, then the xml output method may output +whitespace in addition to the whitespace in the result tree (possibly +based on whitespace stripped from either the source document or the +stylesheet) in order to indent the result nicely; if the +indent attribute has the value no, it should +not output any additional whitespace. The default value is +no. The xml output method should use an +algorithm to output additional whitespace that ensures that the result +if whitespace were to be stripped from the output using the process +described in with the set of +whitespace-preserving elements consisting of just +xsl:text would be the same when additional whitespace is +output as when additional whitespace is not output.

    + +

    It is usually not safe to use indent="yes" with +document types that include element types with mixed content.

    + +

    The cdata-section-elements attribute contains a +whitespace-separated list of QNames. Each QName is expanded into an +expanded-name using the namespace declarations in effect on the +xsl:output element in which the QName occurs; if there is a default +namespace, it is used for QNames +that do not have a prefix. The expansion is performed before the +merging of multiple xsl:output elements into a single +effective xsl:output element. If the expanded-name of the +parent of a text node is a member of the list, then the text node +should be output as a CDATA section. For example,

    + +]]> + +

    would cause a literal result element written in the stylesheet as

    + +<foo>]]> + +

    or as

    + +<example><![CDATA[<foo>]]></example> + +

    to be output as

    + +<example><![CDATA[<foo>]]></example> + +

    If the text node contains the sequence of characters +]]>, then the currently open CDATA section should be +closed following the ]] and a new CDATA section opened +before the >. For example, a literal result element +written in the stylesheet as

    + +<example>]]&gt;</example> + +

    would be output as

    + +<example><![CDATA[]]]]><![CDATA[>]]></example> + +

    If the text node contains a character that is not representable in +the character encoding being used to output the result tree, then the +currently open CDATA section should be closed before the character, +the character should be output using a character reference or entity +reference, and a new CDATA section should be opened for any further +characters in the text node.

    + +

    CDATA sections should not be used except for text nodes that the +cdata-section-elements attribute explicitly specifies +should be output using CDATA sections.

    + +

    The xml output method should output an XML declaration +unless the omit-xml-declaration attribute has the value +yes. The XML declaration should include both version +information and an encoding declaration. If the +standalone attribute is specified, it should include a +standalone document declaration with the same value as the value as +the value of the standalone attribute. Otherwise, it +should not include a standalone document declaration; this ensures +that it is both a XML declaration (allowed at the beginning of a +document entity) and a text declaration (allowed at the beginning of +an external general parsed entity).

    + +

    If the doctype-system attribute is specified, the +xml output method should output a document type +declaration immediately before the first element. The name following +<!DOCTYPE should be the name of the first element. If +doctype-public attribute is also specified, then the +xml output method should output PUBLIC +followed by the public identifier and then the system identifier; +otherwise, it should output SYSTEM followed by the system +identifier. The internal subset should be empty. The +doctype-public attribute should be ignored unless the +doctype-system attribute is specified.

    + +

    The media-type attribute is applicable for the +xml output method. The default value for the +media-type attribute is text/xml.

    + +
    + + +HTML Output Method + +

    The html output method outputs the result tree as +HTML; for example,

    + +<xsl:stylesheet version="1.0" + xmlns:xsl="&XSLT.ns;"> + + + + + + + +... + +]]> + +

    The version attribute indicates the version of the +HTML. The default value is 4.0, which specifies that the +result should be output as HTML conforming to the HTML 4.0 +Recommendation .

    + +

    The html output method should not output an element +differently from the xml output method unless the +expanded-name of the element has a null namespace URI; an element +whose expanded-name has a non-null namespace URI should be output as +XML. If the expanded-name of the element has a null namespace URI, +but the local part of the expanded-name is not recognized as the name +of an HTML element, the element should output in the same way as a +non-empty, inline element such as span.

    + +

    The html output method should not output an end-tag +for empty elements. For HTML 4.0, the empty elements are +area, base, basefont, +br, col, frame, +hr, img, input, +isindex, link, meta and +param. For example, an element written as +<br/> or <br></br> in the +stylesheet should be output as <br>.

    + +

    The html output method should recognize the names of +HTML elements regardless of case. For example, elements named +br, BR or Br should all be +recognized as the HTML br element and output without an +end-tag.

    + +

    The html output method should not perform escaping for +the content of the script and style +elements. For example, a literal result element written in the +stylesheet as

    + +if (a < b) foo()]]> + +

    or

    + +]]> + +

    should be output as

    + +if (a < b) foo()]]> + +

    The html output method should not escape +< characters occurring in attribute values.

    + +

    If the indent attribute has the value +yes, then the html output method may add or +remove whitespace as it outputs the result tree, so long as it does +not change how an HTML user agent would render the output. The +default value is yes.

    + +

    The html output method should escape non-ASCII +characters in URI attribute values using the method recommended in +Section +B.2.1 of the HTML 4.0 Recommendation.

    + +

    The html output method may output a character using a +character entity reference, if one is defined for it in the version of +HTML that the output method is using.

    + +

    The html output method should terminate processing +instructions with > rather than +?>.

    + +

    The html output method should output boolean +attributes (that is attributes with only a single allowed value that +is equal to the name of the attribute) in minimized form. For example, +a start-tag written in the stylesheet as

    + +]]> + +

    should be output as

    + +]]> + +

    The html output method should not escape a +& character occurring in an attribute value +immediately followed by a { character (see Section +B.7.1 of the HTML 4.0 Recommendation). For example, a start-tag +written in the stylesheet as

    + +]]> + +

    should be output as

    + +]]> + +

    The encoding attribute specifies the preferred +encoding to be used. If there is a HEAD element, then the +html output method should add a META element +immediately after the start-tag of the HEAD element +specifying the character encoding actually used. For example,

    + + + +...]]> + +

    It is possible that the result tree will contain a character that +cannot be represented in the encoding that the XSLT processor is using +for output. In this case, if the character occurs in a context where +HTML recognizes character references, then the character should be +output as a character entity reference or decimal numeric character +reference; otherwise (for example, in a +script or style element or in a comment), +the XSLT processor should signal an error.

    + +

    If the doctype-public or doctype-system +attributes are specified, then the html output method +should output a document type declaration immediately before the first +element. The name following <!DOCTYPE should be +HTML or html. If the +doctype-public attribute is specified, then the output +method should output PUBLIC followed by the specified +public identifier; if the doctype-system attribute is +also specified, it should also output the specified system identifier +following the public identifier. If the doctype-system +attribute is specified but the doctype-public attribute +is not specified, then the output method should output +SYSTEM followed by the specified system identifier.

    + +

    The media-type attribute is applicable for the +html output method. The default value is +text/html.

    + +
    + + +Text Output Method + +

    The text output method outputs the result tree by +outputting the string-value of every text node in the result tree in +document order without any escaping.

    + +

    The media-type attribute is applicable for the +text output method. The default value for the +media-type attribute is text/plain.

    + +

    The encoding attribute identifies the encoding that +the text output method should use to convert sequences of +characters to sequences of bytes. The default is system-dependent. If +the result tree contains a character that cannot be represented in the +encoding that the XSLT processor is using for output, the XSLT +processor should signal an error.

    + +
    + + +Disabling Output Escaping + +

    Normally, the xml output method escapes & and < +(and possibly other characters) when outputting text nodes. This +ensures that the output is well-formed XML. However, it is sometimes +convenient to be able to produce output that is almost, but not quite +well-formed XML; for example, the output may include ill-formed +sections which are intended to be transformed into well-formed XML by +a subsequent non-XML aware process. For this reason, XSLT provides a +mechanism for disabling output escaping. An xsl:value-of +or xsl:text element may have a +disable-output-escaping attribute; the allowed values are +yes or no; the default is no; +if the value is yes, then a text node generated by +instantiating the xsl:value-of or xsl:text +element should be output without any escaping. For example,

    + +<]]> + +

    should generate the single character <.

    + +

    It is an error for output escaping to be disabled for a text node +that is used for something other than a text node in the result tree. +Thus, it is an error to disable output escaping for an +xsl:value-of or xsl:text element that is +used to generate the string-value of a comment, processing instruction +or attribute node; it is also an error to convert a result tree fragment to a +number or a string if the result tree fragment contains a text node for +which escaping was disabled. In both cases, an XSLT processor may +signal the error; if it does not signal the error, it must recover by +ignoring the disable-output-escaping attribute.

    + +

    The disable-output-escaping attribute may be used with +the html output method as well as with the +xml output method. The text output method +ignores the disable-output-escaping attribute, since it +does not perform any output escaping.

    + +

    An XSLT processor will only be able to disable output escaping if +it controls how the result tree is output. This may not always be the +case. For example, the result tree may be used as the source tree for +another XSLT transformation instead of being output. An XSLT +processor is not required to support disabling output escaping. If an +xsl:value-of or xsl:text specifies that +output escaping should be disabled and the XSLT processor does not +support this, the XSLT processor may signal an error; if it does not +signal an error, it must recover by not disabling output escaping.

    + +

    If output escaping is disabled for a character that is not +representable in the encoding that the XSLT processor is using for +output, then the XSLT processor may signal an error; if it does not +signal an error, it must recover by not disabling output escaping.

    + +

    Since disabling output escaping may not work with all XSLT +processors and can result in XML that is not well-formed, it should be +used only when there is no alternative.

    + + +
    + +
    + + +Conformance + +

    A conforming XSLT processor must be able to use a stylesheet to +transform a source tree into a result tree as specified in this +document. A conforming XSLT processor need not be able to output the +result in XML or in any other form.

    + +

    Vendors of XSLT processors are strongly encouraged to provide +a way to verify that their processor is behaving conformingly by +allowing the result tree to be output as XML or by providing access to +the result tree through a standard API such as the DOM or +SAX.

    + +

    A conforming XSLT processor must signal any errors except for those +that this document specifically allows an XSLT processor not to +signal. A conforming XSLT processor may but need not recover from any +errors that it signals.

    + +

    A conforming XSLT processor may impose limits on the processing +resources consumed by the processing of a stylesheet.

    + +
    + + +Notation + +

    The specification of each XSLT-defined element type is preceded by +a summary of its syntax in the form of a model for elements of that +element type. The meaning of syntax summary notation is as +follows:

    + + + +

    An attribute is required if and only if its name is in +bold.

    + +

    The string that occurs in the place of an attribute value +specifies the allowed values of the attribute. If this is surrounded +by curly braces, then the attribute value is treated as an attribute value template, +and the string occurring within curly braces specifies the allowed +values of the result of instantiating the attribute value template. +Alternative allowed values are separated by |. A quoted +string indicates a value equal to that specific string. An unquoted, +italicized name specifies a particular type of value.

    + +

    If the element is allowed not to be empty, then the element +contains a comment specifying the allowed content. The allowed +content is specified in a similar way to an element type declaration +in XML; template means that any mixture of text nodes, +literal result elements, extension elements, and XSLT elements from +the instruction category is allowed; +top-level-elements means that any mixture of XSLT +elements from the top-level-element category is +allowed.

    + +

    The element is prefaced by comments indicating if it belongs +to the instruction category or +top-level-element category or both. The category of an +element just affects whether it is allowed in the content of elements +that allow a template or +top-level-elements.

    + +
    + +
    + + + + + +References + +Normative References + + + +World Wide Web Consortium. Extensible +Markup Language (XML) 1.0. W3C Recommendation. See http://www.w3.org/TR/1998/REC-xml-19980210 + +World Wide Web +Consortium. Namespaces in XML. W3C Recommendation. See +http://www.w3.org/TR/REC-xml-names + +World Wide Web Consortium. XML Path +Language. W3C Recommendation. See http://www.w3.org/TR/xpath + + + + +Other References + + + +World Wide Web Consortium. Cascading +Style Sheets, level 2 (CSS2). W3C Recommendation. See http://www.w3.org/TR/1998/REC-CSS2-19980512 + +International Organization +for Standardization, International Electrotechnical Commission. +ISO/IEC 10179:1996. Document Style Semantics and Specification +Language (DSSSL). International Standard. + +World Wide Web Consortium. HTML 4.0 +specification. W3C Recommendation. See http://www.w3.org/TR/REC-html40 + +Internet Assigned Numbers +Authority. Character Sets. See ftp://ftp.isi.edu/in-notes/iana/assignments/character-sets. + +N. Freed, J. Postel. IANA +Charset Registration Procedures. IETF RFC 2278. See http://www.ietf.org/rfc/rfc2278.txt. + +E. Whitehead, M. Murata. XML +Media Types. IETF RFC 2376. See http://www.ietf.org/rfc/rfc2376.txt. + +T. Berners-Lee, R. Fielding, and +L. Masinter. Uniform Resource Identifiers (URI): Generic +Syntax. IETF RFC 2396. See http://www.ietf.org/rfc/rfc2396.txt. + +Unicode Consortium. +Unicode Technical Report #10. Unicode Collation +Algorithm. Unicode Technical Report. See http://www.unicode.org/unicode/reports/tr10/index.html. + +World Wide Web Consortium. XHTML +1.0: The Extensible HyperText Markup Language. W3C Proposed +Recommendation. See http://www.w3.org/TR/xhtml1 + +World Wide Web +Consortium. XML Pointer Language (XPointer). W3C Working +Draft. See http://www.w3.org/TR/xptr + +World Wide Web +Consortium. Associating stylesheets with XML documents. +W3C Recommendation. See http://www.w3.org/TR/xml-stylesheet + +World Wide Web Consortium. Extensible +Stylesheet Language (XSL). W3C Working Draft. See http://www.w3.org/TR/WD-xsl + + + + + + + +Element Syntax Summary + + + + + + +DTD Fragment for XSLT Stylesheets + +

    This DTD Fragment is not normative because XML 1.0 DTDs do +not support XML Namespaces and thus cannot correctly describe the +allowed structure of an XSLT stylesheet.

    + +

    The following entity can be used to construct a DTD for XSLT +stylesheets that create instances of a particular result DTD. Before +referencing the entity, the stylesheet DTD must define a +result-elements parameter entity listing the allowed +result element types. For example:

    + +]]> + +

    Such result elements should be declared to have +xsl:use-attribute-sets and +xsl:extension-element-prefixes attributes. The following +entity declares the result-element-atts parameter for +this purpose. The content that XSLT allows for result elements is the +same as it allows for the XSLT elements that are declared in the +following entity with a content model of %template;. The +DTD may use a more restrictive content model than +%template; to reflect the constraints of the result +DTD.

    + +

    The DTD may define the non-xsl-top-level parameter +entity to allow additional top-level elements from namespaces other +than the XSLT namespace.

    + +

    The use of the xsl: prefix in this DTD does not imply +that XSLT stylesheets are required to use this prefix. Any of the +elements declared in this DTD may have attributes whose name starts +with xmlns: or is equal to xmlns in addition +to the attributes declared in this DTD.

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +&XSLT.ns; + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +]]> + +
    + + +Examples + + +Document Example + +

    This example is a stylesheet for transforming documents that +conform to a simple DTD into XHTML . The DTD +is:

    + + + + + + + +]]> + +

    The stylesheet is:

    + +<xsl:stylesheet version="1.0" + xmlns:xsl="&XSLT.ns;" + xmlns="&XHTML.ns;"> + + + + + + + <xsl:value-of select="title"/> + + + + + + + + + +

    + +

    +
    + + +

    + +

    +
    + + +

    + +

    +
    + + +

    + +

    +
    + + +

    + NOTE: + +

    +
    + + + + + + + +]]>
    + +

    With the following input document

    + + + +Document Title + +Chapter Title +
    +Section Title +This is a test. +This is a note. +
    +
    +Another Section Title +This is another test. +This is another note. +
    +
    +
    ]]>
    + +

    it would produce the following result

    + +<?xml version="1.0" encoding="iso-8859-1"?> +<html xmlns="&XHTML.ns;"> +Document Title + + +

    Document Title

    +

    Chapter Title

    +

    Section Title

    +

    This is a test.

    +

    +NOTE: This is a note.

    +

    Another Section Title

    +

    This is another test.

    +

    +NOTE: This is another note.

    + +]]>
    + +
    + + +Data Example + +

    This is an example of transforming some data represented in XML +using three different XSLT stylesheets to produce three different +representations of the data, HTML, SVG and VRML.

    + +

    The input data is:

    + + + + + 10 + 9 + 7 + + + + 4 + 3 + 4 + + + + 6 + -1.5 + 2 + + +]]> + +

    The following stylesheet, which uses the simplified syntax +described in , transforms +the data into HTML:

    + +<html xsl:version="1.0" + xmlns:xsl="&XSLT.ns;" + + Sales Results By Division + + + + + + + + + + + + + + + + + + + +
    DivisionRevenueGrowthBonus
    + + + + + + + + color:red + + + + + +
    + +]]>
    + +

    The HTML output is:

    + + + + +Sales Results By Division + + + + + + + + + + + + + + + +
    DivisionRevenueGrowthBonus
    North1097
    West6-1.52
    South434
    + +]]>
    + +

    The following stylesheet transforms the data into SVG:

    + +<xsl:stylesheet version="1.0" + xmlns:xsl="&XSLT.ns;" + + + + + + + + + + + Revenue + Division + + + + + + + + + + + + + + + + + + + + + + + + + + +]]> + +

    The SVG output is:

    + + + + + + Revenue + Division + + North + 10 + + South + 4 + + West + 6 + +]]> + +

    The following stylesheet transforms the data into VRML:

    + +<xsl:stylesheet version="1.0" + xmlns:xsl="&XSLT.ns;"> + + + #VRML V2.0 utf8 + +# externproto definition of a single bar element +EXTERNPROTO bar [ + field SFInt32 x + field SFInt32 y + field SFInt32 z + field SFString name + ] + "http://www.vrml.org/WorkingGroups/dbwork/barProto.wrl" + +# inline containing the graph axes +Inline { + url "http://www.vrml.org/WorkingGroups/dbwork/barAxes.wrl" + } + + +bar { + x + y + z + name "" + } + + + + +]]> + +

    The VRML output is:

    + + + +
    + +
    + + +Acknowledgements +

    The following have contributed to authoring this draft:

    + +Daniel Lipkin, Saba +Jonathan Marsh, Microsoft +Henry Thompson, University of Edinburgh +Norman Walsh, Arbortext +Steve Zilles, Adobe + + +

    This specification was developed and approved for publication by the +W3C XSL Working Group (WG). WG approval of this specification does not +necessarily imply that all WG members voted for its approval. The +current members of the XSL WG are:

    + + + +Sharon Adler +IBM +Co-Chair + + +Anders Berglund +IBM + + +Perin Blanchard +Novell + + +Scott Boag +Lotus + + +Larry Cable +Sun + + +Jeff Caruso +Bitstream + + +James Clark + + +Peter Danielsen +Bell Labs + + +Don Day +IBM + + +Stephen Deach +Adobe + + +Dwayne Dicks +SoftQuad + + +Andrew Greene +Bitstream + + +Paul Grosso +Arbortext + + +Eduardo Gutentag +Sun + + +Juliane Harbarth +Software AG + + +Mickey Kimchi +Enigma + + +Chris Lilley +W3C + + +Chris Maden +Exemplary Technologies + + +Jonathan Marsh +Microsoft + + +Alex Milowski +Lexica + + +Steve Muench +Oracle + + +Scott Parnell +Xerox + + +Vincent Quint +W3C + + +Dan Rapp +Novell + + +Gregg Reynolds +Datalogics + + +Jonathan Robie +Software AG + + +Mark Scardina +Oracle + + +Henry Thompson +University of Edinburgh + + +Philip Wadler +Bell Labs + + +Norman Walsh +Arbortext + + +Sanjiva Weerawarana +IBM + + +Steve Zilles +Adobe +Co-Chair + + + +
    + + +Changes from Proposed Recommendation + +

    The following are the changes since the Proposed Recommendation:

    + + + +

    The xsl:version attribute is required on a +literal result element used as a stylesheet (see ).

    + +

    The data-type attribute on xsl:sort +can use a prefixed name to specify a data-type not defined by +XSLT (see ).

    + +
    + +
    + + +Features under Consideration for Future Versions of XSLT + +

    The following features are under consideration for versions of XSLT +after XSLT 1.0:

    + + + +

    a conditional expression;

    + +

    support for XML Schema datatypes and archetypes;

    + +

    support for something like style rules in the original XSL +submission;

    + +

    an attribute to control the default namespace for names +occurring in XSLT attributes;

    + +

    support for entity references;

    + +

    support for DTDs in the data model;

    + +

    support for notations in the data model;

    + +

    a way to get back from an element to the elements that +reference it (e.g. by IDREF attributes);

    + +

    an easier way to get an ID or key in another document;

    + +

    support for regular expressions for matching against any or +all of text nodes, attribute values, attribute names, element type +names;

    + +

    case-insensitive comparisons;

    + +

    normalization of strings before comparison, for example for +compatibility characters;

    + +

    a function string resolve(node-set) function +that treats the value of the argument as a relative URI and turns it +into an absolute URI using the base URI of the node;

    + +

    multiple result documents;

    + +

    defaulting the select attribute on +xsl:value-of to the current node;

    + +

    an attribute on xsl:attribute to control how the +attribute value is normalized;

    + +

    additional attributes on xsl:sort to provide +further control over sorting, such as relative order of +scripts;

    + +

    a way to put the text of a resource identified by a URI into +the result tree;

    + +

    allow unions in steps (e.g. foo/(bar|baz));

    + +

    allow for result tree fragments all operations that are +allowed for node-sets;

    + +

    a way to group together consecutive nodes having duplicate +subelements or attributes;

    + +

    features to make handling of the HTML style +attribute more convenient.

    + +
    + +
    + +
    +
    ADDED tests/data/books.xml Index: tests/data/books.xml ================================================================== --- /dev/null +++ tests/data/books.xml @@ -0,0 +1,62 @@ + + + + + + Seven Years in Trenton + + Joe + Bob + Trenton Literary Review Honorable Mention + + 12 + + + History of Trenton + + Mary + Bob + + Selected Short Stories of + Mary Bob + + + 55 + + + Tracking Trenton + 2.50 + + + + Trenton Today, Trenton Tomorrow + + Toni + Bob + B.A. + Ph.D. + Pulizer + Still in Trenton + Trenton Forever + + 6.50 + +

    It was a dark and stormy night.

    +

    But then all nights in Trenton seem dark and + stormy to someone who has gone through what + I have.

    + + Trenton + misery + +
    +
    + + Who's Who in Trenton + Robert Bob + +
    + + + + ADDED tests/data/i18n_1.xml Index: tests/data/i18n_1.xml ================================================================== --- /dev/null +++ tests/data/i18n_1.xml @@ -0,0 +1,1 @@ +абвгдежзий ADDED tests/data/xslt_1.xsl Index: tests/data/xslt_1.xsl ================================================================== --- /dev/null +++ tests/data/xslt_1.xsl @@ -0,0 +1,9 @@ + + + + + + + ADDED tests/decls.test Index: tests/decls.test ================================================================== --- /dev/null +++ tests/decls.test @@ -0,0 +1,383 @@ +# Features covered: Declarations +# +# This file contains a collection of tests for the TclXML parser. +# This file tests the parser's performance on markup declarations. +# Sourcing this file into Tcl runs the tests and generates output +# for errors. No output means no errors were found. +# +# Copyright (c) 2000 Zveno Pty Ltd. +# +# $Id$ + +if {[lsearch [namespace children] ::tcltest] == -1} { + source [file join [pwd] [file dirname [info script]] defs.tcl] +} + +if {[lsearch $auto_path [file dirname [file dirname [info script]]]] == -1} { + set auto_path [linsert $auto_path 0 [file dirname [file dirname [file join [pwd] [info script]]]]] +} + +if {[catch {package require expat 2.0}]} { + catch {puts stderr "Cannot load expat 2.0 package"} + return +} + +catch {unset elements} +proc elementDecl {name cmodel} { + set ::elements($name) $cmodel +} +catch {unset attrs} +proc attlistDecl {name attName type dfltValue isrequired} { + lappend ::attrs($name/$attName) $type $isrequired $dfltValue +} +catch {unset entities} +proc entityDecl {name is_param value base systemId publicId notationName} { + set ::entities($name) [list $is_param $value $base $systemId $publicId $notationName] +} +catch {unset cdata} +proc CData data { + append ::cdata [string trim $data] +} + +proc extRefH {base systemId publicId} { + if {![regexp {^[a-zA-Z]+:/} $systemId]} { + regsub {^[a-zA-Z]+:} $base {} base + set basedir [file dirname $base] + set systemId "[set basedir]/[set systemId]" + } else { + regsub {^[a-zA-Z]+:} $systemId systemId + } + if {[catch {set fd [open $systemId]}]} { + return -code error \ + -errorinfo "Failed to open external entity $systemId" + } + return [list channel $systemId $fd] +} + +proc extRefHstr {base systemId publicId} { + if {![regexp {^[a-zA-Z]+:/} $systemId]} { + regsub {^[a-zA-Z]+:} $base {} base + set basedir [file dirname $base] + set systemId "[set basedir]/[set systemId]" + } else { + regsub {^[a-zA-Z]+:} $systemId systemId + } + if {[catch {set fd [open $systemId]}]} { + return -code error \ + -errorinfo "Failed to open external entity $systemId" + } + return [list string $systemId [read $fd [file size $systemId]]] +} + + +# Internal DTD subset + +test decls-1.1 {element declaration} { + catch {unset ::elements} + array set ::elements {} + + catch {rename xml::decls-1.1 {}} + set parser [xml::parser decls-1.1 \ + -elementdeclcommand elementDecl] + $parser parse { + +]> + +} + + array get ::elements +} {Test {MIXED {} {} {}}} + +test decls-2.1 {attribute list declaration, implied} { + catch {unset ::attrs} + array set ::attrs {} + + catch {rename xml::decls-2.1 {}} + set parser [xml::parser decls-2.1 \ + -attlistdeclcommand attlistDecl] + $parser parse { + + +]> +} + + array get ::attrs +} {Test/test {CDATA 0 {}}} + +test decls-2.2 {attribute list declaration, enum} { + catch {unset ::attrs} + array set ::attrs {} + + catch {rename xml::decls-2.2 {}} + set parser [xml::parser decls-2.2 \ + -attlistdeclcommand attlistDecl] + $parser parse { + + +]> +} + + array get ::attrs +} {Test/test {(LGL|OTH) 0 LGL}} + +test decls-3.1 {entity declarations} { + catch {unset ::entities} + array set ::entities {} + + catch {rename xml::decls-3.1 {}} + set parser [xml::parser decls-3.1 \ + -entitydeclcommand entityDecl] + $parser parse { + + +]> +} + + array get ::entities +} {testEnt {0 {replacement text} {} {} {} {}}} + +test decls-4.1 {parameter entity declarations} { + catch {unset ::entities} + array set ::entities {} + catch {unset ::elements} + array set ::elements {} + + catch {rename xml::decls-4.1 {}} + set parser [xml::parser decls-4.1 \ + -paramentityparsing notstandalone \ + -elementdeclcommand elementDecl] + $parser parse { + +%PEnt; +]> +} + + array get ::elements +} {Test {MIXED {} {} {}}} + +# NB. entity.test tests entity replacement as well + +# External entities + +test decls-5.1 {external entity} { + catch {unset ::elements} + array set ::elements {} + + catch {rename xml::decls-5.1 {}} + set parser [xml::parser decls-5.1 \ + -paramentityparsing notstandalone \ + -externalentitycommand extRefH \ + -elementdeclcommand elementDecl \ + -baseurl file://[file join [pwd] decls.test]] + $parser parse { + +} + + array get ::elements +} {Test {MIXED {} {} {}}} + +test decls-5.2 {external DTD subset} { + catch {unset ::elements} + array set ::elements {} + catch {unset ::entities} + array set ::entities {} + + catch {rename xml::decls-5.2 {}} + set parser [xml::parser decls-5.2 \ + -paramentityparsing notstandalone \ + -externalentitycommand extRefH \ + -elementdeclcommand elementDecl \ + -baseurl file://[file join [pwd] decls.test]] + $parser parse { + +} + + array get ::elements +} {Test {MIXED {} {} {}}} + +test decls-5.3 {external entity} { + catch {unset ::elements} + array set ::elements {} + catch {unset ::entities} + array set ::entities {} + catch {unset ::externals} + array set ::externals {} + + catch {rename xml::decls-5.3 {}} + set parser [xml::parser decls-5.3 \ + -paramentityparsing notstandalone \ + -externalentitycommand extRefH \ + -elementdeclcommand elementDecl \ + -baseurl file://[file join [pwd] decls.test]] + $parser parse { + +%module; +]> +} + + array get ::elements +} {Test {MIXED {} {} {}}} + + +test decls-5.4 {external entity} { + catch {unset ::elements} + array set ::elements {} + + catch {rename xml::decls-5.4 {}} + set parser [xml::parser decls-5.4 \ + -paramentityparsing notstandalone \ + -externalentitycommand extRefHstr \ + -elementdeclcommand elementDecl \ + -baseurl file://[file join [pwd] decls.test]] + $parser parse { + +} + + array get ::elements +} {Test {MIXED {} {} {}}} + +test decls-5.5 {external DTD subset} { + catch {unset ::elements} + array set ::elements {} + catch {unset ::entities} + array set ::entities {} + + catch {rename xml::decls-5.5 {}} + set parser [xml::parser decls-5.5 \ + -paramentityparsing notstandalone \ + -externalentitycommand extRefHstr \ + -elementdeclcommand elementDecl \ + -baseurl file://[file join [pwd] decls.test]] + $parser parse { + +} + + array get ::elements +} {Test {MIXED {} {} {}}} + + +test decls-5.6 {external entity} { + catch {unset ::elements} + array set ::elements {} + catch {unset ::entities} + array set ::entities {} + catch {unset ::externals} + array set ::externals {} + + catch {rename xml::decls-5.6 {}} + set parser [xml::parser decls-5.6 \ + -paramentityparsing notstandalone \ + -externalentitycommand extRefHstr \ + -elementdeclcommand elementDecl \ + -baseurl file://[file join [pwd] decls.test]] + $parser parse { + +%module; +]> +} + + array get ::elements +} {Test {MIXED {} {} {}}} + + +# Conditional Sections + +test decls-6.1 {conditional section: include} { + catch {unset ::elements} + array set ::elements {} + + catch {rename xml::decls-6.1 {}} + set parser [xml::parser decls-6.1 \ + -paramentityparsing notstandalone \ + -externalentitycommand extRefH \ + -elementdeclcommand elementDecl \ + -baseurl file://[file join [pwd] decls.test]] + $parser parse { + +} + + array get ::elements +} {Test {MIXED {} {} {}}} + +test decls-6.2 {conditional section: include, empty} { + catch {unset ::elements} + array set ::elements {} + + catch {rename xml::decls-6.2 {}} + set parser [xml::parser decls-6.2 \ + -paramentityparsing notstandalone \ + -externalentitycommand extRefH \ + -elementdeclcommand elementDecl \ + -baseurl file://[file join [pwd] decls.test]] + $parser parse { + +} + + array get ::elements +} {} + +test decls-6.3 {conditional section: include, empty} { + catch {unset ::elements} + array set ::elements {} + + catch {rename xml::decls-6.3 {}} + set parser [xml::parser decls-6.3 \ + -paramentityparsing notstandalone \ + -externalentitycommand extRefH \ + -elementdeclcommand elementDecl \ + -baseurl file://[file join [pwd] decls.test]] + $parser parse { + +} + + array get ::elements +} {} + +test decls-6.4 {conditional section: include, nested} { + catch {unset ::elements} + array set ::elements {} + + catch {rename xml::decls-6.4 {}} + set parser [xml::parser decls-6.4 \ + -paramentityparsing notstandalone \ + -externalentitycommand extRefH \ + -elementdeclcommand elementDecl \ + -baseurl file://[file join [pwd] decls.test]] + $parser parse { + +} + + array size ::elements +} 3 + +test decls-6.6 {conditional section/PE combo} { + catch {unset ::elements} + array set ::elements {} + + catch {rename xml::decls-6.6 {}} + set parser [xml::parser decls-6.6 \ + -paramentityparsing notstandalone \ + -externalentitycommand extRefH \ + -elementdeclcommand elementDecl \ + -baseurl file://[file join [pwd] decls.test]] + $parser parse { + +} + + array size ::elements +} 2 + +# Comments + +# cleanup +::tcltest::cleanupTests +return ADDED tests/doctype.test Index: tests/doctype.test ================================================================== --- /dev/null +++ tests/doctype.test @@ -0,0 +1,59 @@ +# Features covered: Document Type Declaration +# +# This file contains a collection of tests for the TclXML parser. +# This file tests the parser's performance on Document Type Declarations. +# Sourcing this file into Tcl runs the tests and generates output +# for errors. No output means no errors were found. +# +# Copyright (c) 1998-2000 Zveno Pty Ltd. +# +# $Id$ + +if {[lsearch [namespace children] ::tcltest] == -1} { + source [file join [pwd] [file dirname [info script]] defs.tcl] +} + +if {[lsearch $auto_path [file dirname [file dirname [info script]]]] == -1} { + set auto_path [linsert $auto_path 0 [file dirname [file dirname [file join [pwd] [info script]]]]] +} + +if {[catch {package require expat 2.0}]} { + catch {puts stderr "Cannot load expat 2.0 package"} + return +} + +catch {unset result} +proc doctype {name pub system dtd} { + lappend ::result $name $pub $system $dtd +} + +test doctype-1.1 {Document Type Declaration: no internal DTD subset} { + set ::result {} + + catch {rename xml::doctype-1.1 {}} + set parser [xml::parser doctype-1.1 \ + -startdoctypedeclcommand doctype] + $parser parse { + + +} + set ::result +} {Test NULL NULL 0} + +test doctype-2.1 {Document Type Declaration: internal DTD subset} { + set ::result {} + + catch {rename xml::doctype-2.1 {}} + set parser [xml::parser doctype-2.1 \ + -startdoctypedeclcommand doctype] + $parser parse { +]> + +} + set ::result +} {Test NULL NULL 1} + + +# cleanup +::tcltest::cleanupTests +return ADDED tests/dom.test Index: tests/dom.test ================================================================== --- /dev/null +++ tests/dom.test @@ -0,0 +1,123 @@ +# Features covered: DOM functions +# +# This file contains a collection of tests for the DOM methods of +# tDOM. +# Tested commands and object commands: +# dom: +# dom-1.*: createDocument, createDocumentNS +# dom-2.*: parse +# +# Copyright (c) 2002 Rolf Ade. + +if {[lsearch [namespace children] ::tcltest] == -1} { + package require tcltest + namespace import -force ::tcltest::* +} + +if {[catch {package require tdom 0.7}]} { + puts stderr "Cannot load tDOM 0.7 package" + return +} + +test dom-1.1 {createDocument with root node name not a XML Name} { + list [catch {dom createDocument "root node"} msg] $msg +} "1 {invalid root element name}" + +test dom-1.2 {createDocument with root node name not a XML Name} { + list [catch {dom createDocument "1root"} msg] $msg +} "1 {invalid root element name}" + +test dom-1.3 {createDocument - root name us-ascii} { + dom createDocument "root" doc + set root [$doc documentElement] + set result [$root nodeName] + $doc delete + set result +} "root" + +test dom-1.4 {createDocument - root name with UTF-8 chars} { + dom createDocument "\u00c4\u00d4\u00dc" doc + set root [$doc documentElement] + set result [$root nodeName] + $doc delete + set result +} "\u00c4\u00d4\u00dc" + +test dom-1.5 {createDocument with FQ root name} { + dom createDocument "foo:bar" doc + set root [$doc documentElement] + set result [$root nodeName] + $doc delete + set result +} "foo:bar" + +test dom-1.6 {createDocument with wrong # of args} { + list [catch {dom createDocument "root" "http:/foo:bar" doc} msg] $msg +} "1 {wrong \# args: should be \"createDocument docElemName ?newObjVar?\"}" + +test dom-1.7 {createDocumentNS - check root name} { + set doc [dom createDocumentNS root "http://foo.bar"] + set root [$doc documentElement] + set result [$root nodeName] + $doc delete + set result +} "root" + +test dom-1.8 {createDocumentNS - check the NS of the created root} { + dom createDocumentNS root "http://foo.bar" doc + set root [$doc documentElement] + set result [$root namespaceURI] + $doc delete + set result +} "http://foo.bar" + +test dom-1.9 {createDocumentNS with root name not a NCName} { + list [catch {dom createDocumentNS "foo bar" "http://foo.bar" doc} msg] $msg +} "1 {invalid local name}" + +test dom-1.10 {createDocumentNS with root name not a NCName} { + list [catch {dom createDocumentNS "a:b:c" "http://foo.bar" doc} msg] $msg +} "1 {invalid local name}" + +test dom-1.11 {createDocumentNS with root name not a NCName} { + list [catch {dom createDocumentNS "a b:b" "http://foo.bar" doc} msg] $msg +} "1 {invalid prefix name}" + +test dom-1.12 {createDocumentNS with root name not a NCName} { + list [catch {dom createDocumentNS "a:a b" "http://foo.bar" doc} msg] $msg +} "1 {invalid local name}" + +test dom-1.13 {createDocumentNS - check root name} { + set doc [dom createDocumentNS foo:root "http://foo.bar"] + set root [$doc documentElement] + set result [$root nodeName] + $doc delete + set result +} "foo:root" + +test dom-2.1 {knownBug} {Don't quash white space at start or end of non white space content} { + set doc [dom parse { + some content + }] + set root [$doc documentElement] + $root text +} { + some content + } + +test dom-3.1 {knownBug} {repetitived createDocument with objVar, then delete} { + dom createDocument "root" doc + $doc documentElement root + $doc delete + + dom createDocument "\u00c4\u00d4\u00dc" doc + $doc documentElement root + set result [$root nodeName] + $doc delete + set result +} "\u00c4\u00d4\u00dc" + + +# cleanup +::tcltest::cleanupTests +return ADDED tests/domnamespace.test Index: tests/domnamespace.test ================================================================== --- /dev/null +++ tests/domnamespace.test @@ -0,0 +1,15 @@ + +test domnamespace-1.1 {multiple definition of the same namespace (same prefix/uri)} { + set ch [open data/REC-xslt-19991116.xml] + fconfigure $ch -encoding iso8859-1 + set doc [dom parse -channel $ch] + set root [$doc documentElement] + set nodes [$root selectNodes //e:element-syntax] + $doc delete + llength $nodes +} {35} + + +# cleanup +::tcltest::cleanupTests +return ADDED tests/element.test Index: tests/element.test ================================================================== --- /dev/null +++ tests/element.test @@ -0,0 +1,90 @@ +# Features covered: Elements +# +# This file contains a collection of tests for the TclXML parser. +# This file tests the parser's performance on elements. +# Sourcing this file into Tcl runs the tests and generates output +# for errors. No output means no errors were found. +# +# Copyright (c) 1999-2000 Zveno Pty Ltd. +# +# $Id$ + +if {[lsearch [namespace children] ::tcltest] == -1} { + source [file join [pwd] [file dirname [info script]] defs.tcl] +} + +if {[lsearch $auto_path [file dirname [file dirname [info script]]]] == -1} { + set auto_path [linsert $auto_path 0 [file dirname [file dirname [file join [pwd] [info script]]]]] +} + +if {[catch {package require expat 2.0}]} { + catch {puts stderr "Cannot load expat 2.0 package"} + return +} + +proc parray arrayName { + upvar #0 $arrayName arr + foreach key [lsort [array names $arrayName]] { + lappend result $key $arr($key) + } + return $result +} + +catch {unset started} +proc Start {name atList args} { + array set opts $args + if {![info exists ::started($name)]} { + set ::started($name) 1 + } else { + incr ::started($name) + } +} +catch {unset ended} +proc End {name args} { + array set opts $args + if {![info exists ::ended($name)]} { + set ::ended($name) 1 + } else { + incr ::ended($name) + } +} + +catch {unset elList} +proc ElStart {name atList args} { + array set opts {-empty 0} + array set opts $args + lappend ::elList start $name $opts(-empty) +} +proc ElEnd {name args} { + array set opts {-empty 0} + array set opts $args + lappend ::elList end $name $opts(-empty) +} + +test element-1.1 {document element} { + catch {unset ::started} + catch {unset ::ended} + + catch {rename xml::element-1.1 {}} + set parser [xml::parser element-1.1 \ + -elementstartcommand Start \ + -elementendcommand End] + $parser parse {} + list [array get ::started] [array get ::ended] +} {{Test 1} {Test 1}} + +test element-1.2 {distinct elements} { + catch {unset ::started} + catch {unset ::ended} + + catch {rename xml::element-1.2 {}} + set parser [xml::parser element-1.2 \ + -elementstartcommand Start \ + -elementendcommand End] + $parser parse {} + list [parray ::started] [parray ::ended] +} {{Child1 1 Child2 1 Test 1} {Child1 1 Child2 1 Test 1}} + +# cleanup +::tcltest::cleanupTests +return ADDED tests/entity.test Index: tests/entity.test ================================================================== --- /dev/null +++ tests/entity.test @@ -0,0 +1,94 @@ +# Features covered: Entities +# +# This file contains a collection of tests for the TclXML parser. +# This file tests the parser's performance on entities. +# Sourcing this file into Tcl runs the tests and generates output +# for errors. No output means no errors were found. +# +# Copyright (c) 1999-2000 Zveno Pty Ltd. +# +# $Id$ + +if {[lsearch [namespace children] ::tcltest] == -1} { + source [file join [pwd] [file dirname [info script]] defs.tcl] +} + +if {[lsearch $auto_path [file dirname [file dirname [info script]]]] == -1} { + set auto_path [linsert $auto_path 0 [file dirname [file dirname [file join [pwd] [info script]]]]] +} + +if {[catch {package require expat 2.0}]} { + catch {puts stderr "Cannot load expat 2.0 package"} + return +} + +proc Start {name attrList args} { + incr ::elements +} + +proc pcdata text { + append ::result $text +} + +proc EntityRef name { + lappend ::references $name + append ::result ##entityreference## + return {} +} + +test entity-1.1 {parameter entity in document entity} { + set ::result {} + + catch {rename xml::entity-1.1 {}} + set parser [xml::parser entity-1.1 \ + -characterdatacommand pcdata] + $parser parse { +]> +%wrong;} + set ::result +} {%wrong;} + +test entity-1.2 {character entities in hex} { + set ::result {} + + catch {rename xml::entity-1.2 {}} + set parser [xml::parser entity-1.2 \ + -characterdatacommand pcdata] + $parser parse {A<>$[]} + set ::result +} {A<>$[]} + +test entity-1.3 {character entities in decimal} { + set ::result {} + + catch {rename xml::entity-1.3 {}} + set parser [xml::parser entity-1.3 \ + -characterdatacommand pcdata] + $parser parse {A<>$[]} + set ::result +} {A<>$[]} + +test entity-1.4 {illegal character entity} { + set ::result {} + + catch {rename xml::entity-1.4 {}} + set parser [xml::parser entity-1.4 \ + -characterdatacommand pcdata] + set err [catch {$parser parse {&#blah;}}] + list $err $::result +} {1 {}} + +test entity-2.1 {predefined general entities} { + set ::result {} + + catch {rename xml::entity-2.1 {}} + set parser [xml::parser entity-2.1 \ + -characterdatacommand pcdata] + $parser parse {<>&"'} + set ::result +} {<>&"'} + +# cleanup +::tcltest::cleanupTests +return ADDED tests/i18n.test Index: tests/i18n.test ================================================================== --- /dev/null +++ tests/i18n.test @@ -0,0 +1,35 @@ + + +test i18n-1.1 {parse utf-8 string} { + set russian "\u0430\u0431\u0432\u0433\u0434\u0435\u0436\u0437\u0438\u0439" + set doc [dom parse "$russian"] + set root [$doc documentElement $doc] + set text [$root text] + $doc delete + string compare $text "\u0430\u0431\u0432\u0433\u0434\u0435\u0436\u0437\u0438\u0439" +} {0} + +test i18n-1.2 {parse utf-8 channel } { + set fd [open data/i18n_1.xml] + fconfigure $fd -encoding utf-8 + set doc [dom parse -channel $fd] + close $fd + set root [$doc documentElement $doc] + set text [$root text] + $doc delete + string compare $text "\u0430\u0431\u0432\u0433\u0434\u0435\u0436\u0437\u0438\u0439" +} {0} + +test i18n-1.3 {parse utf-8 readFile} { + set doc [dom parse [::tDOM::xmlReadFile data/i18n_1.xml]] + set root [$doc documentElement $doc] + set text [$root text] + $doc delete + string compare $text "\u0430\u0431\u0432\u0433\u0434\u0435\u0436\u0437\u0438\u0439" +} {0} + + +# cleanup +::tcltest::cleanupTests +return + ADDED tests/namespace.test Index: tests/namespace.test ================================================================== --- /dev/null +++ tests/namespace.test @@ -0,0 +1,144 @@ +# Features covered: XML Namespaces +# +# This file contains a collection of tests for the TclXML parser. +# This file tests the parser's performance on XML namespaces. +# Sourcing this file into Tcl runs the tests and generates output +# for errors. No output means no errors were found. +# +# Copyright (c) 2000 Zveno Pty Ltd. +# +# $Id$ + +if {[lsearch [namespace children] ::tcltest] == -1} { + source [file join [pwd] [file dirname [info script]] defs.tcl] +} + +if {[lsearch $auto_path [file dirname [file dirname [info script]]]] == -1} { + set auto_path [linsert $auto_path 0 [file dirname [file dirname [file join [pwd] [info script]]]]] +} + +if {[catch {package require expat 2.0}]} { + catch {puts stderr "Cannot load expat 2.0 package"} + return +} + +proc keysort args { + array set keyvalue $args + set result {} + foreach key [lsort [array names keyvalue]] { + lappend result $key $keyvalue($key) + } + return $result +} + +catch {unset result} +catch {unset nsdecls} +proc EStart {tag attlist args} { + global result nsdecls + + array set extra $args + + catch {eval lappend nsdecls $extra(-namespacedecls)} + + if {[info exists extra(-namespace)]} { + lappend result $extra(-namespace)^$tag + } else { + lappend result $tag + } +} + +proc NSDeclStart {prefix uri} { + global result + + lappend result $prefix + lappend result $uri +} + +test ns-1.1 {Namespace declaration} { + set ::result {} + set ::nsdecls {} + + catch {rename xml::ns-1.1 {}} + set parser [xml::parser ns-1.1 \ + -namespace \ + -startnamespacedeclcommand NSDeclStart \ + -elementstartcommand EStart] + $parser parse { + +} + list $::result $::nsdecls +} {{test http://www.zveno.com/Schemas Test} {}} + +test ns-1.2 {Multiple namespace declarations} { + set ::result {} + set ::nsdecls {} + + catch {rename xml::ns-1.2 {}} + set parser [xml::parser ns-1.2 \ + -namespace \ + -startnamespacedeclcommand NSDeclStart \ + -elementstartcommand EStart] + $parser parse { + +} + list $::result [eval keysort $::nsdecls] +} {{test http://www.zveno.com/Schemas x urn:schema Test} {}} + +test ns-1.3 {Default namespace declaration} { + set ::result {} + set ::nsdecls {} + + catch {rename xml::ns-1.3 {}} + set parser [xml::parser ns-1.3 \ + -namespace \ + -startnamespacedeclcommand NSDeclStart \ + -elementstartcommand EStart] + $parser parse { + +} + list $::result [eval keysort $::nsdecls] +} {{{} http://www.zveno.com/Schemas x urn:schema http://www.zveno.com/Schemas:Test} {}} + +test ns-1.4 {Default namespace declaration w/- separate usage} { + set ::result {} + set ::nsdecls {} + + catch {rename xml::ns-1.4 {}} + set parser [xml::parser ns-1.4 \ + -namespace \ + -startnamespacedeclcommand NSDeclStart \ + -elementstartcommand EStart] + $parser parse { + +} + list $::result [eval keysort $::nsdecls] +} {{{} http://www.zveno.com/Schemas x urn:schema urn:schema:Test http://www.zveno.com/Schemas:Test} {}} + +test ns-2.0 {Multiple namespace declarations, same prefix} { + set ::result {} + set ::nsdecls {} + + catch {rename xml::ns-2.0 {}} + set parser [xml::parser ns-2.0 \ + -namespace \ + -startnamespacedeclcommand NSDeclStart \ + -elementstartcommand EStart] + $parser parse { + + + + + + + + +} + list $::result [eval keysort $::nsdecls] +} {{Test x http://www.zveno.com/Schemas http://www.zveno.com/Schemas:Test http://www.zveno.com/Schemas:y x urn:schema urn:schema:Test urn:schema:z} {}} + +# cleanup +::tcltest::cleanupTests +return ADDED tests/parser.test Index: tests/parser.test ================================================================== --- /dev/null +++ tests/parser.test @@ -0,0 +1,271 @@ +# Features covered: Parser functions +# +# This file contains a collection of tests for the TclXML parser. +# This file tests the parser's basic functions. +# Sourcing this file into Tcl runs the tests and generates output +# for errors. No output means no errors were found. +# +# Copyright (c) 1999-2000 Zveno Pty Ltd. +# +# $Id$ + +if {[lsearch [namespace children] ::tcltest] == -1} { + source [file join [pwd] [file dirname [info script]] defs.tcl] +} + +if {[lsearch $auto_path [file dirname [file dirname [info script]]]] == -1} { + set auto_path [linsert $auto_path 0 [file dirname [file dirname [file join [pwd] [info script]]]]] +} + +if {[catch {package require expat 2.0}]} { + catch {puts stderr "Cannot load expat 2.0 package"} + return +} + +proc parray arrayName { + upvar #0 $arrayName arr + foreach key [lsort [array names $arrayName]] { + lappend result $key $arr($key) + } + return $result +} + +catch {unset count} +proc Count {args} { + if {![info exists ::count]} { + set ::count 1 + } else { + incr ::count + } +} + +catch {unset started} +proc Start {name atList args} { + array set opts $args + array set atts $atList + + if {![info exists ::started($name)]} { + set ::started($name) 1 + } else { + incr ::started($name) + } + if {[info exists atts(class)]} { + switch $atts(class) { + continue { + return -code continue + } + break { + return -code break + } + error { + return -code error "error condition in callback" + } + default { + return -code $atts(class) + } + } + } +} +catch {unset ended} +proc End {name args} { + array set opts $args + if {![info exists ::ended($name)]} { + set ::ended($name) 1 + } else { + incr ::ended($name) + } +} +proc PI {name args} { + return -code $name +} + +catch {unset elList} +proc ElStart {name atList args} { + array set opts {-empty 0} + array set opts $args + lappend ::elList start $name $opts(-empty) +} +proc ElEnd {name args} { + array set opts {-empty 0} + array set opts $args + lappend ::elList end $name $opts(-empty) +} + +test parser-1.1 {parser creation} { + set p [::xml::parser] + regexp {^xmlparser[0-9]+$} $p +} 1 + +test parser-1.2 {parser creation, only options} { + set p [::xml::parser -elementstartcommand Start] + regexp {^xmlparser[0-9]+$} $p +} 1 + +test parser-1.3 {parser creation, named} { + catch {rename testparser {}} + ::xml::parser testparser +} testparser + +test parser-1.4 {parser creation, named with options} { + catch {rename testparser {}} + ::xml::parser testparser -elementstartcommand Start +} testparser + +# Test break return code from callback + +test parser-2.1 {break in callback} { + catch {unset ::started} + + catch {rename parser-2.1 {}} + set p [::xml::parser parser-2.1 -elementstartcommand Start] + $p parse { + +Should see this data +Should not see this data +Should not see this data + +} + set ::started(Element) +} 2 + +test parser-2.2 {break in callback} { + catch {unset ::started} + + catch {rename parser-2.2 {}} + set p [::xml::parser parser-2.2 -elementstartcommand Start] + $p parse { + +Should see this data +Should see this data +Should not see this data + +} + set ::started(Element) +} 3 + +test parser-2.3 {break in callback} { + catch {unset ::started} + + catch {rename parser-2.3 {}} + set p [::xml::parser parser-2.3 -elementstartcommand Start] + $p parse { + +Should see this data +Should see this data + + +Should not see this data + +} + set ::started(Element) +} 3 + +test parser-3.1 {continue in callback} { + catch {unset ::started} + + catch {rename parser-3.1 {}} + set p [::xml::parser parser-3.1 -elementstartcommand Start] + $p parse { + +Should see this data +Should not see this data + + +Should see this data + +} + set ::started(Element) +} 3 + +test parser-3.2 {continue in callback} { + catch {unset ::started} + + catch {rename parser-3.2 {}} + set p [::xml::parser parser-3.2 -elementstartcommand Start] + $p parse { + +Should see this data +Should see this data + + Should not see this data + + + Should see this data + + +Should see this data + +} + set ::started(Element) +} 5 + +test parser-3.3 {continue in callback} { + catch {unset ::started} + + catch {rename parser-3.3 {}} + set p [::xml::parser parser-3.3 -elementstartcommand Start] + $p parse { + +Should see this data +Should see this data + + Should not see this data + + break will have no effect + + Should see this data + + +Should see this data + +} + set ::started(Element) +} 5 + +test parser-4.1 {error in callback} { + catch {unset ::started} + + catch {rename parser-4.1 {}} + set p [::xml::parser parser-4.1 -elementstartcommand Start] + set errcode [catch {$p parse { + +Should see this data + +Should not see this data + +}} result] + list $errcode $::started(Element) +} {1 2} + +test parser-4.2 {error in callback} { + catch {unset ::started} + + catch {rename parser-4.2 {}} + set p [::xml::parser parser-4.2 -elementstartcommand Start] + set errcode [catch {$p parse { + +Should see this data + +Should not see this data + +}} result] + list $::errcode $::started(Element) +} {13 2} + +test parser-5.1 {parse channel input} { + catch {unset ::count} + + catch {rename parser-5.1 {}} + set parser [::xml::parser parser-5.1 -elementstartcommand Count] + set fd [open data/books.xml] + $parser parsechannel $fd + close $fd + list $::count +} {42} + +# cleanup +::tcltest::cleanupTests +return + + + ADDED tests/pcdata.test Index: tests/pcdata.test ================================================================== --- /dev/null +++ tests/pcdata.test @@ -0,0 +1,97 @@ +# Features covered: PCDATA +# +# This file contains a collection of tests for the TclXML parser. +# This file tests the parser's performance on PCDATA. +# Sourcing this file into Tcl runs the tests and generates output +# for errors. No output means no errors were found. +# +# Copyright (c) 1998-2000 Zveno Pty Ltd. +# +# $Id$ + +if {[lsearch [namespace children] ::tcltest] == -1} { + source [file join [pwd] [file dirname [info script]] defs.tcl] +} + +if {[lsearch $auto_path [file dirname [file dirname [info script]]]] == -1} { + set auto_path [linsert $auto_path 0 [file dirname [file dirname [file join [pwd] [info script]]]]] +} + +if {[catch {package require expat 2.0}]} { + catch {puts stderr "Cannot load expat 2.0 package"} + return +} + +catch {unset result} +proc pcdata data { + append ::result $data + incr ::pcdataCounter +} +proc Estart {tagName attrList} { + switch -- $tagName { + Test { + } + default { + incr ::element + } + } +} + +proc EStop tagname { +} + +test pcdata-1.1 {Simple PCDATA} { + set ::result {} + set ::element 0 + set ::pcdataCounter 0 + + catch {rename xml::pcdata-1.1 {}} + set parser [xml::parser pcdata-1.1 \ + -elementstartcommand Estart \ + -elementendcommand EStop \ + -characterdatacommand pcdata] + $parser parse { + +This is PCDATA +} + list $::result $::element +} {{This is PCDATA} 0} + +test pcdata-1.2 {PCDATA section with Tcl specials} { + set ::result {} + set ::element 0 + set ::pcdataCounter 0 + + catch {rename xml::pcdata-1.2 {}} + set parser [xml::parser pcdata-1.2 \ + -elementstartcommand Estart \ + -elementendcommand EStop \ + -characterdatacommand pcdata] + $parser parse { + +Dollar $ backslash \ square brackets [ ] braces { } +} + list $::result $::element +} {{Dollar $ backslash \ square brackets [ ] braces { }} 0} + +# Requested by Marshall Rose, 20/3/1999 +test pcdata-1.3 {PCDATA with no entity expansion} { + set ::result {} + set ::element 0 + set ::pcdataCounter 0 + + catch {rename xml::pcdata-1.3 {}} + set parser [xml::parser pcdata-1.3 \ + -elementstartcommand Estart \ + -elementendcommand EStop \ + -characterdatacommand pcdata] + $parser parse { + +This is <PCDATA> +} + list $::result $::pcdataCounter +} {{This is } 4} + +# cleanup +::tcltest::cleanupTests +return ADDED tests/pi.test Index: tests/pi.test ================================================================== --- /dev/null +++ tests/pi.test @@ -0,0 +1,72 @@ +# Features covered: Processing Instructions +# +# This file contains a collection of tests for the TclXML parser. +# This file tests the parser's performance on Processing Instructions. +# Sourcing this file into Tcl runs the tests and generates output +# for errors. No output means no errors were found. +# +# Copyright (c) 1998-2000 Zveno Pty Ltd. +# +# $Id$ + +if {[lsearch [namespace children] ::tcltest] == -1} { + source [file join [pwd] [file dirname [info script]] defs.tcl] +} + +if {[lsearch $auto_path [file dirname [file dirname [info script]]]] == -1} { + set auto_path [linsert $auto_path 0 [file dirname [file dirname [file join [pwd] [info script]]]]] +} + +if {[catch {package require expat 2.0}]} { + catch {puts stderr "Cannot load expat 2.0 package"} + return +} + +catch {unset result} +proc PI {target data args} { + lappend ::result $target $data +} + +test pi-1.1 {PI} { + set ::result {} + + catch {rename xml::pi-1.1 {}} + set parser [xml::parser pi-1.1 \ + -processinginstructioncommand PI] + $parser parse { + + +} + set ::result +} {Test {This is a processing instruction}} + +test pi-1.2 {PI: missing trailing ?} { + set ::result {} + + catch {rename xml::pi-1.2 {}} + set parser [xml::parser pi-1.2 \ + -processinginstructioncommand PI] + set returncode [catch {$parser parse { + + +}} msg] + + list $returncode [regexp {error "unclosed token" at.+} $msg] +} {1 1} + +test pi-2.1 {PI with special characters} { + set ::result {} + + catch {rename xml::pi-2.1 {}} + set parser [xml::parser pi-2.1 \ + -processinginstructioncommand PI] + $parser parse { + + +} + set ::result +} {Test {[if !VMLRender]}} + +# cleanup +::tcltest::cleanupTests +return ADDED tests/xmltest.test Index: tests/xmltest.test ================================================================== --- /dev/null +++ tests/xmltest.test @@ -0,0 +1,64 @@ +# Features covered: Conformance +# +# This file contains a collection of tests for the TclXML parser. +# This file tests the parser's performance on conformance to the +# XML specification. As such it is crude and does not test callback +# features. +# +# Sourcing this file into Tcl runs the tests and generates output +# for errors. No output means no errors were found. +# +# Copyright (c) 1999-2000 Zveno Pty Ltd. +# +# $Id$ + +if {[lsearch [namespace children] ::tcltest] == -1} { + source [file join [pwd] [file dirname [info script]] defs.tcl] +} + +if {[lsearch $auto_path [file dirname [file dirname [info script]]]] == -1} { + set auto_path [linsert $auto_path 0 [file dirname [file dirname [file join [pwd] [info script]]]]] +} + +if {[catch {package require expat 2.0}]} { + catch {puts stderr "Cannot load expat 2.0 package"} + return +} + +proc xmltest {expect dir subdir f} { + + set ch [open $f] + set data [read $ch] + close $ch + + test xmltest-$dir-$subdir-[file rootname [file tail $f]] "Document $dir $subdir [file tail $f]" { + set parser [xml::parser xmltest] + set code [catch {$parser parse $data}] + rename $parser {} + set code + } $expect +} + +# NB. Uses James Clark's test suite for WFF checking. +# Need a framework to test against each file: it's too time- +# consuming to setup a test proc for each one. + +set testDir [file join [pwd] xmltest] + +# These documents should fail +foreach dir {not-wf} { + foreach file [glob -nocomplain [file join $testDir $dir sa *.xml]] { + xmltest 1 $dir sa $file + } +} + +# These documents should pass +foreach dir {invalid valid} { + foreach file [glob -nocomplain [file join $testDir $dir sa *.xml]] { + xmltest 0 $dir sa $file + } +} + +# cleanup +::tcltest::cleanupTests +return ADDED tests/xpath.test Index: tests/xpath.test ================================================================== --- /dev/null +++ tests/xpath.test @@ -0,0 +1,12 @@ + +test xpath-1.1 {function normalize-space} { + set doc [dom createDocument foo] + set root [$doc documentElement] + set r [$root selectNodes {normalize-space('f ')}] + $doc delete + string length $r +} {1} + +# cleanup +::tcltest::cleanupTests +return ADDED tests/xslt.test Index: tests/xslt.test ================================================================== --- /dev/null +++ tests/xslt.test @@ -0,0 +1,59 @@ + + +test xslt-1.1 {unicode chars outside of US-ASCII in var name} { + set xml [dom parse {}] + set xslt [dom parse [tDOM::xmlReadFile data/xslt_1.xsl]] + set xmlroot [$xml documentElement] + $xmlroot xslt $xslt resultDoc + set resultroot [$resultDoc documentElement] + set result [$resultroot asXML] + $xml delete + $xslt delete + $resultDoc delete + set result +} {foo} + + +test xslt-1.2 {xpath parse real number} { + set xml [dom parse {}] + set xslt [dom parse { + + + +}] + set xmlroot [$xml documentElement] + $xmlroot xslt $xslt resultDoc + set resultroot [$resultDoc documentElement] + set result [$resultroot asXML] + $xml delete + $xslt delete + $resultDoc delete + set result +} {0.12345} + +test xslt-1.2 {xpath parse real number} { + set xml [dom parse {}] + set xslt [dom parse { + + + +}] + set xmlroot [$xml documentElement] + $xmlroot xslt $xslt resultDoc + set resultroot [$resultDoc documentElement] + set result [$resultroot asXML] + $xml delete + $xslt delete + $resultDoc delete + set result +} {-0.12345} + + + +# cleanup +::tcltest::cleanupTests +return Index: unix/configure ================================================================== --- unix/configure +++ unix/configure @@ -540,11 +540,11 @@ # in library names (Windows). The VERSION variable is used on the # other systems. #-------------------------------------------------------------------- MAJOR_VERSION=0 -MINOR_VERSION=6 +MINOR_VERSION=7 PATCHLEVEL=0 VERSION=${MAJOR_VERSION}.${MINOR_VERSION} PACKAGE=tdom ADDED win/config.h Index: win/config.h ================================================================== --- /dev/null +++ win/config.h ADDED win/makefile.vc Index: win/makefile.vc ================================================================== --- /dev/null +++ win/makefile.vc @@ -0,0 +1,225 @@ +#---------------------------------------------------------------------------- +# This is derivated from the tcl8.3 win makefile and surely not +# perfect. It works for me. +# rolf ade, 2001 (rolf@pointsman.de) +# +# +# +# Project directories +# +# ROOT = top of source tree +# +# TOOLS32 = location of VC++ 32-bit development tools. +# +# INSTALLDIR = location of the Tcl installation +# +#---------------------------------------------------------------------------- + +!if "$(MSVCDIR)" == "" +MSG = ^ +You'll need to run vcvars32.bat from Developer Studio, first, to setup^ +the environment. +!error $(MSG) +!endif + + +# Set this to the appropriate value of /MACHINE: for your platform +MACHINE = IX86 +ROOT = .. +INSTALLDIR = c:\Progra~1\Tcl + +TOOLS32 = $(MSVCDIR) +TOOLS32_rc = $(MSVCDIR)\..\common\MSDev98 + +# Uncomment the following line to compile with thread support +#THREADDEFINES = -DTCL_THREADS=1 + +# Set NODEBUG to 0 to compile with symbols +NODEBUG = 1 + +# The following defines can be used to control the amount of debugging +# code that is added to the compilation. +# +# -DTCL_MEM_DEBUG Enables the debugging memory allocator. +# -DTCL_COMPILE_DEBUG Enables byte compilation logging. +# -DTCL_COMPILE_STATS Enables byte compilation statistics gathering. +# -DUSE_TCLALLOC=0 Disables the Tcl memory allocator in favor +# of the native malloc implementation. This is +# +# DEBUGDEFINES = -DTCL_MEM_DEBUG -DTCL_COMPILE_DEBUG -DTCL_COMPILE_STATS +# DEBUGDEFINES = -DUSE_TCLALLOC=0 + + +#------------------------------------------------------------------------- +# +# Do not modify below this line +# +#------------------------------------------------------------------------- + +NAMEPREFIX = tdom +STUBPREFIX = $(NAMEPREFIX)stub +DOTVERSION = 0.7 +VERSION = 07 + +BINROOT = . +!IF "$(NODEBUG)" == "1" +TMPDIRNAME = +DBGX = +!ELSE +TMPDIRNAME = Debug +DBGX = d +!ENDIF +TMPDIR = $(BINROOT) +OUTDIRNAME = $(TMPDIRNAME) +OUTDIR = $(TMPDIR) +TOP_DIR = $(BINROOT)\.. + +TDOMLIB = $(OUTDIR)\$(NAMEPREFIX)$(VERSION)$(DBGX).lib +TDOMDLLNAME = $(NAMEPREFIX)$(VERSION)$(DBGX).dll +TDOMDLL = $(OUTDIR)\$(TDOMDLLNAME) + +MKDIR = .\mkd.bat +RM = del + +LIB_INSTALL_DIR = $(INSTALLDIR)\lib +BIN_INSTALL_DIR = $(INSTALLDIR)\bin +SCRIPT_INSTALL_DIR = $(INSTALLDIR)\lib\tcl$(DOTVERSION) +INCLUDE_INSTALL_DIR = $(INSTALLDIR)\include + + +TDOMOBJS = $(TMPDIR)\xmlrole.obj \ + $(TMPDIR)\xmltok.obj \ + $(TMPDIR)\xmlparse.obj \ + $(TMPDIR)\xmlsimple.obj \ + $(TMPDIR)\utf8conv.obj \ + $(TMPDIR)\dom.obj \ + $(TMPDIR)\domalloc.obj \ + $(TMPDIR)\domhtml.obj \ + $(TMPDIR)\domxslt.obj \ + $(TMPDIR)\nodecmd.obj \ + $(TMPDIR)\domxpath.obj \ + $(TMPDIR)\tclexpat.obj \ + $(TMPDIR)\tcldom.obj \ + $(TMPDIR)\tdominit.obj + +cc32 = "$(TOOLS32)\bin\cl.exe" +link32 = "$(TOOLS32)\bin\link.exe" +rc32 = "$(TOOLS32_rc)\bin\rc.exe" +include32 = -I"$(TOOLS32)\include" +libpath32 = /LIBPATH:"$(TOOLS32)\lib" +tcllibpath = /LIBPATH:"$(INSTALLDIR)\lib" +lib32 = "$(TOOLS32)\bin\lib.exe" + +WINDIR = $(ROOT)\win +GENERICDIR = $(ROOT)\generic +EXPATDIR = $(ROOT)\expat-1.95.1 +TCLINCDIR = $(INSTALLDIR)\Include + +TCL_INCLUDES = -I"$(WINDIR)" -I"$(GENERICDIR)" -I"$(EXPATDIR)" -I"$(TCLINCDIR)" +TCL_DEFINES = $(DEBUGDEFINES) $(THREADDEFINES) + +#------------------------------------------------------------------------- +# +# Compile flags +# +#------------------------------------------------------------------------- + +!IF "$(NODEBUG)" == "1" +# This cranks the optimization level to maximize speed +cdebug = -O2 -Gs -GD +!ELSE +!IF "$(MACHINE)" == "IA64" +cdebug = -Od -Zi +!ELSE +cdebug = -Z7 -Od +!ENDIF +!ENDIF + +# declarations common to all compiler options +cflags = -c -W3 -nologo -Fp$(TMPDIR)\ -YX -DXML_DTD -DXML_NS -DUSE_TCL_STUBS -DCOMPILED_FROM_DSP -DVERSION="\"1.95.1\"" +#cflags = -c -W3 -nologo -Fp$(TMPDIR)\ -YX -DXML_DTD -DXML_NS -DCOMPILED_FROM_DSP -DVERSION="\"1.95.1\"" +cvarsdll = -MD$(DBGX) + +TCL_CFLAGS = $(cdebug) $(cflags) $(cvarsdll) $(include32) \ + $(TCL_INCLUDES) $(TCL_DEFINES) +CON_CFLAGS = $(cdebug) $(cflags) $(include32) -DCONSOLE + +#------------------------------------------------------------------------- +# +# Link flags +# +#------------------------------------------------------------------------- + +!IF "$(NODEBUG)" == "1" +ldebug = /RELEASE +!ELSE +ldebug = -debug:full -debugtype:cv +!ENDIF + +# declarations common to all linker options +lflags = /NODEFAULTLIB /NOLOGO /MACHINE:$(MACHINE) $(libpath32) $(tcllibpath) + +# declarations for use on Intel i386, i486, and Pentium systems +DLLENTRY = @12 +dlllflags = $(lflags) -entry:_DllMainCRTStartup$(DLLENTRY) -dll + + +conlflags = $(lflags) -subsystem:console -entry:mainCRTStartup +guilflags = $(lflags) -subsystem:windows -entry:WinMainCRTStartup + +libc = libc$(DBGX).lib oldnames.lib +libcdll = msvcrt$(DBGX).lib oldnames.lib + +baselibs = kernel32.lib $(optlibs) advapi32.lib user32.lib tclstub83$(DBGX).lib +#baselibs = kernel32.lib $(optlibs) advapi32.lib user32.lib tcl83.lib +winlibs = $(baselibs) gdi32.lib comdlg32.lib winspool.lib + + +guilibs = $(libc) $(winlibs) +conlibs = $(libc) $(baselibs) +guilibsdll = $(libcdll) $(winlibs) +conlibsdll = $(libcdll) $(baselibs) + +#------------------------------------------------------------------------- +# +# Project specific targets +# +#------------------------------------------------------------------------- + +dlls: $(TDOMDLL) +all: dlls + +install: all + @echo installing tDOM + @$(MKDIR) "$(INSTALLDIR)\lib\tDOM" + @xcopy /y $(TDOMDLL) "$(INSTALLDIR)\lib\tDOM" + @xcopy /y pkgIndex.tcl "$(INSTALLDIR)\lib\tDOM" + @xcopy /y ..\lib\tdom.tcl "$(INSTALLDIR)\lib\tDOM" + @xcopy /y ..\lib\domhtml.tcl "$(INSTALLDIR)\lib\tDOM" + +$(TDOMLIB): $(TDOMDLL) + +$(TDOMDLL): $(TDOMOBJS) + $(link32) $(ldebug) $(dlllflags) \ + -out:$@ $(guilibsdll) @<< +$(TDOMOBJS) +<< + +#------------------------------------------------------------------------- +# Implicit rules +#------------------------------------------------------------------------- + +{$(EXPATDIR)}.c{$(TMPDIR)}.obj: + $(cc32) -DBUILD_tcl $(TCL_CFLAGS) -Fo$(TMPDIR)\ $< + +{$(GENERICDIR)}.c{$(TMPDIR)}.obj: + $(cc32) -DBUILD_tcl $(TCL_CFLAGS) -Fo$(TMPDIR)\ $< + +clean: + -@$(RM) $(OUTDIR)\*.exp 2>nul + -@$(RM) $(OUTDIR)\*.lib 2>nul + -@$(RM) $(OUTDIR)\*.dll 2>nul + -@$(RM) $(TMPDIR)\*.pch 2>nul + -@$(RM) $(TMPDIR)\*.obj 2>nul + -@$(RM) $(TMPDIR)\*.ilk 2>nul + -@$(RM) $(TMPDIR)\*.pdb 2>nul ADDED win/mkd.bat Index: win/mkd.bat ================================================================== --- /dev/null +++ win/mkd.bat @@ -0,0 +1,13 @@ +@echo off +rem RCS: @(#) $Id$ + +if exist %1\nul goto end + +md %1 +if errorlevel 1 goto end + +echo Created directory %1 + +:end + + ADDED win/pkgIndex.tcl Index: win/pkgIndex.tcl ================================================================== --- /dev/null +++ win/pkgIndex.tcl @@ -0,0 +1,5 @@ +# tDOM Tcl package index file + +package ifneeded tdom 0.7 \ + "source [file join $dir tdom.tcl]; \ + load [file join $dir tdom07[info sharedlibextension] ] tdom "