2

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?

Daniel Sim
  • 21
  • 2
  • Are you using XSLT1.0 or XSLT2.0? In XSLT2.0 there is the 'tokenize' function which would make this easier. – Tim C Apr 30 '12 at 06:10

3 Answers3

2

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 
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
0

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>
Kickaha
  • 3,680
  • 6
  • 38
  • 57
0

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?.

Community
  • 1
  • 1
Maestro13
  • 3,656
  • 8
  • 42
  • 72