📄 xml_domit_xpath.php
字号:
$total = $contextNode->childCount; $nodeCounter = 0; for ($i = 0; $i < $total; $i++) { $currChildNode =& $contextNode->childNodes[$i]; if ($currChildNode->nodeName == $nodeName) { $nodeCounter++; if ($nodeCounter == $index) { $this->localNodeContainer[] =& $currChildNode; } } if ($deep) { $this->_filterByIndex($currChildNode, $nodeName, $index, $deep); } } } } //_filterByIndex /** * Selects named elements with the specified named child * @param string The pattern segment containing the node expression * @param string The tag name of the matching child * @param boolean True if the selection is to be performed recursively */ function filterByChildName($nodeName, $childName, $deep) { if (count($this->globalNodeContainer) != 0) { foreach ($this->globalNodeContainer as $key =>$value) { $currNode =& $this->globalNodeContainer[$key]; $this->_filterByChildName($currNode, $nodeName, $childName, $deep); } } } //filterByChildName /** * Selects named elements with the specified named child * @param object The context node * @param string The pattern segment containing the node expression * @param string The tag name of the matching child * @param boolean True if the selection is to be performed recursively */ function _filterByChildName(&$contextNode, $nodeName, $childName, $deep) { if (($contextNode->nodeType == DOMIT_ELEMENT_NODE) || ($contextNode->nodeType == DOMIT_DOCUMENT_NODE)) { $total = $contextNode->childCount; for ($i = 0; $i < $total; $i++) { $currChildNode =& $contextNode->childNodes[$i]; if (($currChildNode->nodeName == $nodeName) && ($currChildNode->nodeType == DOMIT_ELEMENT_NODE)) { $total2 = $currChildNode->childCount; for ($j = 0; $j < $total2; $j++) { $currChildChildNode =& $currChildNode->childNodes[$j]; if ($currChildChildNode->nodeName == $childName) { $this->localNodeContainer[] =& $currChildNode; } } } if ($deep) { $this->_filterByChildName($currChildNode, $nodeName, $childName, $deep); } } } } //_filterByChildName /** * Selects named attributes of the current context nodes * @param string The attribute name, or * to match all attributes */ function selectAttribute($attrName) { if (count($this->globalNodeContainer) != 0) { foreach ($this->globalNodeContainer as $key =>$value) { $currNode =& $this->globalNodeContainer[$key]; $isRecursive = ($this->searchType == DOMIT_XPATH_SEARCH_VARIABLE) ? true : false; $this->_selectAttribute($currNode, $attrName, $isRecursive); } } $this->charContainer = ''; } //selectAttribute /** * Selects all attributes of the context nodes * @param object The context node * @param string The attribute name, or * to match all attributes * @param boolean True if the selection is to be performed recursively */ function _selectAttribute(&$contextNode, $attrName, $deep) { if (($contextNode->nodeType == DOMIT_ELEMENT_NODE) || ($contextNode->nodeType == DOMIT_DOCUMENT_NODE)) { $total = $contextNode->childCount; for ($i = 0; $i < $total; $i++) { $currNode =& $contextNode->childNodes[$i]; if ($currNode->nodeType == DOMIT_ELEMENT_NODE) { if ($attrName == '*') { $total2 = $currNode->attributes->getLength(); for ($j = 0; $j < $total2; $j++) { $this->localNodeContainer[] =& $currNode->attributes->item($j); } } else { if ($currNode->hasAttribute($attrName)) { $this->localNodeContainer[] =& $currNode->getAttributeNode($attrName); } } } if ($deep) { $this->_selectAttribute($currNode, $attrName, $deep); } } } } //_selectAttribute /** * Selects all child nodes of the current context nodes * @param string The element name */ function selectNamedChild($tagName) { if (count($this->globalNodeContainer) != 0) { foreach ($this->globalNodeContainer as $key =>$value) { $currNode =& $this->globalNodeContainer[$key]; $isRecursive = ($this->searchType == DOMIT_XPATH_SEARCH_VARIABLE) ? true : false; $this->_selectNamedChild($currNode, $tagName, $isRecursive); } } $this->charContainer = ''; } //selectNamedChild /** * Selects all child nodes of the context node * @param object The context node * @param string The element name * @param boolean True if the selection is to be performed recursively */ function _selectNamedChild(&$contextNode, $tagName, $deep = false) { if (($contextNode->nodeType == DOMIT_ELEMENT_NODE) || ($contextNode->nodeType == DOMIT_DOCUMENT_NODE)) { $total = $contextNode->childCount; for ($i = 0; $i < $total; $i++) { $currChildNode =& $contextNode->childNodes[$i]; if (($currChildNode->nodeType == DOMIT_ELEMENT_NODE) || ($currChildNode->nodeType == DOMIT_DOCUMENT_NODE)) { if (($tagName == '*') || ($tagName == $currChildNode->nodeName)) { $this->localNodeContainer[] =& $currChildNode; } if ($deep) { $this->_selectNamedChild($currChildNode, $tagName, $deep); } } } } } //_selectNamedChild /** * Selects parent node of the current context nodes */ function selectParent() { if (count($this->globalNodeContainer) != 0) { foreach ($this->globalNodeContainer as $key =>$value) { $currNode =& $this->globalNodeContainer[$key]; $isRecursive = ($this->searchType == DOMIT_XPATH_SEARCH_VARIABLE) ? true : false; $this->_selectParent($currNode, $isRecursive); } } $this->charContainer = ''; } //selectParent /** * Selects parent node of the current context nodes * @param object The context node * @param boolean True if the selection is to be performed recursively */ function _selectParent(&$contextNode, $deep = false) { if ($contextNode->nodeType == DOMIT_ELEMENT_NODE) { if ($contextNode->parentNode != null) { $this->localNodeContainer[] =& $contextNode->parentNode; } } if ($deep) { if (($contextNode->nodeType == DOMIT_ELEMENT_NODE) || ($contextNode->nodeType == DOMIT_DOCUMENT_NODE)) { $total = $contextNode->childCount; for ($i = 0; $i < $total; $i++) { $currNode =& $contextNode->childNodes[$i]; if ($currNode->nodeType == DOMIT_ELEMENT_NODE) { $this->_selectParent($contextNode, $deep); } } } } } //_selectParent /** * Selects any nodes of the current context nodes which match the given function */ function selectNodesByFunction() { $doProcess = false; $targetNodeType = -1; switch (strtolower(trim($this->charContainer))) { case 'last()': if (count($this->globalNodeContainer) != 0) { foreach ($this->globalNodeContainer as $key =>$value) { $currNode =& $this->globalNodeContainer[$key]; if ($currNode->nodeType == DOMIT_ELEMENT_NODE) { if ($currNode->lastChild != null) { $this->localNodeContainer[] =& $currNode->lastChild; } } } } break; case 'text()': $doProcess = true; $targetNodeType = DOMIT_TEXT_NODE; break; case 'comment()': $doProcess = true; $targetNodeType = DOMIT_COMMENT_NODE; break; case 'processing-instruction()': $doProcess = true; $targetNodeType = DOMIT_PROCESSING_INSTRUCTION_NODE; break; } if ($doProcess) { if (count($this->globalNodeContainer) != 0) { foreach ($this->globalNodeContainer as $key =>$value) { $currNode =& $this->globalNodeContainer[$key]; if ($currNode->nodeType == DOMIT_ELEMENT_NODE) { $total = $currNode->childCount; for ($j = 0; $j < $total; $j++) { if ($currNode->childNodes[$j]->nodeType == $targetNodeType) { $this->localNodeContainer[] =& $currNode->childNodes[$j]; } } } } } } $this->charContainer = ''; } //selectNodesByFunction /** * Splits the supplied pattern into searchable segments * @param string The pattern */ function splitPattern($pattern) { //split multiple patterns if they exist (e.g. pattern1 | pattern2 | pattern3) $this->arPathSegments =& explode(DOMIT_XPATH_SEPARATOR_OR, $pattern); //split each pattern by relative path dividers (i.e., '//') $total = count($this->arPathSegments); for ($i = 0; $i < $total; $i++) { $this->arPathSegments[$i] =& explode(DOMIT_XPATH_SEPARATOR_RELATIVE, trim($this->arPathSegments[$i])); $currArray =& $this->arPathSegments[$i]; $total2 = count($currArray); for ($j = 0; $j < $total2; $j++) { $currArray[$j] =& explode(DOMIT_XPATH_SEPARATOR_ABSOLUTE, $currArray[$j]); } } } //splitPattern /** * Converts long XPath syntax into abbreviated XPath syntax * @param string The pattern * @return string The normalized pattern */ function normalize($pattern) { $pattern = strtr($pattern, $this->normalizationTable); while (strpos($pattern, ' ') !== false) { $pattern = str_replace(' ', ' ', $pattern); } $pattern = strtr($pattern, $this->normalizationTable2); $pattern = strtr($pattern, $this->normalizationTable3); return $pattern; } //normalize /** * Initializes the contextNode and searchType * @param array The current array of path segments * @return int The index of the first array item to begin the search at */ function initSearch(&$currArPathSegments) { $this->globalNodeContainer = array(); if (is_null($currArPathSegments[0])) { if (count($currArPathSegments) == 1) { //variable path $this->searchType = DOMIT_XPATH_SEARCH_VARIABLE; $this->globalNodeContainer[] =& $this->callingNode->ownerDocument; } else { //absolute path $this->searchType = DOMIT_XPATH_SEARCH_ABSOLUTE; $this->globalNodeContainer[] =& $this->callingNode->ownerDocument; } } else { //relative path $this->searchType = DOMIT_XPATH_SEARCH_RELATIVE; if ($this->callingNode->uid != $this->callingNode->ownerDocument->uid) { $this->globalNodeContainer[] =& $this->callingNode; } else { $this->globalNodeContainer[] =& $this->callingNode->ownerDocument; } } } //initSearch} //DOMIT_XPath?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -