0

here is my xsl (xslt 1.0)

<tr>
    <td align="center" height="60">
          <input type="hidden" id="imageA" value="{Item}"/>
      <div id="barcode" class="barcode"></div>
    </td>
</tr>

The value="{Item}" is a string.

item1;item2;item3;item4;

How to get the 'item3' only?

This will be passed into JavaScript to generate barcode. Currently the barcode is able to show for simple string.

Mathias Müller
  • 22,203
  • 13
  • 58
  • 75
king jia
  • 692
  • 8
  • 20
  • 43
  • do you know the contents before hand or the contents can be dynamic and you need to pick the third word?? – Saurav Jan 02 '15 at 05:55
  • Are you asking how to extract the **third** item, or how to extract the **Nth** item in general? The answer can be different for each case (as [I have told you before](http://stackoverflow.com/questions/27561834/split-a-tag-inside-foreach#comment43555445_27561925)). – michael.hor257k Jan 02 '15 at 06:46
  • Yeah, only the third item... btw, I solved it by passing the whole 'item' into javascript and extract inside function. XSLT always confuse me... :( – king jia Jan 02 '15 at 07:33

2 Answers2

2

You can use:

substring-before(substring-after(substring-after(Item, ';'), ';'), ';')

to extract the third value of a semicolon separated string.

Alternatively, if using the Xalan-J processor (or another processor supporting the EXSLT str:tokenize() function):

str:tokenize(Item, ';')[3]
michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
1

XSLT 1.0 allows only immutable variables. You can use recursion on a variable that contains the data of your "value" attribute.

  <xsl:template match="/*">
    <root>
      <xsl:variable name="data" select="td/input/@value"/>
      <xsl:call-template name="find">
        <xsl:with-param name="count" select="1"/>
        <xsl:with-param name="data" select="substring-after($data,';')"/>
        <xsl:with-param name="prevData" select="substring-before($data,';')"/>
      </xsl:call-template>
    </root>
  </xsl:template>
  <xsl:template name="find">
    <xsl:param name="count"/>
    <xsl:param name="data"/>
    <xsl:param name="prevData"/>
    <xsl:if test="$count=3">
      <data>
        <xsl:value-of select="$prevData"/>
      </data>
    </xsl:if>
    <xsl:if test="$count &lt; 3">
      <xsl:call-template name="find">
        <xsl:with-param name="count" select="$count+1"/>
        <xsl:with-param name="data" select="substring-after($data,';')"/>
        <xsl:with-param name="prevData" select="substring-before($data,';')"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>

Hope this helps If third is word isn't what you require everytime then you need the data that tells which word you need and accordingly change the number in find template to suit your need

Saurav
  • 592
  • 4
  • 21