0

I have a xml below

<Report>
<rl>
    <id>12345;12346</id>
    <activity>a2/a3</activity>
    <result>r2/r3</result>
    <operator>test</operator>
    <timestamp>12/18/2014 3:51:19 PM</timestamp>
    <quantity>2</quantity>
</rl>
<rl>
    <id>22345;22346</id>
    <activity>a3/a4</activity>
    <result>r3/r4</result>
    <operator>test</operator>
    <timestamp>12/18/2014 3:51:19 PM</timestamp>
    <quantity>2</quantity>
</rl>
</Report>

and for my xsl,

<table border="1" style="border-width: 1px" width="90%" bordercolor="#C0C0C0" align="center">
    <tr>
        <th width="5%" align="center">
          <font color="#000000" face="Verdana" size="3">Index</font>
        </th>              
        <th width="15%" align="center">
          <font color="#000000" face="Verdana" size="3">ID A:</font>
        </th>
        <th width="15%" align="center">
          <font color="#000000" face="Verdana" size="3">ID B:</font>
        </th>
    </tr>
    <xsl:for-each select="Report/rl">
    <tr height="25">  
     <td width="5%" align="center" >
       <font color="#000000" face="Verdana" size="2">
         <xsl:value-of select="position()" />          
       </font>
     </td> 
     <td align="center">
       <font color="#000000" face="Verdana" size="2">
         <xsl:value-of select="idA" />
       </font>
     </td>
     <td align="center">
       <font color="#000000" face="Verdana" size="2">
         <xsl:value-of select="idB" />
       </font>
     </td>
    </tr>
  </xsl:for-each>
</table>

For 1st rl, there is 12345:12346 for tag, I want to split them into 12345 and 12346 and show them in the 'idA' and 'idB'. How should I do that?

My xslt version is 1.0.

king jia
  • 692
  • 8
  • 20
  • 43
  • Take a look here http://stackoverflow.com/questions/4845660/xsl-how-to-split-strings – fly_ua Dec 19 '14 at 07:57
  • yes, I did check the post, however, in foreach loop, i cant manage to do it – king jia Dec 19 '14 at 07:58
  • Did you create XSL template and call it via ? – fly_ua Dec 19 '14 at 08:00
  • yes, for this testing, but for my case, no matter i put the , my html will show nothing... This is my first time to create xsl..and There is limitation for me to create new tag inside the xml (code-based conversion from plain text to XML and I cannot change the code.) – king jia Dec 19 '14 at 08:04

2 Answers2

1

Assuming there are always exactly two values, separated by a semicolon, use:

<xsl:value-of select="substring-before(id, ';')"/>

to populate the idA cell, and:

<xsl:value-of select="substring-after(id, ';')"/>

to populate the idB cell.


Added:

For the same example as posted, can you elaborate more about the 'recursive named template'?

The solution using a recursive named template would look something like this:

XSLT 1.0

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:template match="/Report">
    <table border="1">
        <tr>
            <th>Index</th>              
            <th>ID A</th>
            <th>ID B</th>
        </tr>
        <xsl:apply-templates select="rl"/>
    </table>
</xsl:template>

<xsl:template match="rl">
    <tr>
        <td>
            <xsl:value-of select="position()" />          
        </td>   
        <xsl:call-template name="tokenize">
            <xsl:with-param name="text" select="id"/>
        </xsl:call-template>
    </tr>
</xsl:template>

<xsl:template name="tokenize">
    <xsl:param name="text"/>
    <xsl:param name="delimiter" select="';'"/>
    <td>
        <xsl:value-of select="substring-before(concat($text, $delimiter), $delimiter)"/>
    </td>
    <xsl:if test="contains($text, $delimiter)">
        <!-- recursive call -->
        <xsl:call-template name="tokenize">
            <xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>

</xsl:stylesheet>

Note that for the header we assume that the number of columns is known beforehand. Otherwise you'd have to use a similar recursive template to generate the header cells too.

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
  • If there is more than 2 values, what should I change? – king jia Dec 19 '14 at 08:40
  • Where would you put them? Your table has room for only two. – michael.hor257k Dec 19 '14 at 08:43
  • In future I might add new columns... ID C: , but I have limitation on changing the conversion code. So I have only this way to populate the data – king jia Dec 19 '14 at 10:00
  • It depends: if you plan to have more than three columns, then you should definitely switch to using a recursive named template. Three is a borderline case: if you will always have exactly three values, then you can still use substring-before() and substring-after() to extract each one specifically. -- I am not sure what you mean by "*I have limitation on changing the conversion code*". Adding a column to the output table changes the conversion code just as much as changing the formula for extracting the values does. – michael.hor257k Dec 19 '14 at 11:30
  • the limitation is I cannot add more tag in the XML. I can only use currently tags to populate the data, if there are more data need to be populated, I can only use ';'. For the same example as posted, can you elaborate more about the 'recursive named template'? – king jia Dec 22 '14 at 01:45
0

Check this example

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="*">
          <xsl:for-each select="rl">
            <node>
            <idA>
                <xsl:value-of select="substring-before(id,';')"/>
            </idA>
            <idB>
              <xsl:value-of select="substring-after(id,';')"/>
            </idB>
            </node>
          </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>
fly_ua
  • 1,034
  • 8
  • 12