Daniel Sim
Daniel Sim

Reputation: 21

XSL: Split text attribute based on delimiter

I have the following code.

<xsl:template match="Rel/SPFMarkupFile">
<xsl:for-each select="./SPFReportItem">
    <tr>
        <td>X</td>
        <td><xsl:value-of select='../../../SPFReportItem/ReportAttribute[@AttrName="Name"]/@AttrValue' /></td>
        <td colspan="4" indent="yes">
            <xsl:value-of select='ReportAttribute[@AttrName="SPFMarkupText"]/@AttrValue' />
        </td>   
        <td><xsl:value-of select='ReportAttribute[@AttrName="CreationUser"]/@AttrValue' /></td>
        <td colspan="2">N/A</td>
        <td><xsl:value-of select='ReportAttribute[@AttrName="SPFMarkupType"]/@AttrValue' /></td>
    </tr>
</xsl:for-each>

For <xsl:value-of select='ReportAttribute[@AttrName="SPFMarkupText"]/@AttrValue' />

The value for example could be test123~test2~test4~test1 The delimiter is ~

I want to split that value and put it into different rows. So I want it to be like

test123

test2

test4

test1

For the other columns, it will be the same values.

How can I achieve this?

Upvotes: 2

Views: 1967

Answers (3)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243459

The value for example could be test123~test2~test4~test1 The delimiter is ~

I want to split that value and put it into different rows.

Use:

translate(., '~', '&#xA;')

The evaluation of this XPath expression produces from the string value of the current node another string in which every occurence of the ~ character is replaced by a NL (new line) character.

Demo:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:template match="/*">
     <xsl:value-of select="translate(., '~', '&#xA;')"/>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on this XML document:

<t>test123~test2~test4~test1 </t>

the wanted, correct result is produced:

test123
test2
test4
test1 

II. XSLT 2.0 solution:

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:template match="/*">
     <xsl:value-of select="tokenize(., '~')" separator="&#xA;"/>
 </xsl:template>
</xsl:stylesheet>

When applied on the same XML document (above), the wanted, correct result is produced:

test123
test2
test4
test1 

Upvotes: 2

Maestro13
Maestro13

Reputation: 3696

Split the value into parts by using tokenize function in XSLT 2.0 or use the very good answer by Martin Honnen to one of my own questions when using XSLT 1.0 : How can make an XSLT Javascript extension function return a node-set?.

Upvotes: 0

Kickaha
Kickaha

Reputation: 3857

A list of conditional statements should work for you

<xsl:if test="'ReportAttribute[@AttrName="SPFMarkupText"]/@AttrValue'='Test123'">
</xsl:if>
<xsl:if test="'ReportAttribute[@AttrName="SPFMarkupText"]/@AttrValue'='Test1'">
</xsl:if>
<xsl:if test="'ReportAttribute[@AttrName="SPFMarkupText"]/@AttrValue'='Test2'">
</xsl:if>
<xsl:if test="'ReportAttribute[@AttrName="SPFMarkupText"]/@AttrValue'='Test3'">
</xsl:if>

Upvotes: 0

Related Questions