XML/XSLT stylesheet/extension — различия между версиями

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

Текущая версия на 08:26, 26 мая 2010

Define function in VBscript

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0" xmlns:extra="urn:extra-functions">
  <msxsl:script xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    language="VBScript" implements-prefix="extra">
    Function ToMillimetres(inches) ToMillimetres = inches * 25.4 End
    Function
  </msxsl:script>
  <xsl:output method="html" />
  <xsl:template match="/">
    <html>
      <body>
        <p>
          <xsl:apply-templates />
        </p>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="inches">
    <xsl:text> </xsl:text>
    <xsl:value-of
      select="format-number(extra:ToMillimetres(number(.)),
                         "0.00")" />
    <xsl:text>mm</xsl:text>
  </xsl:template>
</xsl:stylesheet>
Output:
<html xmlns:extra="urn:extra-functions">
   <body>
      <p>2.0</p>
   </body>
</html>



Java extension

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0" xmlns:FileReader="java:java.io.FileReader"
  xmlns:BufferedReader="java:java.io.BufferedReader"
  exclude-result-prefixes="FileReader BufferedReader">
  <xsl:output indent="yes" />
  <xsl:param name="filename" />
  <xsl:template name="main">
    <xsl:variable name="reader"
      select="BufferedReader:new( FileReader:new($filename))" />
    <xsl:call-template name="read-addresses">
      <xsl:with-param name="reader" select="$reader" />
    </xsl:call-template>
  </xsl:template>
  <xsl:template name="read-addresses">
    <xsl:param name="reader" />
    <xsl:variable name="line1"
      select="BufferedReader:readLine($reader)" />
    <xsl:if test="$line1 != "****"">
      <xsl:variable name="line2"
        select="BufferedReader:readLine($reader)" />
      <xsl:variable name="line3"
        select="BufferedReader:readLine($reader)" />
      <xsl:variable name="line4"
        select="BufferedReader:readLine($reader)" />
      <xsl:variable name="line5"
        select="BufferedReader:readLine($reader)" />
      <label>
        <address>
          <xsl:value-of select="$line3" />
          <br />
          <xsl:value-of select="$line4" />
          <br />
        </address>
        <recipient>
          Attn:
          <xsl:value-of select="$line2" />
        </recipient>
      </label>
      <xsl:call-template name="read-addresses">
        <xsl:with-param name="reader" select="$reader" />
      </xsl:call-template>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
  
    XSLT
    Java
    C++
    SQL
    Oracle
    Ava"s Man



Use math function from Java library

File: Data.xml
<?xml version="1.0"?>
<number>2.0</number>
File: Transform.xslt

<?xml version="1.0"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="xs" version="2.0">
  <xsl:template match="number">
    <result>
      <xsl:value-of select="Math:sqrt(xs:double(.))"
        xmlns:Math="java:java.lang.Math" />
    </result>
  </xsl:template>
</xsl:transform>
Output:
<?xml version="1.0" encoding="UTF-8"?><result>1.4142135623730951</result>