Use XSL for string replacement

Source: Internet
Author: User
Tags stringreplace translate function xml parser xsl

Anyone familiar with XPath knows that using the translate function in XSL can replace a string by character. For example:

Translate ("bar", "ABC", "ABC") will get the bar

Translate ("-- AAA --", "ABC-", "ABC") will get AAA.

However, if you want to replace a substring with another substring, you cannot use translate for xpath1.0.

Generally, there are two methods to replace strings in XSL:

(1) Use MS: script to embed the XSL implementation, but the XML Parser must be MS.

(2) Use a pure XSL template.

Of course, the advantage of using method (1) Is that JavaScript's powerful string processing functions can be fully utilized, for example, regular expressions. Here we use a pure XSL template to replace strings. The source code is as follows, and the following part is an example for testing:

<? XML version = "1.0" encoding = "gb2312"?>
<XSL: stylesheet version = "1.0" xmlns: XSL = "http://www.w3.org/1999/XSL/Transform">
<! -- String replacement template -->
<! --
Copyright copyright ye jiansheng yjs_lh@sohu.com Co., yjs_lh@sina.com.
@ Version 1.0 2005.04.19 on Nanping, Chongqing
-->

<XSL: Template Name = "stringreplace">
<XSL: Param name = "srcstring"/>
<XSL: Param name = "fromstring"/>
<XSL: Param name = "tostring"/>
<XSL: Choose>
<XSL: When test = "contains ($ srcstring, $ fromstring)">
<XSL: value-of select = "substring-before ($ srcstring, $ fromstring)"/>
<XSL: value-of select = "$ tostring"/>
<XSL: Call-Template Name = "stringreplace">
<XSL: With-Param name = "srcstring" select = "substring-after ($ srcstring, $ fromstring)"/>
<XSL: With-Param name = "fromstring" select = "$ fromstring"/>
<XSL: With-Param name = "tostring" select = "$ tostring"/>
</XSL: Call-template>
</XSL: When>
<XSL: otherwise> <XSL: value-of select = "$ srcstring"/> </XSL: otherwise>
</XSL: Choose>
</XSL: Template>
<! -- Only used for testing. You can delete this template when using it. -->
<XSL: template match = "/">
<HTML>
<Body>
<Table border = "1">
<Caption> string replacement function </caption>
<Tr>
<TH> source string </Th>
<TH> fromstring </Th>
<TH> tostring </Th>
<TH> replacement result </Th>
</Tr>
<XSL: For-each select = "// s">
<Tr>
<TD>
<XSL: value-of select = "Source"/>
</TD>
<TD>
<XSL: value-of select = "from"/>
</TD>
<TD>
<XSL: value-of select = "to"/>
</TD>
<TD>
<XSL: Call-Template Name = "stringreplace">
<XSL: With-Param name = "srcstring" select = "Source"/>
<XSL: With-Param name = "fromstring" select = "from"/>
<XSL: With-Param name = "tostring" select = "to"/>
</XSL: Call-template>
</TD>
</Tr>
</XSL: For-each>
</Table>
</Body>
</Html>
</XSL: Template>
</XSL: stylesheet>


XML used for testing:

<? XML version = "1.0" encoding = "gb2312"?>
<Root>
<S>
<Source> asdflaksdjfalsdkfjaslkdjf </source>
<From> A </from>
<To> BBB </to>
</S>
<S>
<Source> asdflaksdjfalsdkfjaslkdjf </source>
<From> flaksdjfals </from>
<To> BBB </to>
</S>
<S>
<Source> asdflaksdjfalsdkfjaslkdjf </source>
<From> djfa </from>
<To> BBB </to>
</S>
<S>
<Source> asdflaksdjfalsdkfjaslkdjf </source>
<From> SDF </from>
<To> vvv </to>
</S>
<S>
<Source> I am not the publisher </source>
<From> Sichuan </from>
<To> Chongqing </to>
</S>
<S>
<Source> select * from table where a = B </source>
<From >=</from>
<To> * = </to>
</S>
</Root>


Conversion Result:

String replacement function
Source string Fromstring Tostring Replacement result
Asdflaksdjfalsdkfjaslkdjf A Bbb Bbbsdflbbbksdjfbbblsdkfjbbbslkdjf
Asdflaksdjfalsdkfjaslkdjf Flaksdjfals Bbb Asdbbbdkfjaslkdjf
Asdflaksdjfalsdkfjaslkdjf Djfa Bbb Asdflaksbbblsdkfjaslkdjf
Asdflaksdjfalsdkfjaslkdjf SDF Vvv Avvvlaksdjfalsdkfjaslkdjf
I am not a publisher Sichuan Chongqing I am not from Chongqing
Select * from table where a = B = * = Select * from table where a * = B

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.