Home  >  Article  >  Backend Development  >  Detailed introduction to the complete explanation of spaces in xml

Detailed introduction to the complete explanation of spaces in xml

黄舟
黄舟Original
2017-03-04 17:06:321556browse

Tips: I extracted the core part of the explanation of spaces in "XSLT From Beginner to Master" to use it as a starting point. I hope everyone will actively participate in the discussion. Talk about your understanding of spaces.

Only suitable for scholars who have a certain understanding of xml file structure, not suitable for beginners. Please read from top to bottom.


For html files, spaces are not important; however, for xml, the default position is to retain space nodes (see below for an explanation of space nodes).


According to the XML specification, the so-called space is any combination sequence of four characters:
-------------------- --
space character (space), the corresponding character value is #x20
return character (Carriage Return), the corresponding character value is #xD
newline character (Newline), the corresponding character value is #xA
Tab character (Tab), the corresponding character value is #x9.

The spaces in the xml file will also form nodes, which are space nodes. Space nodes belong to the text node type.

For XML and ##1. To determine which spaces in the xml input file are important, the xslt processor needs to see these space nodes. The key to the decision is the xml:space attribute.
2. To determine which spaces are important in the xsl template file, the xslt processor should copy it to the result tree, and the key to the decision is the two commands xsl:strip-space
and xsl:PReserve-space.

"Important and unimportant space nodes"

-----------------------
If someone The content of a component can only contain components, so the space nodes in the component are insignificant;
If the content of a component is of type #PCDATA, the space nodes within it should be considered important. (Significant).
As for the situation where the component content mixes text content and components, it is impossible to judge. It should depend on the semantic meaning of the component and its content.

Before the xslt processor comes into contact with the xml input file, it will be analyzed by the xml parser

-------------------- ---
(1) The xml:space attribute can change the mode of processing space nodes by subsequent XML applications. For example, the xslt processor will be affected by the xml:space attribute.
(2) The ending symbols at the end of any column mark or content in the xml file will be replaced by a single new line character (#xA).
(3) Before the attribute value is handed over to the XML application, the XML parser should also perform standardized operations on the attribute value. This is because different operating systems have different combinations of the ending characters of each line of text. For example, the Windows system will end with the return character and the new line character, while the Unix system will only end with the new line character group
symbol. After the XML parser reads the XML file, it first replaces all the ending symbols with a single new line character. This not only unifies the differences in the design of different ending symbols between different systems, but also simplifies the operation difficulty of subsequent XML applications. Such a process is called "normalization".
a, the end character of each text column must be normalized to a single newline character (#xA).
b, any space character (#x20, #xD, #xA, #x9) should be replaced by a space character (#x20).
c. If the attribute value contains a word parameter code, it should be replaced with this reference character. For example, will be replaced with a new line character (#xA).
d. If the attribute value contains an entity reference, it should be replaced with its replacement text.
e, any characters other than this should be placed directly into the normalized attribute value.
f. Finally, if the attribute type is not CDATA, the XML parser should further delete the sequence of space characters before and after the attribute value, and if there is a sequence of spaces in the middle of the attribute value, it should also be replaced with a single space character.
 
 
After the xslt processor builds the structure tree of the xml input file and xsl template file, it will merge the adjacent text nodes in the component into a single text node, and then merge some Remove text nodes. However, if the text node meets one of the following conditions, it will be retained:
------------------------
(1) The parent component of the text node is a member of the Set Of Whitespace-preserving Element Names.
(2) There is at least one non-space character in the text node.
(3) There is an xml:space attribute in an ancestor component of the text node, and its value is preserve, and there is no other xml:space attribute value in the recent ancestor component that is default. Other text nodes will be removed.

For xsl templates, only one xsl:text component is available in the so-called space-preserving component name set. The space nodes in the xsl template file will be deleted, but if the space nodes appear in the xsl:text component, they will be retained.


The above is a detailed introduction to the complete explanation of spaces in xml. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn