WebDAV用到的XML的一些概念
最近在做跟WebDAV有关的个人项目,WebDAV协议里有一部分HTTP method的body需要用到xml,就查了些不熟的概念,copy到这里放在一起方便看。
XML Element
What is an XML Element?
An XML element is everything from (including) the element's start tag
to (including) the element's end tag.
<price>29.99</price>
An element can contain:
- text
- attributes
- other elements
- or a mix of the above
1 | <bookstore> |
In the example above:
<title>
, <author>
,
<year>
, and <price>
have
text content because they contain text (like
29.99
).
<bookstore>
and <book>
have
element contents, because they contain elements.
<book>
has an attribute
(category="children"
).
Empty XML Elements
An element with no content is said to be empty.
In XML, you can indicate an empty element like this:
<element></element>
You can also use a so called self-closing tag:
<element />
The two forms produce identical results in XML software (Readers, Parsers, Browsers).
Empty elements can have attributes.
Namespace
Solving the Name Conflict Using a Prefix
Name conflicts in XML can easily be avoided using a name prefix.
This XML carries information about an HTML table, and a piece of furniture:
1 | <h:table> |
In the example above, there will be no conflict because the two
<table>
elements have different names.
Local Name
the element's actual name within the namespace, for example in
<h:table>
, table
is the local name
XML Namespaces - The xmlns Attribute
When using prefixes in XML, a namespace for the prefix must be defined.
The namespace can be defined by an xmlns attribute in the start tag of an element.
The namespace declaration has the following syntax. xmlns:prefix="URI".
1 | <root> |
In the example above:
The xmlns attribute in the first <table>
element
gives the h:
prefix a qualified namespace.
The xmlns attribute in the second <table>
element
gives the f:
prefix a qualified namespace.
When a namespace is defined for an element, all child elements with the same prefix are associated with the same namespace.
Namespaces can also be declared in the XML root element:
1 | <root xmlns:h="http://www.w3.org/TR/html4/" |
Note: The namespace URI is not used by the parser to look up information.
The purpose of using an URI is to give the namespace a unique name.
However, companies often use the namespace as a pointer to a web page containing namespace information.
Uniform Resource Identifier (URI)
A Uniform Resource Identifier (URI) is a string of characters which identifies an Internet Resource.
The most common URI is the Uniform Resource Locator (URL) which identifies an Internet domain address. Another, not so common type of URI is the Uniform Resource Name (URN).
Default Namespaces
Defining a default namespace for an element saves us from using
prefixes in all the child elements. It has the following syntax:
xmlns="namespaceURI"
This XML carries HTML table information:
1 | <table xmlns="http://www.w3.org/TR/html4/"> |
This XML carries information about a piece of furniture:
1 | <table xmlns="https://www.w3schools.com/furniture"> |
copy from: https://www.w3schools.com/