For more information, see the BizTalk hands-on experiment series directory.
BizTalk Development Series
Recently, we are working on a BizTalk project, which is very complicated to process XML files. I was wondering if there is any way to remove the spaces of the vertices and attribute values in the XML file at one time. However, after searching for a long time, I didn't see any related methods. If you have any trouble with this method, let me talk about it: cbcye@live.com, but below I sorted out the relevant XML space (WhiteSpace ).
Let's take a look at this question and compare whether the two XML files are the same?
XML (1)
<AddBook>
<Name/>
<Mobile/>
</AddBook>
XML (2)
<AddBook> <Name/> <Mobile/> </AddBook>
If you think the same, let's proceed as follows. We use XSLT (XML Conversion language) to read the entire XML document. This XSLT file is used to copy nodes from the entire source XML file to the target XML file.
<? Xml version = "1.0" encoding = "UTF-8"?>
<Xsl: stylesheet version = "1.0" xmlns: xsl = "http://www.w3.org/1999/XSL/Transform">
<Xsl: output method = "xml" indent = "yes"/>
<Xsl: template match = "node ()">
<Xsl: copy>
<Xsl: apply-templates select = "node ()"/>
</Xsl: copy>
</Xsl: template>
</Xsl: stylesheet>
Set breakpoints on the XSLT debugging interface of Visual Studio during testing. In the Monitoring window, use the Xpath function: node () to view node changes in the XML document.
XML (1) nodes:
XML (2) nodes:
From the test data above, we can see that when XML is in different rows, there will be more nodes of the whitespace type. It can be seen that XML (1) and XML (2) are different. Why are they different? Why do we see the same effect if we use XML Parser such as IE to open two files? Of course, we will not discuss how IE is handled here (in fact, I don't know how IE is handled :). Let's take a look at why the two XML files are different. That is, the Whitespace involved in this article.
First, let's take a look at what is WiteSpace in XML. XML classifies the following four types of characters as blank characters: Carriage Return (\ r or ch (13), line feed (\ n or ch (10), and tab (\ t) and space (''). In XML documents, white space characters are divided into two types:
Meaningful blank characters are part of the document and should be retained.
Meaningless blank characters are used when editing XML documents to increase readability. These blank characters are generally not retained during document delivery.
It can be seen that the XML document sometimes adds special characters such as carriage return and line feed according to the processing needs. However, if these characters are not cleared before processing XML (if necessary ). Then it will cause some errors. For example, if the XSLT file contains the following expression, the blank characters in the XML file may reference the conversion error. Because blank characters cannot be used as node names.
<Xsl: element name = "{local-name ()}">
After learning about the differences between white spaces in XML files and the usefulness of white spaces in XML files, let's take a look at how the XML processor processes white spaces. How to handle white spaces as needed.
XML analysis: the XML specification provides a built-in attribute xml: space to tell the XML analyzer whether it should ignore white spaces. This attribute is inherited by the child element of its root element. During declaration, you must specify it as the enumeration type. The possible values can only be "default" and "preserve ". If "preserve" is specified, the white space characters in the defined element must be retained.
Example:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Root xmlns = "http://quicklearn.cn"/>
<Part1>
<Ns0: books xmlns: ns0 = "http://books.quicklearn.cn"/>
<Ns0: book>
<Name> BizTalk Develop </name>
<ISDN> 1, 0101010101010 </ISDN>
</Ns0: book>
</Ns0: books>
</Part1>
<Part2>
<Ns1: MERs xmlns: ns1 = "customers.quicklearn.cn">
<Customer xml: space = "preserve">
<Name> Zhang San </name>
<Mobile> 1, 133333333333 </mobile>
<Address>
<Province> Guang Dong </province>
<City> Shen Zhen </city>
</Address>
</Customer>
</Ns1: customers>
</Part2>
</Root>
XmlDocument xmlDoc = new XmlDocument ();
XmlDoc. PreserveWhitespace = false;
XmlDoc. Load (@ "D: \ Projects \ Temp \ release ttest \ XMLData2.xml ");
This. richTextBox1.Text = xmlDoc. OuterXml;
<? Xml version = "1.0" encoding = "UTF-8"?> <Root xmlns = "http://quicklearn.cn"/> <part1> <ns0: books xmlns: ns0 = "http://books.quicklearn.cn"/> <ns0: book> <name> BizTalk Develop </name> <ISDN> 0101010101010 </ISDN> </ns0: book> </ns0: books> </part1> <part2> <ns1: MERs xmlns: ns1 = "customers.quicklearn.cn"> <customer xml: space = "preserve">
<Name> Zhang San </name>
<Mobile> 1, 133333333333 </mobile>
<Address>
<Province> Guang Dong </province>
<City> Shen Zhen </city>
</Address>
</Customer> </ns1: MERs> </part2> </root>
XSLT conversion. The W3C XSLT specification provides two elements (namely, xsl: strip-space and xsl: preserve-spacea €) to process white spaces. Xsl: strip-space specifies the XML element that should be deleted from the blank character text node (that is, the text node is completely composed of blank characters. Note that xsl: strip-space only has nodes that affect pure white space characters. Xsl: strip-space can be listed as a group of elements separated by blank characters or by wildcards (for example. Xsl: preserve-space has similar syntax, but the execution result is exactly the opposite of xsl: strip-space.
Usage: declare the global attributes in the XSLT file <xsl: strip-space elements = "*"/> or <xsl: strip-space elements = "element1 element2... "/>
The output result is the same as that of the XML analyzer.
Summary
Understand the functions of XML blank characters and the XML Analysis and Processing Methods of the processor. When processing XML white-space characters, if XML is for background applications, it is best to remove XML white-space characters before processing. If XML is for users to read, the relevant white space characters are retained.