简体   繁体   中英

how to Split random string with coma(,) in XSLT 1.0

I need to split string with coma and output something like below.

input 1:
<xsl:variable name="weekdays" select="sunday,tuesday,wednesday">

input 2:
<xsl:variable name="weekdays" select="sunday,thursday,friday,saturday">

Input might contain random weekdays. I need output something like below.

output 1:
<weekday>
sunday
Tuesday
wednesday
</weekday>

output 2:
<weekday>
sunday
Thursday
Friday
Saturday
</weekday>

Can someone help on this.

In order to achieve this, your input XML should contain the comma separated values in one of the elements or you will have to prepare the value of the <xsl:variable> accordingly. The syntax for <xsl:variable> shown in your question is incorrect.

Input XML

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<weekdays>Sunday,Tuesday,Wednesday</weekdays>

In the XSL, you can use a variable to hold the value from the element and use the translate function to replace the comma with the newline characters.

Transformation XSL

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
    <xsl:strip-space elements="*" />
    <xsl:variable name="weekday-names" select="weekdays" />
    <xsl:template match="/">
        <output>
            <xsl:value-of select="translate($weekday-names,',','&#xA;')" />
        </output>
    </xsl:template>
</xsl:stylesheet>

Output XML

<?xml version="1.0" encoding="UTF-8"?>
<output>Sunday
    Tuesday
    Wednesday
</output>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM