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

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

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

Compare number value

File: Data.xml
<poem author="jm" year="1667">
  <verse>line 1</verse>
  <verse>line 2</verse>
  <verse>line 3</verse>
  <verse>line 4</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="poem">
    <xsl:choose>
      <xsl:when test="@year &lt; 1638">
        The poem is one of Milton"s earlier works.
      </xsl:when>
      <xsl:when test="@year &lt; 1650">
        The poem is from Milton"s middle period.
      </xsl:when>
      <xsl:when test="@year &lt; 1668">
        The poem is one of Milton"s later works.
      </xsl:when>
      <xsl:when test="@year &lt; 1675">
        The poem is one of Milton"s last works.
      </xsl:when>
      <xsl:otherwise>
        The poem was written after Milton"s death.
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
</xsl:stylesheet>
Output:

        The poem is one of Milton"s later works.



Comparison operator

File: Data.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<booklist>
   <book>
      <title>title 1</title>
      <author>author 1</author>
      <publisher>publisher 1</publisher>
      <isbn>1-11-11111-1</isbn>
      <price>6.99</price>
      <sales>235</sales>
   </book>
   <book>
      <title>title 2</title>
      <author>author 2</author>
      <publisher>publisher 2</publisher>
      <isbn>0 14 018967 X</isbn>
      <price>12.99</price>
      <sales>12</sales>
   </book>
</booklist>

File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet 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 name="minMax">
    <xsl:param name="nodes" as="node()*" />
    <xsl:param name="min-so-far" select="number("INF")"
      as="xs:double" />
    <xsl:param name="max-so-far" select="number("-INF")"
      as="xs:double" />
    <xsl:choose>
      <xsl:when test="count($nodes) gt 1">
        <xsl:variable name="first">
          <xsl:call-template name="minMax">
            <xsl:with-param name="nodes"
              select="subsequence($nodes, 1, count($nodes) idiv 2)" />
          </xsl:call-template>
        </xsl:variable>
        <xsl:variable name="second">
          <xsl:call-template name="minMax">
            <xsl:with-param name="nodes"
              select="subsequence($nodes, (count($nodes) idiv 2) + 1)" />
          </xsl:call-template>
        </xsl:variable>
        <xsl:document>
          <min>
            <xsl:value-of
              select="if(number($first/min) lt number($second/min))
                                    then $first/min
                                    else $second/min" />
          </min>
          <max>
            <xsl:value-of
              select="if(number($first/max) gt number($second/max))
                                    then $first/max
                                    else $second/max" />
          </max>
        </xsl:document>
      </xsl:when>
      <xsl:when test="count($nodes) eq 1">
        <xsl:document>
          <min>
            <xsl:value-of select="$nodes[1]" />
          </min>
          <max>
            <xsl:value-of select="$nodes[1]" />
          </max>
        </xsl:document>
      </xsl:when>
      <xsl:otherwise>
        <xsl:document>
          <min>INF</min>
          <max>-INF</max>
        </xsl:document>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template match="/">
    <result>
      <xsl:call-template name="minMax">
        <xsl:with-param name="nodes"
          select="/booklist/book/price" />
      </xsl:call-template>
    </result>
  </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><result><min>6.99</min><max>12.99</max></result>



if test="position()!=last()"

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<emailList>
  <head:header xmlns:head="http://www.domain.ru/namespace/header">
    <title>Email List</title>
    <maintainer>Joe</maintainer>
  </head:header>
  <person type="personal" id="p001">
    <name>person1</name>
    <email>p@hotmail.ru</email>
  </person>
  <person type="work" id="p002">
    <name>person2</name>
    <email>p@hotmail.ru</email>
  </person>
  <person type="personal" id="p003">
    <name>person3</name>
    <email>p3@hotmail.ru</email>
  </person>
</emailList>
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:output method="html" indent="yes" />
   <xsl:template match="/">
      <p>Your email list consists of the following individuals:
      <xsl:for-each select="emailList/person">
         <xsl:if test="position()!=last()">
         <xsl:apply-templates select="name" />, </xsl:if>
         <xsl:if test="position()=last()">
         <xsl:apply-templates select="name" />.</xsl:if>
         
      </xsl:for-each>
      </p>
   </xsl:template>
   <xsl:template match="name">
      <xsl:value-of select="." />
   </xsl:template>
</xsl:stylesheet>
Output:
<p>Your email list consists of the following individuals:
   person1, person2, person3.
</p>