XML/XSLT stylesheet/substring before

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

substring-before 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="substring-before(.,"dreary")" />
  </xsl:template>
  
</xsl:stylesheet>



substring-before() function

File: Data.xml 
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="Transform.xslt" ?>
<neighbours>
  <planet name="Venus">
    <description>
    description
    </description>
    <positionFromSun>2</positionFromSun>
    <diameter> 12104 km (7505 miles)</diameter>
    <moons> 0</moons>
    <meanTemp> 482C (900F)</meanTemp>
    <oneDay> 243.01 earth days</oneDay>
    <oneYear> 224.7 earth days</oneYear>
  </planet>
</neighbours>
File: Transform.xslt
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:import href="planetsToXHTML.xsl"/>
  <xsl:output method="html" version="4.0" indent="yes"/>
  <xsl:template match="neighbours">
    <html>
      <head>
        <title>Sorted planets</title>
        <style type="text/css">
          body { font-family: Verdana, Arial, sans-serif; font-size:12px;}
      </style>
      </head>
      <body>
        <h1>My sorted list of planets</h1>
        <xsl:apply-templates>
          <xsl:sort select="substring-before(meanTemp/text(), "C")" order="ascending" data-type="number"/>
        </xsl:apply-templates>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>
Output:
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>Sorted planets</title><style type="text/css">
          body { font-family: Verdana, Arial, sans-serif; font-size:12px;}
      </style></head>
   <body>
      <h1>My sorted list of planets</h1>
      
      
      
      
      description
      
      2
       12104 km (7505 miles)
       0
       482C (900F)
       243.01 earth days
       224.7 earth days
      
   </body>
</html>