XML data encryption and case-sensitivity conversion using XSL

Source: Internet
Author: User
Tags xsl file

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> <Person> <Name> <first> net_lover </first> <last> xianhui Meng </last> <chinesename> Meng xianhui </chinesename> </Name> </person> </xmp>

Suppose you want to convert to the following format:

Example 2: newperson. xml

<xmp> <Person> <Name> <first> net_lover </first> <last> xianhui Meng </last> <chinesename> Meng xianhui </chinesename> </Name> </person> </xmp>

To solve this conversion problem, we usually use the translate () in the XPath function, for example:

<xmp> <Foo> <value-of select = "translate ('this is a test', 'tis ', 'tis')"> </value-of> </Foo> </xmp>

The conversion result is as follows:

<xmp> <Foo> This is a test </Foo> </xmp>

But how can we convert all the text into uppercase? The following is the process.Code:

Define two variables first:

<xmp> <Variable name = "uppercase"> abcdefghijklmnopqrstuvwxyz </variable> <variable name = "lowercase"> abcdefghijklmnopqrstuvwxyz </variable> </xmp>

Then convert:

<xmp> <Foo> <value-of select = "translate ('this is a test', $ lowercase, $ uppercase)"> </value-of> </Foo> </xmp>

The result is as follows:

<xmp> <Foo> This is a test </Foo> </xmp>

For the problem, you can write the XSL file as follows:

Example 3: person. XSL

<xmp> <? XML version = "1.0" encoding = "gb2312"?> <Br/> <stylesheet xmlns: XSL = "http://www.w3.org/1999/XSL/Transform" version = "1.0"> <variable name = "uppercase"> lower </variable> <variable name = "lowercase"> abcdefghijklmnopqrstuvwxyz </variable> <template match = "/"> <person> <Name> <first> <value-of select = "translate (/person/name/first, $ lowercase, $ uppercase) "> </value-of> </first> <last> <value-of select =" translate (/person/name/last, $ lowercase, $ uppercase) "> </value-of> </last> </Name> </person> </template> </stylesheet> </xmp>

Inspiration

What can I think of from the above method? Encrypt data! By the way, you can encrypt XML data.

Example:

<xmp> <Variable name = "Alphanumeric"> classification <br/> </variable> <variable name = "encoded"> <br/> classification <br/> </variable> <foo> <value-of select = "translate ('this is a test ', $ alphanumeric, $ encoded) "> </value-of> </Foo> </xmp>

The encrypted result is as follows:

<xmp> <Foo> 8snr6nr6k6nzrn </Foo> </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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.