In XML, element names are defined by developers. when two different documents use the same element names, a name conflict occurs. The XML namespace provides methods to avoid element naming conflicts.
Name conflict
In XML, element names are defined by developers. when two different documents use the same element names, a name conflict occurs.
This XML document carries the information in a table:
Apples Bananas
This XML document carries information about the table (a piece of furniture ):
African Coffee Table
80
120
If the two XML documents are used together, because both documents contain different content and definitions
The XML parser cannot determine how to handle such conflicts.
This document contains information in a table:
Apples
Bananas
This XML document carries information about a piece of furniture:
African Coffee Table
80
120
Now, the name conflict does not exist because both documents use different names to name them.
Element.Use a prefix to avoid name conflicts
And ).
By using the prefix, we create two different types
This XML document carries the information in a table:
Apples
Bananas
This XML document carries information about a piece of furniture:
African Coffee Table
80
120
Unlike the prefix
The XML namespace attribute is placed in the start tag of the element and uses the following syntax:
xmlns:namespace-prefix="namespaceURI"
When a namespace is defined in the start tag of an element, all child elements with the same prefix are associated with the same namespace.
Note: the address used to indicate the namespace will not be used by the parser to find information. The only function is to assign a unique namespace name. However, many companies often use namespaces as pointers to point to existing webpages that contain information about namespaces.
Uniform Resource IdentifierIs a string of characters that can identify Internet resources. The most common URI is used to indicate the Internet domain name address.Uniform Resource Locator (URL). Another less commonly used URI isUnified Resource naming (URN). In our example, we only use URL.
Defining the default namespace for the element saves us the effort to use the prefix in all sub-elements.
Use the following syntax:
xmlns="namespaceURI"
This XML document carries the information in a table:
The tag adds an xmlns attribute, so that a qualified name associated with a namespace is assigned to the prefix.XML Namespace (xmlns) attributesUniform Resource Identifier (URI ))Default Namespaces)
This XML document carries information about a piece of furniture:
African Coffee Table
80
120
(To the original http://www.cnblogs.com/mgen/archive/2011/05/24/2056025.html)
As we all know, XmlDocument can be used for XPath queries, but in fact, the XPath query mentioned here is limited to XML without a namespace (without the xmlns attribute). once an XML with a namespace is encountered, no result is returned for the corresponding XPath query.
For example, the following XML
ccc
XPath queries/a/B will return null, and if there is no xmlns, it will return node B.
If the XPath expression does not include a prefix, it is assumed that the namespace URI is the empty namespace. If your XML includes a default namespace, you must still add a prefix and namespace URI to the XmlNamespaceManager; otherwise, you will not get any nodes selected
This means that if the XPath expression does not have a prefix (for example, the prefix of a: B is a), the queried node (note that the attribute can also be a node) the URI of the namespace should be null (also the default value); otherwise, XPath will not return the result.
In the XML above, because node a and node B both have namespaces, there will be no results in the natural XPath query.
(If the node has a default namespace, you must manually add the prefix and namespace value to XmlNamespaceManager. this will be explained later)
Before looking at the solution, you must first be able to identify the XML namespace. of course, it is easy to identify the XML namespace value. refer to the following XML (this XML will be used in later programs)
data in a
data in b
data in e
The namespaces of all its XML nodes are as follows:
data in a
data in b
data in e
If there is no problem in identifying the XML namespace, the subsequent operations will be quite simple. remember:When you use XPath to query a node in XmlDocument, you must give it a prefix as long as its namespace value is not a null value., Use this prefix to represent the namespace value of this node! These prefixes are added through the XmlNamespaceManager class. when used, you can pass XmlNamespaceManager to SelectNodes or SelectSingleNode. This is why "if a node has a default namespace, you have to manually add the prefix and namespace value to XmlNamespaceManager.
In addition, XmlNameTable is required to construct an XmlNamespaceManager object, which can be obtained from the XmlDocument. NameTable and XmlReader. NameTable attributes.
Next we step into the code, such as querying node e in the XML above, and analyzing the location node e is located at: root-> c-> d-> e, then add the required namespace value to XmlNamespaceManager (the prefix name does not matter, as long as the XPath is consistent). The query is successful, as shown in the following code:
/** Assume that the above XML file is in C: \ a.txt * The following code queries the target node e and outputs data: data in e **/var xmlDoc = newXmlDocument (); xmlDoc. load (@ "C: \ a.txt"); // add the namespace and prefix var xmlnsm = newXmlNamespaceManager (xmlDoc. nameTable); xmlnsm. addNamespace ("d", "dotnet"); xmlnsm. addNamespace ("s", "silverlight"); xmlnsm. addNamespace ("w", "wpf"); var node = xmlDoc. selectSingleNode ("/d: root/s: c/w: d/s: e", xmlnsm); Console. writeLine (node. innerText); // output: data in e
The above is a detailed description of the XML Namespaces introduction and the sample code of the node reading method. For more information, see other related articles in the first PHP community!