Split characters in XML + XSL

Source: Internet
Author: User

A few days ago, there is a problem on the forum (see: http://community.csdn.net/Expert/topic/3237/3237160.xml? Temp =. 6712152)

To solve this problem, we mainly apply two functions in XPath: substring-before; substring-after

An example is as follows:

Test. xml:

<? XML version = "1.0" encoding = "UTF-8"?>
<Data_info cc_id = '2013'/>

-----------------------------------------------------------------------

Test. XSL:

<? XML version = "1.0" encoding = "UTF-8"?>
<XSL: stylesheet version = "1.0" xmlns: XSL = "http://www.w3.org/1999/XSL/Transform">
<XSL: Output indent = "yes" method = "XML" encoding = "UTF-8"/>
<XSL: template match = "/data_info">
<XSL: element name = "{name ()}">
<XSL: Call-Template Name = "splitstr">
<XSL: With-Param name = "str" select = "@ cc_id"/>
<XSL: With-Param name = "ctrlname" select = "'CC _ id'"/>
</XSL: Call-template>
</XSL: Element>
</XSL: Template>
<XSL: Template Name = "splitstr">
<XSL: Param name = "str"/>
<XSL: Param name = "ctrlname"/>
<XSL: element name = "input">
<XSL: attribute name = "name"> <XSL: value-of select = "$ ctrlname"/> </XSL: attribute>
<XSL: attribute name = "value">
<XSL: Choose>
<XSL: When test = "contains ($ STR, ',')"> <XSL: value-of select = "substring-before ($ STR ,',') "/> </XSL: When>
<XSL: otherwise> <XSL: value-of select = "$ Str"/> </XSL: otherwise>
</XSL: Choose>
</XSL: attribute>
</XSL: Element>
<XSL: If test = "contains ($ STR, ',')">
<XSL: Call-Template Name = "splitstr">
<XSL: With-Param name = "str" select = "substring-after ($ STR, ',')"/>
<XSL: With-Param name = "ctrlname" select = "$ ctrlname"/>
</XSL: Call-template>
</XSL: If>
</XSL: Template>
</XSL: stylesheet>

--------------------------------------------------------------------------------

Conversion Result:

<? XML version = "1.0" encoding = "UTF-8"?>
<Data_info>
<Input name = "cc_id" value = "195"/>
<Input name = "cc_id" value = "196"/>
<Input name = "cc_id" value = "197"/>
<Input name = "cc_id" value = "198"/>
</Data_info>

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.