//============================================================================== // // cXmlMethods.pkg: Created to hold XML methods that are useful in a number of // places. Made them global - save using a mixin and calling // Self all the time. // // Author: Mike Peat // Copyright: Mike Peat, 2002 // Date: 21/06/2002 12:33 // // This program is free software; you can redistribute it and/or modify it // under the terms of the GNU General Public License version 2 as published // by the Free Software Foundation (a copy is included in "licence.htm" with // this distribution). This program is distributed in the hope that it will // be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. // // Comments, bug reports (or better yet, bug fixes), etc. to: // // mailto:mike@unicorn-fs.com // // Current version v0.1 dated 21/06/2002 12:33 // // Amendment History: //------------------------------------------------------------------------------ // Date Time Initials // ---------- ----- -------- --------------------------------------------------- // 21/06/2002 12:33 MJP Original //============================================================================== // Pass it an XML child object handle and it will return the next sibling of // that object and destroy the object for you. Just save some code in common // loops. Function NextSib Global Handle hoObj Returns Handle Handle hoNext Get NextSibling of hoObj to hoNext Send Destroy to hoObj Function_Return hoNext End_Function // NextSib // Pass it a QName (prefix:postfix) and it will return the prefix part. If // an NCName is passed a null-string will be returned. Function NamePref Global String sName Returns String If ":" in sName ; Function_Return (Left(sName, (Pos(":", sName) - 1))) Else Function_Return "" End_Function // NamePref // Pass it a QName (prefix:postfix) and it will return the postfix part. If // an NCName is passed the whole string will be returned. Function NamePost Global String sName Returns String Function_Return (Right(sName, (Length(sName) - Pos(":", sName)))) End_Function // NamePost // Pass it an XML object and an attribute name and it returns the attribute // value or a null-string if none found. Function Attrib Global Handle hoElmt String sAtt Returns String Handle hoAtts hoAtt Integer iAtt iLast String sName sText Get AttributeNodes of hoElmt to hoAtts If hoAtts eq 0 Function_Return "" Move (NodeListLength(hoAtts) - 1) to iLast Move "" to sText For iAtt from 0 to iLast Get CollectionNode of hoAtts iAtt to hoAtt Get psNodeName of hoAtt to sName If sName eq sAtt Get psText of hoAtt to sText Send Destroy to hoAtt Loop Send Destroy to hoAtts Function_Return sText End_Function // Attrib // We can't rely on the supplied 'FindNode' method - the b****y namespace // prefixes mess us up - so here is a version that does the same thing the // hard way, but ignores the namespace prefixes (this may be wrong, but it // seems to work). If the attribute passed is blank the first name (postfix) // match will be returned, if it is present then the first name (postfix) // match where the attribute content matches the passed value will be // returned. Function FindChild Global Handle hoParent String sName String sAttrib ; String sVal Returns Handle Handle hoChild Get FirstChild of hoParent to hoChild While hoChild ne 0 If (NamePost(psNodeName(hoChild)) = sName) Begin If sAttrib eq "" Function_Return hoChild If (Attrib(hoChild, sAttrib) = sVal) Function_Return hoChild End Move (NextSib(hoChild)) to hoChild Loop Function_Return 0 // Not found End_Function // FindChild // Hand this an XML document (the section of a WSDL doc for instance) // and the "targetNamespace" of the schema you are looking for and it will // return the handle to that schema or zero if not found. Function FindSchemaByTNS Global Handle hoXml String sNS Returns Handle Handle hoSchema Get FirstChild of hoXml to hoSchema While hoSchema ne 0 If ((NamePost(psNodeName(hoSchema)) = "schema") and ; (Attrib(hoSchema, "targetNamespace") = sNS)) ; // OK, that's it! Function_Return hoSchema // Return that. Move (NextSib(hoSchema)) to hoSchema Loop Function_Return 0 // Not found End_Function // FindSchmaByTNS // Returns TRUE if passed type is one of the basic known ones, FALSE otherwise Function isBaseType Global String sType Returns Boolean Move (Trim(NamePost(sType))) to sType // Types defined in XML 1.0 // Put common ones first for speed: If sType eq "string" Function_Return TRUE If sType eq "int" Function_Return TRUE If sType eq "float" Function_Return TRUE If sType eq "boolean" Function_Return TRUE // ...now the rest: If sType eq "decimal" Function_Return TRUE If sType eq "integer" Function_Return TRUE If sType eq "negativeInteger" Function_Return TRUE If sType eq "nonNegativeInteger" Function_Return TRUE If sType eq "positiveInteger" Function_Return TRUE If sType eq "nonPositiveInteger" Function_Return TRUE If sType eq "long" Function_Return TRUE If sType eq "short" Function_Return TRUE If sType eq "byte" Function_Return TRUE If sType eq "unsignedLong" Function_Return TRUE If sType eq "unsignedShort" Function_Return TRUE If sType eq "unsignedInt" Function_Return TRUE If sType eq "unsignedByte" Function_Return TRUE If sType eq "double" Function_Return TRUE If sType eq "date" Function_Return TRUE If sType eq "dateTime" Function_Return TRUE If sType eq "duration" Function_Return TRUE If sType eq "gDay" Function_Return TRUE If sType eq "gMonth" Function_Return TRUE If sType eq "gMonthDay" Function_Return TRUE If sType eq "gYear" Function_Return TRUE If sType eq "gYearMonth" Function_Return TRUE If sType eq "time" Function_Return TRUE If sType eq "ID" Function_Return TRUE If sType eq "IDREF" Function_Return TRUE If sType eq "IDREFS" Function_Return TRUE If sType eq "ENTITY" Function_Return TRUE If sType eq "ENTITIES" Function_Return TRUE If sType eq "NMTOKEN" Function_Return TRUE If sType eq "NMTOKENS" Function_Return TRUE If sType eq "NOTATION" Function_Return TRUE If sType eq "normalisedString" Function_Return TRUE If sType eq "token" Function_Return TRUE If sType eq "QName" Function_Return TRUE If sType eq "Name" Function_Return TRUE If sType eq "NCName" Function_Return TRUE // Not one of the basic types then... Function_Return FALSE End_Function // isBaseType // Find an element in a schema from an XML document by the schema's // targetNamespace and the element's name. Function FindElementByTNS Global Handle hoXml String sNS String sName Returns Handle Handle hoSchema hoElement Move (FindSchemaByTns(hoXml, sNS)) to hoSchema If hoSchema eq 0 Function_Return 0 // No schema Move (FindChild(hoSchema, "element", "name", sName)) to hoElement Send Destroy to hoSchema Function_Return hoElement End_Function // FindElementByTNS // Returns the text in a named child element of the passed XML object Function ChildText Global Handle hoXML String sChild Returns String Handle hoChild String sText Move (FindChild(hoXML, sChild, "", "")) to hoChild Move (psText(hoChild)) to sText Send Destroy to hoChild Function_Return sText End_Function