XML/XSLT stylesheet/substring after

Материал из Web эксперт
Перейти к: навигация, поиск

substring-after() and text()

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>



substring-after demo

File: Data.xml
<poem>
  <verse>line 1</verse>
  <verse>
    line 2
  </verse>
</poem>
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="no" />
  <xsl:template match="verse">
    <xsl:value-of select="." />
    <xsl:value-of select="substring-after(.,"desolation")" />
  </xsl:template>
  
</xsl:stylesheet>
Output:

  line 1
  
    line 2