substring with index : substring « XSLT stylesheet « XML






substring with index



File: Data.xml

<winelist>
  <wine>
    <winery>shop 1</winery>
    <product>product 1</product>
    <year>1998</year>
    <price>6.99</price>
    <binCode>15A-7</binCode>
  </wine>
  <wine>
    <winery>shop 2</winery>
    <product>product 2</product>
    <year>1997</year>
    <price>7.55</price>
    <binCode>15C-5</binCode>
  </wine>
  <wine>
    <winery>shop 1</winery>
    <product>product 1</product>
    <year>1996</year>
    <price>14.99</price>
    <binCode>12D-1</binCode>
  </wine>
</winelist>

File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">

  <xsl:output method="xml" omit-xml-declaration="yes" indent="yes" />
  <xsl:template match="binCode">
    <productLocation>
      <row>
        <xsl:value-of select="substring(text(),1,2)" />
      </row>
      <shelf>
        <xsl:value-of select="substring(.,3,1)" />
      </shelf>
      <prodNum>
        <xsl:value-of select="substring-after(text(),'-')" />
      </prodNum>
    </productLocation>
  </xsl:template>

  

  <xsl:template match="*">
    <xsl:copy>
      <xsl:apply-templates />
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
Output:

<winelist>
    <wine>
        <winery>shop 1</winery>
        <product>product 1</product>
        <year>1998</year>
        <price>6.99</price>
        <productLocation>
         <row>15</row>
         <shelf>A</shelf>
         <prodNum>7</prodNum>
      </productLocation>
    </wine>
    <wine>
        <winery>shop 2</winery>
        <product>product 2</product>
        <year>1997</year>
        <price>7.55</price>
        <productLocation>
         <row>15</row>
         <shelf>C</shelf>
         <prodNum>5</prodNum>
      </productLocation>
    </wine>
    <wine>
        <winery>shop 1</winery>
        <product>product 1</product>
        <year>1996</year>
        <price>14.99</price>
        <productLocation>
         <row>12</row>
         <shelf>D</shelf>
         <prodNum>1</prodNum>
      </productLocation>
    </wine>
</winelist>

 








Related examples in the same category

1.substring function