Search code examples
xmlxsltxslt-2.0

Need to insert symbol inbetween the particular instance


I want to insert underscore symbol inbetween the link and created as different attribute value:

My Input xml is:

<img imageid="36" alt="water" height="250" width="400" 
class="right" src="https://irctc.com/Services/Gets/Contents/imagesv1/Images/water" />

XSL I used as:

  <xsl:template match="img">
    <xsl:element name="image">
      <xsl:attribute name="id">
        <xsl:value-of select="@imageid"/>
      </xsl:attribute>
      <xsl:attribute name="alt">
        <xsl:value-of select="@alt"/>
      </xsl:attribute>
      <xsl:attribute name="height">
        <xsl:value-of select="@height"/>
      </xsl:attribute>
      <xsl:attribute name="width">
        <xsl:value-of select="@width"/>
      </xsl:attribute>
      <xsl:attribute name="align">
        <xsl:value-of select="@class"/>
      </xsl:attribute>
      <xsl:attribute name="href">
        <xsl:value-of select="@src"/>
      </xsl:attribute>
    </xsl:element>
  </xsl:template>

Output i got as:

 <image id="36"
 alt="water"
 height="250"
 width="400"
 align="right"
 href="https://irctc.com/Services/Gets/Contents/imagesv1/Images/water"/>

But i need the output as images-v1 needs to come along with the file name and i need underscore symbol inbetween that

Expected output:

 <image id="36"
 alt="water"
 height="250"
 width="400"
 align="right"
 href="imagesv1_water"/>

Please provide suggestion for this. Thanks in advance


Solution

  • change

    <xsl:attribute name="href">
        <xsl:value-of select="@src"/>
      </xsl:attribute>
    

    to

        <xsl:attribute name="href">
                <xsl:value-of select="tokenize(@src, '/')[position() = last()-2 or position() = last()]" separator="_"/>
        </xsl:attribute>