ProgramI have written a lot, but I have been operating on XML more or less, but I am not fully familiar with some concepts of XML. Recently I have learned some basic knowledge about XML.
XML Namespaces namespace
Without looking at or using the namespace of XML, we still seldom know about it, and we do not know what it is.
The XML namespace is like the namespace of the. NET program. It can provide methods to avoid element naming conflicts.
Why is there a name conflict? Since the element names in XML are predefined, when two different documents use the same element names, a name conflict occurs.
This XML document carries the information in a table:
<Table> <tr> <TD> apples </TD> <TD> bananas </TD> </tr> </table>
This XML document carries information about the table (a piece of furniture ):
<Table> <Name> afrcan coffee table </Name> <width> 80 </width> <length> 120 </length> </table>
If the two XML documents are used together, because both documents contain <Table> elements with different content and definitions, naming conflicts will occur because their semantics is different, that is, we all want to name it table. Use a prefix to avoid name conflicts
This document contains information in a table:
<H: Table>
This XML document carries information about a piece of furniture:
<F: Table> <F: Name> African coffee table </F: Name> <F: width> 80 </F: width> <F: length> 120 </F: length> </F: Table>
Now, the name conflict does not exist because both documents use different names to name their <Table> elements (
When we use XSL, we will see the namespace in use. XSL style sheets are used to convert XML documents to other formats, such as HTML.
<? XML version = "1.0" encoding = "ISO-8859-1"?> <XSL: stylesheetversion = "1.0"Xmlns: XSL = "http://www.w3.org/1999/XSL/Transform"
> <XSL: template match = "/">
</XSL: Template>
. The namespace for XML operations under. net. xmlnamespacemanager resolves the namespace of the Set, adds a namespace to the set, removes the namespace from the set, and provides range management for these namespaces, any prefix in the XPath expression uses the provided system. XML. xmlnamespacemanager is parsed. The following is a simple example of xpath.
Console. writeline (X. documentelement. namespaceuri );
Xmlnamespacemanager xmlnamespace = new xmlnamespacemanager (X. nametable );
Xmlnamespace. addnamespace ("XSL", "http://www.w3.org/1999/XSL/Transform ");
Console. writeline (X. selectsinglenode ("// XSL: Template", xmlnamespace). namespaceuri );
Console. writeline (X. selectsinglenode ("// XSL: Template/html", xmlnamespace). namespaceuri );
Console. Readline ();