-- ------------------------------------------------------------{- |
Module : Text.XML.HXT.DOM.XmlTreeFunctions
Copyright : Copyright (C) 2008 Uwe Schmidt
License : MIT
Maintainer : Uwe Schmidt (uwe@fh-wedel.de)
Stability : stable
Portability: portable
Basic XmlTree functions
-}-- ------------------------------------------------------------moduleText.XML.HXT.DOM.XmlTreeFunctions(moduleText.XML.HXT.DOM.XmlTreeFunctions,moduleText.XML.HXT.DOM.ShowXml)whereimportText.XML.HXT.DOM.XmlTreeTypesimportText.XML.HXT.DOM.XmlKeywordsimportText.XML.HXT.DOM.ShowXmlimportData.Maybe-- --------------------------------------------------------------------------------- XNode Predicates--isXCdataNode::XNode->BoolisXCdataNode(XCdata_)=TrueisXCdataNode_=FalseisXCharRefNode::XNode->BoolisXCharRefNode(XCharRef_)=TrueisXCharRefNode_=FalseisXCmtNode::XNode->BoolisXCmtNode(XCmt_)=TrueisXCmtNode_=FalseisXDTDNode::XNode->BoolisXDTDNode(XDTD__)=TrueisXDTDNode_=FalseisXAttrNode::XNode->BoolisXAttrNode(XAttr_)=TrueisXAttrNode_=FalseisXEntityRefNode::XNode->BoolisXEntityRefNode(XEntityRef_)=TrueisXEntityRefNode_=FalseisXErrorNode::XNode->BoolisXErrorNode(XError__)=TrueisXErrorNode_=FalseisXPiNode::XNode->BoolisXPiNode(XPi__)=TrueisXPiNode_=FalseisXTagNode::XNode->BoolisXTagNode(XTag__)=TrueisXTagNode_=FalseisXTextNode::XNode->BoolisXTextNode(XText_)=TrueisXTextNode_=False-- -----------------------------------------------------------------------------isRootNode::XNode->BoolisRootNode=isTagNodet_rootisTagNode::String->XNode->BoolisTagNoden=isOfTagNode((==n).qualifiedName)isOfTagNode::(TagName->Bool)->XNode->BoolisOfTagNodep(XTagn_)=pnisOfTagNode__=FalseisAttrNode::String->XNode->BoolisAttrNoden=isOfAttrNode((==n).qualifiedName)isOfAttrNode::(AttrName->Bool)->XNode->BoolisOfAttrNodep(XAttrn)=pnisOfAttrNode__=FalseisTextNode::String->XNode->BoolisTextNodet=isOfTextNode(==t)isOfTextNode::(String->Bool)->XNode->BoolisOfTextNodep(XTextt)=ptisOfTextNode__=FalseisPiNode::String->XNode->BoolisPiNoden=isOfPiNode((==n).qualifiedName)isOfPiNode::(TagName->Bool)->XNode->BoolisOfPiNodep(XPin_)=pnisOfPiNode__=FalseisDTDElemNode::DTDElem->XNode->BoolisDTDElemNodee(XDTDn_)=n==eisDTDElemNode__=FalseisErrorNode::Int->XNode->BoolisErrorNodel(XErrorl'_)=l==l'isErrorNode__=False-- -----------------------------------------------------------------------------textOfXNode::XNode->StringtextOfXNode(XTextt)=ttextOfXNode_=""-- --------------------------------------------------------------------------------- XmlTree constructors-- |-- Create a tree with a tag node.---- * 1.parameter n : the name of the tag---- - 2.parameter al : the tag attribte list---- - 3.parameter cs : the list of children---- - returns : the new treemkXTagTree::String->XmlTrees->XmlTrees->XmlTreemkXTagTreenalcs=mkNode(XTag(mkNamen)al)cs-- | Version with qualified name of 'mkXTagTree'mkQTagTree::QName->XmlTrees->XmlTrees->XmlTreemkQTagTreeqalcs=mkNode(XTagqal)cs-- |-- create a tree with a namespace aware tag node.---- * 1.parameter n : the prefix:localpart of the tag---- - 2.parameter ns: the namespace uri---- - 3.parameter al : the tag attribte list---- - 4.parameter cs : the list of children---- - returns : the new tree---- see also: 'mkXTagTree'mkXNsTagTree::String->String->XmlTrees->XmlTrees->XmlTreemkXNsTagTreennsalcs=mkNode(XTag(mkNsNamenns)al)cs-- |-- creates a new document tree with empty contents.---- * 1.parameter al : the attribute list for the root node---- returns a single node tree with tag name \"\/\" indicating a root and-- with empty list of children---- see also : 'emptyRoot'newRoot::XmlTrees->XmlTreenewRootal=mkXTagTreet_rootal[]-- | the empty document tree---- see also : 'newRoot'emptyRoot::XmlTreeemptyRoot=newRoot[]-- |-- create a new empty document with source name as parameternewDocument::String->XmlTreenewDocumentn=newDocument'[(a_source,n),(a_status,showc_ok)]-- |-- create a new empty document with a list of attributes for source location and options---- see also : 'newDocument'newDocument'::Attributes->XmlTreenewDocument'al=newRoot(fromAttrlal)-- |-- create a document root tree.---- * 1.parameter al : the attribute list for the root. This list must contain at-- least an attribute \"source\" that contains the URI of the document to be processed---- - 2.parameter cs : the list for the document content---- - returns : the document rootmkRootTree::XmlTrees->XmlTrees->XmlTreemkRootTreealcs=mkXTagTreet_rootalcs-- |-- create a leaf for a text element.---- * 1.parameter txt : the text---- - returns : the tree with the single node containing the textmkXTextTree::String->XmlTreemkXTextTrees=mkLeaf(XTexts)-- |-- create a leaf for a char reference.---- * 1.parameter i : the integer representing the Unicode char---- - returns : the tree with the single node containing the char referencemkXCharRefTree::Int->XmlTreemkXCharRefTrees=mkLeaf(XCharRefs)-- |-- create a leaf for an entity reference.---- * 1.parameter n : the name of the entity reference---- - returns : the tree with the single node containing the entity referencemkXEntityRefTree::String->XmlTreemkXEntityRefTrees=mkLeaf(XEntityRefs)-- |-- create a leaf for a comment,---- * 1.parameter c : the comment text---- - returns : the tree with the single node containing the commentmkXCmtTree::String->XmlTreemkXCmtTreec=mkLeaf(XCmtc)-- |-- create a tree for a part of a DTD---- * 1.parameter d : the type of the DTD part---- - 2.parameter al : the attribute list for the DTD part---- - 3.parameter ds : the possibly empty list of components for the DTD part---- - returns : the tree with the composed DTD partmkXDTDTree::DTDElem->Attributes->XmlTrees->XmlTreemkXDTDTreedalds=mkNode(XDTDdal)ds-- |-- create an attribute tree as part of a tag attribute list of tag nodes---- * 1.parameter al : the attribute name---- - 2.parameter av : the attribute value as tree list, usually containing a single text nodemkXAttrTree::String->XmlTrees->XmlTreemkXAttrTreeanav=mkNode(XAttr(mkNamean))av-- | Qualified version of 'mkXAttrTree'mkQAttrTree::QName->XmlTrees->XmlTreemkQAttrTreeaqav=mkNode(XAttraq)av-- |-- create an attribute tree with a namespace---- * 1.parameter al : the attribute name---- - 2.parameter ns : namespace uri---- - 3.parameter av : the attribute value as tree list, usually containing a single text node---- see also: 'mkXAttrTree', 'mkXNsTagTree'mkXNsAttrTree::String->String->XmlTrees->XmlTreemkXNsAttrTreeannsav=mkNode(XAttr(mkNsNameanns))av-- |-- create a parameter entity reference DTD part.---- * 1.parameter ref : the name of the reference---- - returns : the DTD part for a PERefmkXPERefTree::String->XmlTreemkXPERefTreeref=mkLeaf(XDTDPEREF[(a_peref,ref)])-- |-- create a processing instruction tree.---- * 1.parameter n : the name of the PI---- - 2.parameter str : the content of a PI---- - returns : the processing instruction tree with a single attribute \"value\"-- with the str parameter as attribute value, with @valueOf a_value@ applied to the result tree-- the content of the PI can be selectedmkXPiTree::String->String->XmlTreemkXPiTreenstr=mkLeaf(XPi(mkNamen)(xattra_valuestr))-- |-- create xml declarationmkXmlDeclTree::XmlTrees->XmlTreemkXmlDeclTreeal=mkLeaf(XPi(mkNamet_xml)al)-- |-- create a CDATA section tree.---- * 1.parameter s : the content of the CDATA section---- - returns : the tree for the CDATA sectionmkXCdataTree::String->XmlTreemkXCdataTrees=mkLeaf(XCdatas)-- |-- create an error tree.---- * 1.parameter l : the level of the error (warning, error fatal)---- - 2.parameter msg : the error message---- - 3.parameter cs : the context, where the error was detectedmkXErrorTree::Int->String->XmlTrees->XmlTreemkXErrorTreelscs=mkNode(XErrorls)csmaybeString2XText::MaybeString->XmlTreesmaybeString2XText=mapmkXTextTree.maybeToList-- ---------------------------------------------------------------- text selectionshowXText::XmlTrees->StringshowXText=concatMapshowTwhereshowT(NTree(XTextt)_)=tshowT_=""showXCharRef::XmlTrees->StringshowXCharRef=concatMapshowTwhereshowT(NTree(XCharRefr)_)="&#"++showr++";"showT_=""showXEntityRef::XmlTrees->StringshowXEntityRef=concatMapshowTwhereshowT(NTree(XEntityRefr)_)="&"++r++";"showT_=""showXErrors::XmlTrees->StringshowXErrors=concatMapshowEwhereshowE(NTree(XErrorlevelstr)_)=msglevel++": "++str++"\n"showE_=""msg::Int->Stringmsgl|l==c_warn="Warning"|l==c_err="Error"|otherwise="Fatal error"-- ---------------------------------------------------------------- the toString conversion functions-- |-- old name for 'xshow' (deprecated)xmlTreesToString::XmlTrees->StringxmlTreesToString=xshow-- |-- conversion of a filter result into a text node---- see also : 'xshow'xmlTreesToText::XmlSFilterxmlTreesToTextts@[(NTree(XText_)_)]=ts-- special case optimisationxmlTreesToTextts@[]=tsxmlTreesToTextts=xtext.xshow$ts-- ------------------------------------------------------------xmlContentModelToString::XmlTree->StringxmlContentModelToString(NTree(XDTDELEMENTal)cs)=showElemType(lookup1a_typeal)cs""xmlContentModelToString_=""-- --------------------------------------------------------------------------------- string access functions-- |-- select the name of a node. For tags, attributes and pi\'s the name string-- is returned, else the empty string.nameOf::XmlTree->StringnameOf=selName.getNodewhereselName(XTagn_)=qualifiedNamenselName(XAttrn)=qualifiedNamenselName(XPin_)=qualifiedNamenselName_=""-- |-- select the local part of a name of a node. For tags, attributes the name string-- is returned, for pi's the whole name, else the empty string.localPartOf::XmlTree->StringlocalPartOf=selName.getNodewhereselName(XTagn_)=localPartnselName(XAttrn)=localPartnselName(XPin_)=qualifiedNamenselName_=""-- |-- select the namespace URI of a tag or an attribute tree, else the empty string is returned-- see also : 'nameOf'namespaceOf::XmlTree->StringnamespaceOf=selName.getNodewhereselName(XTagn_)=namespaceUrinselName(XAttrn)=namespaceUrinselName_=""-- |-- select the namespace prefix of a tag or an attribute tree, else the empty string is returned-- see also : 'nameOf', 'localPartOf'prefixOf::XmlTree->StringprefixOf=selName.getNodewhereselName(XTagn_)=namePrefixnselName(XAttrn)=namePrefixnselName_=""-- |-- select the universal name (namespace uri ++ localPart) of a tag or an attribute tree, else the empty string is returned-- see also : 'nameOf', 'namespaceOf'universalNameOf::XmlTree->StringuniversalNameOf=selName.getNodewhereselName(XTagn_)=universalNamenselName(XAttrn)=universalNamenselName_=""-- |-- select the attributes of a dtd treeattrlOfDTD::XmlTree->AttributesattrlOfDTD(NTree(XDTD_al)_)=alattrlOfDTD_=[]-- |-- select a special attribute of a DTD partvalueOfDTD::String->XmlTree->StringvalueOfDTDn=lookup1n.attrlOfDTD-- |-- test an attribute of a DTD partofDTDequals::String->String->XmlTree->BoolofDTDequalsnv=(==v).valueOfDTDn-- --------------------------------------------------------------------------------- convenient functionsxcmt::String->XmlTreesxcmtcmt=[mkXCmtTreecmt]xerr::String->XmlTreesxerrmsg=[mkXErrorTreec_errmsg[]]xwarn::String->XmlTreesxwarnmsg=[mkXErrorTreec_warnmsg[]]xtext::String->XmlTreesxtextt=[mkXTextTreet]xtag::String->XmlTrees->XmlTrees->XmlTreesxtagtalcl=[mkXTagTreetalcl]xattr::String->String->XmlTreesxattrnv=[mkXAttrTreen(xtextv)]-- --------------------------------------------------------------------------------- conversion functions: XmlTrees <-> AttributestoTreel::XmlTrees->AssocListStringXmlTreestoTreel=concatMaptoTreewheretoTree(NTree(XAttrn)cs)=[(qualifiedNamen,cs)]toTree_=[]toAttrl::XmlTrees->AttributestoAttrl=map(\(k,tl)->(k,xshowtl)).toTreelfromTreel::AssocListStringXmlTrees->XmlTreesfromTreel=map(\(k,tl)->mkXAttrTreektl)fromAttrl::Attributes->XmlTreesfromAttrl=fromTreel.map(\(k,v)->(k,xtextv))-- -----------------------------------------------------------------------------