The most common problem with XML data is the case sensitivity of the data, which can cause headaches during data conversion. The following is a solution.
Assume that you have some data to be sent to another system. It also recognizes data in XML format and requires all data in uppercase. The example data is as follows:
Example 1: person. xml
<xmp> &lt;Person&gt; &lt;Name&gt; &lt;first&gt; net_lover &lt;/first&gt; &lt;last&gt; xianhui Meng &lt;/last&gt; &lt;chinesename&gt; Meng xianhui &lt;/chinesename&gt; &lt;/Name&gt; &lt;/person&gt; </xmp>
Suppose you want to convert to the following format:
Example 2: newperson. xml
<xmp> &lt;Person&gt; &lt;Name&gt; &lt;first&gt; net_lover &lt;/first&gt; &lt;last&gt; xianhui Meng &lt;/last&gt; &lt;chinesename&gt; Meng xianhui &lt;/chinesename&gt; &lt;/Name&gt; &lt;/person&gt; </xmp>
To solve this conversion problem, we usually use the translate () in the XPath function, for example:
<xmp> &lt;Foo&gt; &lt;value-of select = &quot;translate (&#39;this is a test&#39;, &#39;tis &#39;, &#39;tis&#39;)&quot;&gt; &lt;/value-of&gt; &lt;/Foo&gt; </xmp>
The conversion result is as follows:
<xmp> &lt;Foo&gt; This is a test &lt;/Foo&gt; </xmp>
But how can we convert all the text into uppercase? The following is the process.Code:
Define two variables first:
<xmp> &lt;Variable name = &quot;uppercase&quot;&gt; abcdefghijklmnopqrstuvwxyz &lt;/variable&gt; &lt;variable name = &quot;lowercase&quot;&gt; abcdefghijklmnopqrstuvwxyz &lt;/variable&gt; </xmp>
Then convert:
<xmp> &lt;Foo&gt; &lt;value-of select = &quot;translate (&#39;this is a test&#39;, $ lowercase, $ uppercase)&quot;&gt; &lt;/value-of&gt; &lt;/Foo&gt; </xmp>
The result is as follows:
<xmp> &lt;Foo&gt; This is a test &lt;/Foo&gt; </xmp>
For the problem, you can write the XSL file as follows:
Example 3: person. XSL
<xmp> &lt;? XML version = &quot;1.0&quot; encoding = &quot;gb2312&quot;?&gt; &lt;Br/&gt; &lt;stylesheet xmlns: XSL = &quot;http://www.w3.org/1999/XSL/Transform&quot; version = &quot;1.0&quot;&gt; &lt;variable name = &quot;uppercase&quot;&gt; lower &lt;/variable&gt; &lt;variable name = &quot;lowercase&quot;&gt; abcdefghijklmnopqrstuvwxyz &lt;/variable&gt; &lt;template match = &quot;/&quot;&gt; &lt;person&gt; &lt;Name&gt; &lt;first&gt; &lt;value-of select = &quot;translate (/person/name/first, $ lowercase, $ uppercase) &quot;&gt; &lt;/value-of&gt; &lt;/first&gt; &lt;last&gt; &lt;value-of select =&quot; translate (/person/name/last, $ lowercase, $ uppercase) &quot;&gt; &lt;/value-of&gt; &lt;/last&gt; &lt;/Name&gt; &lt;/person&gt; &lt;/template&gt; &lt;/stylesheet&gt; </xmp>
Inspiration
What can I think of from the above method? Encrypt data! By the way, you can encrypt XML data.
Example:
<xmp> &lt;Variable name = &quot;Alphanumeric&quot;&gt; classification &lt;br/&gt; &lt;/variable&gt; &lt;variable name = &quot;encoded&quot;&gt; &lt;br/&gt; classification &lt;br/&gt; &lt;/variable&gt; &lt;foo&gt; &lt;value-of select = &quot;translate (&#39;this is a test &#39;, $ alphanumeric, $ encoded) &quot;&gt; &lt;/value-of&gt; &lt;/Foo&gt; </xmp>
The encrypted result is as follows:
<xmp> &lt;Foo&gt; 8snr6nr6k6nzrn &lt;/Foo&gt; </xmp>
Decryption is also very simple. You only need to reverse the two variables.
<XMP> <? XML version = "1.0" encoding = "gb2312"?> <Br/> <stylesheet xmlns: XSL = "http://www.w3.org/1999/XSL/Transform" version = "1.0"> <variable name = "Alphanumeric"> inline </variable> <variable name = "encoded"> inline </variable> <template match = "/"> <Foo> <value-of select = "translate ('this is a test ', $ alphanumeric, $ encoded) "> </value-of> </Foo> <br/> | <br/> <Foo> <value-of select =" translate ('8ys5 S5 6 rf5r ', $ encoded, $ alphanumeric) "> </value-of> </Foo> </template> </stylesheet> </XMP>