📄 v2.php
字号:
$first = true; foreach ($attributes as $key => $value) { if ($entities != XML_UTIL_ENTITIES_NONE) { $value = XML_Util::replaceEntities($value, $entities); } if ($first) { $string .= " ".$key.'="'.$value.'"'; $first = false; } else { $string .= $linebreak.$indent.$key.'="'.$value.'"'; } } } } return $string; } /** * create a tag * * This method will call XML_Util::createTagFromArray(), which * is more flexible. * * <code> * require_once 'XML/Util.php'; * * // create an XML tag: * $tag = XML_Util::createTag("myNs:myTag", array("foo" => "bar"), "This is inside the tag", "http://www.w3c.org/myNs#"); * </code> * * @access public * @static * @param string $qname qualified tagname (including namespace) * @param array $attributes array containg attributes * @param mixed $content * @param string $namespaceUri URI of the namespace * @param integer $replaceEntities whether to replace XML special chars in content, embedd it in a CData section or none of both * @param boolean $multiline whether to create a multiline tag where each attribute gets written to a single line * @param string $indent string used to indent attributes (_auto indents attributes so they start at the same column) * @param string $linebreak string used for linebreaks * @param string $encoding encoding that should be used to translate content * @return string $string XML tag * @see XML_Util::createTagFromArray() * @uses XML_Util::createTagFromArray() to create the tag */ function createTag($qname, $attributes = array(), $content = null, $namespaceUri = null, $replaceEntities = XML_UTIL_REPLACE_ENTITIES, $multiline = false, $indent = "_auto", $linebreak = "\n", $encoding = XML_UTIL_ENTITIES_XML) { $tag = array( "qname" => $qname, "attributes" => $attributes ); // add tag content if ($content !== null) { $tag["content"] = $content; } // add namespace Uri if ($namespaceUri !== null) { $tag["namespaceUri"] = $namespaceUri; } return XML_Util::createTagFromArray($tag, $replaceEntities, $multiline, $indent, $linebreak, $encoding); } /** * create a tag from an array * this method awaits an array in the following format * <pre> * array( * "qname" => $qname // qualified name of the tag * "namespace" => $namespace // namespace prefix (optional, if qname is specified or no namespace) * "localpart" => $localpart, // local part of the tagname (optional, if qname is specified) * "attributes" => array(), // array containing all attributes (optional) * "content" => $content, // tag content (optional) * "namespaceUri" => $namespaceUri // namespaceUri for the given namespace (optional) * ) * </pre> * * <code> * require_once 'XML/Util.php'; * * $tag = array( * "qname" => "foo:bar", * "namespaceUri" => "http://foo.com", * "attributes" => array( "key" => "value", "argh" => "fruit&vegetable" ), * "content" => "I'm inside the tag", * ); * // creating a tag with qualified name and namespaceUri * $string = XML_Util::createTagFromArray($tag); * </code> * * @access public * @static * @param array $tag tag definition * @param integer $replaceEntities whether to replace XML special chars in content, embedd it in a CData section or none of both * @param boolean $multiline whether to create a multiline tag where each attribute gets written to a single line * @param string $indent string used to indent attributes (_auto indents attributes so they start at the same column) * @param string $linebreak string used for linebreaks * @return string $string XML tag * @see XML_Util::createTag() * @uses XML_Util::attributesToString() to serialize the attributes of the tag * @uses XML_Util::splitQualifiedName() to get local part and namespace of a qualified name */ function createTagFromArray($tag, $replaceEntities = XML_UTIL_REPLACE_ENTITIES, $multiline = false, $indent = "_auto", $linebreak = "\n", $encoding = XML_UTIL_ENTITIES_XML) { if (isset($tag["content"]) && !is_scalar($tag["content"])) { return XML_Util::raiseError( "Supplied non-scalar value as tag content", XML_UTIL_ERROR_NON_SCALAR_CONTENT ); } if (!isset($tag['qname']) && !isset($tag['localPart'])) { return XML_Util::raiseError( 'You must either supply a qualified name (qname) or local tag name (localPart).', XML_UTIL_ERROR_NO_TAG_NAME ); } // if no attributes hav been set, use empty attributes if (!isset($tag["attributes"]) || !is_array($tag["attributes"])) { $tag["attributes"] = array(); } // qualified name is not given if (!isset($tag["qname"])) { // check for namespace if (isset($tag["namespace"]) && !empty($tag["namespace"])) { $tag["qname"] = $tag["namespace"].":".$tag["localPart"]; } else { $tag["qname"] = $tag["localPart"]; } // namespace URI is set, but no namespace } elseif (isset($tag["namespaceUri"]) && !isset($tag["namespace"])) { $parts = XML_Util::splitQualifiedName($tag["qname"]); $tag["localPart"] = $parts["localPart"]; if (isset($parts["namespace"])) { $tag["namespace"] = $parts["namespace"]; } } if (isset($tag["namespaceUri"]) && !empty($tag["namespaceUri"])) { // is a namespace given if (isset($tag["namespace"]) && !empty($tag["namespace"])) { $tag["attributes"]["xmlns:".$tag["namespace"]] = $tag["namespaceUri"]; } else { // define this Uri as the default namespace $tag["attributes"]["xmlns"] = $tag["namespaceUri"]; } } // check for multiline attributes if ($multiline === true) { if ($indent === "_auto") { $indent = str_repeat(" ", (strlen($tag["qname"])+2)); } } // create attribute list $attList = XML_Util::attributesToString($tag["attributes"], true, $multiline, $indent, $linebreak ); if (!isset($tag["content"]) || (string)$tag["content"] == '') { $tag = sprintf("<%s%s />", $tag["qname"], $attList); } else { if ($replaceEntities == XML_UTIL_REPLACE_ENTITIES) { $tag["content"] = XML_Util::replaceEntities($tag["content"], $encoding); } elseif ($replaceEntities == XML_UTIL_CDATA_SECTION) { $tag["content"] = XML_Util::createCDataSection($tag["content"]); } $tag = sprintf("<%s%s>%s</%s>", $tag["qname"], $attList, $tag["content"], $tag["qname"] ); } return $tag; } /** * create a start element * * <code> * require_once 'XML/Util.php'; * * // create an XML start element: * $tag = XML_Util::createStartElement("myNs:myTag", array("foo" => "bar") ,"http://www.w3c.org/myNs#"); * </code> * * @access public * @static * @param string $qname qualified tagname (including namespace) * @param array $attributes array containg attributes * @param string $namespaceUri URI of the namespace * @param boolean $multiline whether to create a multiline tag where each attribute gets written to a single line * @param string $indent string used to indent attributes (_auto indents attributes so they start at the same column) * @param string $linebreak string used for linebreaks * @return string $string XML start element * @see XML_Util::createEndElement(), XML_Util::createTag() */ function createStartElement($qname, $attributes = array(), $namespaceUri = null, $multiline = false, $indent = '_auto', $linebreak = "\n") { // if no attributes hav been set, use empty attributes if (!isset($attributes) || !is_array($attributes)) { $attributes = array(); } if ($namespaceUri != null) { $parts = XML_Util::splitQualifiedName($qname); } // check for multiline attributes if ($multiline === true) { if ($indent === "_auto") { $indent = str_repeat(" ", (strlen($qname)+2)); } } if ($namespaceUri != null) { // is a namespace given if (isset($parts["namespace"]) && !empty($parts["namespace"])) { $attributes["xmlns:".$parts["namespace"]] = $namespaceUri; } else { // define this Uri as the default namespace $attributes["xmlns"] = $namespaceUri; } } // create attribute list $attList = XML_Util::attributesToString($attributes, true, $multiline, $indent, $linebreak); $element = sprintf("<%s%s>", $qname, $attList); return $element; } /** * create an end element * * <code> * require_once 'XML/Util.php'; * * // create an XML start element: * $tag = XML_Util::createEndElement("myNs:myTag"); * </code> * * @access public * @static * @param string $qname qualified tagname (including namespace) * @return string $string XML end element * @see XML_Util::createStartElement(), XML_Util::createTag() */ function createEndElement($qname) { $element = sprintf("</%s>", $qname); return $element; } /** * create an XML comment * * <code> * require_once 'XML/Util.php'; * * // create an XML start element: * $tag = XML_Util::createComment("I am a comment"); * </code> * * @access public * @static * @param string $content content of the comment * @return string $comment XML comment */ function createComment($content) { $comment = sprintf("<!-- %s -->", $content); return $comment; } /** * create a CData section * * <code> * require_once 'XML/Util.php'; * * // create a CData section * $tag = XML_Util::createCDataSection("I am content."); * </code> * * @access public * @static * @param string $data data of the CData section * @return string $string CData section with content */ function createCDataSection($data) { return sprintf("<![CDATA[%s]]>", $data); } /** * split qualified name and return namespace and local part * * <code> * require_once 'XML/Util.php'; * * // split qualified tag * $parts = XML_Util::splitQualifiedName("xslt:stylesheet"); * </code> * the returned array will contain two elements: * <pre> * array( * "namespace" => "xslt", * "localPart" => "stylesheet" * ); * </pre> * * @access public * @static * @param string $qname qualified tag name * @param string $defaultNs default namespace (optional) * @return array $parts array containing namespace and local part */ function splitQualifiedName($qname, $defaultNs = null) { if (strstr($qname, ':')) { $tmp = explode(":", $qname); return array( "namespace" => $tmp[0], "localPart" => $tmp[1] ); } return array( "namespace" => $defaultNs, "localPart" => $qname ); } /** * check, whether string is valid XML name * * <p>XML names are used for tagname, attribute names and various * other, lesser known entities.</p> * <p>An XML name may only consist of alphanumeric characters, * dashes, undescores and periods, and has to start with a letter * or an underscore. * </p> * * <code> * require_once 'XML/Util.php'; * * // verify tag name * $result = XML_Util::isValidName("invalidTag?"); * if (XML_Util::isError($result)) { * print "Invalid XML name: " . $result->getMessage(); * } * </code> * * @access public * @static * @param string $string string that should be checked * @return mixed $valid true, if string is a valid XML name, PEAR error otherwise * @todo support for other charsets */ function isValidName($string) { // check for invalid chars if (!preg_match("/^[[:alnum:]_\-.]$/", $string{0})) { return XML_Util::raiseError( "XML names may only start with letter or underscore", XML_UTIL_ERROR_INVALID_START ); } // check for invalid chars if (!preg_match("/^([a-zA-Z_]([a-zA-Z0-9_\-\.]*)?:)?[a-zA-Z_]([a-zA-Z0-9_\-\.]+)?$/", $string)) { return XML_Util::raiseError( "XML names may only contain alphanumeric chars, period, hyphen, colon and underscores", XML_UTIL_ERROR_INVALID_CHARS ); } // XML name is valid return true; } /** * replacement for XML_Util::raiseError * * Avoids the necessity to always require * PEAR.php * * @access public * @param string error message * @param integer error code * @return object PEAR_Error */ function raiseError($msg, $code) { require_once 'PEAR.php'; return PEAR::raiseError($msg, $code); }}//} // if (!class_exists('XML_Util'))?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -