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

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

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

Call a template with parameter

   <source lang="xml">

File: Data.xml <?xml version="1.0"?> <programme>

  <opera>
     <title>A</title>
     <composer>B</composer>
     <date>1791</date>
  </opera>
  <composer name="Mozart">
     <fullname>Mozart</fullname>
     <born>1756</born>
     <died>1791</died>
  </composer>

</programme> File: Transform.xslt <?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

 version="1.0">
 <xsl:template match="/">
   <xsl:variable name="list">
     <xsl:call-template name="make-list">
       <xsl:with-param name="names"
         select="/programme/composer/fullname" />
     </xsl:call-template>
   </xsl:variable>
   This week"s composers are:
   <xsl:value-of select="translate($list, ",", ";")" />
 </xsl:template>
 <xsl:template name="make-list">
   <xsl:param name="names" />
   <xsl:for-each select="$names">
     <xsl:value-of select="." />
     <xsl:if test="position()!=last()">,</xsl:if>
     <xsl:if test="position()=last()-1">and</xsl:if>
   </xsl:for-each>
 </xsl:template>

</xsl:stylesheet>

Output: <?xml version="1.0" encoding="UTF-8"?>

   This week"s composers are:
   
</source>
   
  


Define and use template

   <source lang="xml">

File: Data.xml <poem>

  <author>author 1</author>
  <date>1912</date>
  <title>Song</title>
 <stanza>
     <line>line 1</line>
     <line>line 2</line>
     <line>line 3</line>
     <line>line 4</line>
  </stanza>
  <stanza>
     <line>line 5</line>
     <line>line 6</line>
     <line>line 7</line>
     <line>line 8</line>
  </stanza>
  <stanza>
     <line>line 9</line>
     <line>line 10</line>
     <line>line 11</line>
     <line>line 12</line>
  </stanza>

</poem> File: Transform.xslt <?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

 version="1.0">
 <xsl:template match="poem">
   <html>
     <head>
       <title>
         <xsl:value-of select="title" />
       </title>
     </head>
     <body>
       <xsl:apply-templates select="title" />
       <xsl:apply-templates select="author" />
       <xsl:apply-templates select="stanza" />
       <xsl:apply-templates select="date" />
     </body>
   </html>
 </xsl:template>
 <xsl:template match="title">

<xsl:value-of select="." />

 </xsl:template>
 <xsl:template match="author">

By <xsl:value-of select="." />

 </xsl:template>
 <xsl:template match="date">

<xsl:value-of select="." />

 </xsl:template>
 <xsl:template match="stanza">

<xsl:apply-templates select="line" />

 </xsl:template>
 <xsl:template match="line">
   <xsl:if test="position() mod 2 = 0">  </xsl:if>
   <xsl:value-of select="." />
   
</xsl:template>

</xsl:stylesheet> Output: <html>

  <head>
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     <title>Song</title>
  </head>
  <body>

Song

By author 1

line 1
  line 2
line 3
  line 4

line 5
  line 6
line 7
  line 8

line 9
  line 10
line 11
  line 12

1912

  </body>

</html>

</source>
   
  


Get two values in one template

   <source lang="xml">

File: Data.xml <?xml version="1.0"?> <people>

 <person born="1912" died="1954">
   <name>
     <first_name>A</first_name>
     <last_name>B</last_name>
   </name>
   <profession>C</profession>
   <profession>D</profession>
   <profession>E</profession>
 </person>
 <person born="2008" died="2008">
   <name>
     <first_name>F</first_name>
     <middle_initial>G</middle_initial>
     <last_name>H</last_name>
   </name>
   <profession>I</profession>
   <hobby>J</hobby>
 </person>

</people>

File: Transform.xslt <?xml version="1.0"?> <xsl:stylesheet version="1.0"

               xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:template match="people">
   <html>
     <head><title>Famous Scientists</title></head>
     <body>
       <xsl:apply-templates/>
     </body>
   </html>
 </xsl:template>
 <xsl:template match="name">

<xsl:value-of select="last_name"/>, <xsl:value-of select="first_name"/>

 </xsl:template>
 <xsl:template match="person">
   <xsl:apply-templates select="name"/>
 </xsl:template>

</xsl:stylesheet> Output: <html>

  <head>
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     <title>Famous Scientists</title>
  </head>
  <body>
     
     

B, A


H, F


  </body>

</html>

</source>
   
  


Locate parent tags and get value from children tags

   <source lang="xml">

File: Data.xml

<?xml version="1.0"?> <emailList>

 <person>
   <name>name 1</name>
   <email>g@gmail.ru</email>
 </person>
 <person>
   <name>name 2</name>
   <email>n@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:template match="/">
   <html>
     <head>
       <title>Email Listing</title>
     </head>
     <body>
<xsl:apply-templates/>
Name E-mail Address
     </body>
   </html>
 </xsl:template>
 <xsl:template match="person">
   <tr>
     <td><xsl:value-of select="name"/></td>
     <td><xsl:value-of select="email"/></td>
   </tr>
 </xsl:template>

</xsl:stylesheet> Output: <html>

  <head>
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     <title>Email Listing</title>
  </head>
  <body>
Name E-mail Address
name 1 g@gmail.ru
name 2 n@hotmail.ru
  </body>

</html>

</source>
   
  


match and get value operations with namespace

   <source lang="xml">

File: Data.xml <?xml version="1.0"?> <people>

 <person born="1912" died="1954">
   <name>
     <first_name>A</first_name>
     <last_name>B</last_name>
   </name>
   <profession>C</profession>
   <profession>D</profession>
   <profession>E</profession>
 </person>
 <person born="2008" died="2008">
   <name>
     <first_name>F</first_name>
     <middle_initial>G</middle_initial>
     <last_name>H</last_name>
   </name>
   <profession>I</profession>
   <hobby>J</hobby>
 </person>

</people>

File: Transform.xslt <?xml version="1.0"?> <xsl:stylesheet version="1.0"

               xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
               xmlns:pe="http://www.cafeconleche.org/namespaces/people">
    
 <xsl:template match="pe:people">
   <html>
     <head><title>Famous Scientists</title></head>
     <body>
       <xsl:apply-templates/>
     </body>
   </html>
 </xsl:template>
    
 <xsl:template match="pe:name">

<xsl:value-of select="pe:last_name"/>, <xsl:value-of select="pe:first_name"/>

 </xsl:template>
    
 <xsl:template match="pe:person">
   <xsl:apply-templates select="pe:name"/>
 </xsl:template>
    

</xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?>


     A
     B
   
   C
   D
   E
 
 
   
     F
     G
     H
   
   I
   J
 
</source>
   
  


match an element

   <source lang="xml">

File: Data.xml <?xml version="1.0"?> <people>

 <person born="1912" died="1954">
   <name>
     <first_name>A</first_name>
     <last_name>B</last_name>
   </name>
   <profession>C</profession>
   <profession>D</profession>
   <profession>E</profession>
 </person>
 <person born="2008" died="2008">
   <name>
     <first_name>F</first_name>
     <middle_initial>G</middle_initial>
     <last_name>H</last_name>
   </name>
   <profession>I</profession>
   <hobby>J</hobby>
 </person>

</people>

File: Transform.xslt <?xml version="1.0"?> <xsl:stylesheet version="1.0"

               xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:template match="person">A Person</xsl:template>

</xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?>

 A Person
 A Person
</source>
   
  


output a table without loop

   <source lang="xml">

File: Data.xml <?xml version="1.0" ?> <customer-list>

 <customer>
   <name>
     <first>A</first>
     <last>B</last>
   </name>
   <order>C</order>
   <order>D</order>
 </customer>

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

 <xsl:template match="/">
   <HTML>
     <HEAD>
       <TITLE>Customers</TITLE>
     </HEAD>
     <BODY>
<xsl:apply-templates select="customer-list/customer" />
     </BODY>
   </HTML>
 </xsl:template>
 <xsl:template match="customer">
   <TR><TH ALIGN="left">
     <xsl:apply-templates select="name"/>
   </TH></TR>
   <xsl:apply-templates select="order"/>
 </xsl:template>
 <xsl:template match="order">
   <TR><TD>
     <xsl:apply-templates/>
   </TD></TR>
 </xsl:template>
 <xsl:template match="name">
   <xsl:value-of select="last" />, 
   <xsl:value-of select="first" />
 </xsl:template>

</xsl:stylesheet> Output: <HTML>

  <HEAD>
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     <TITLE>Customers</TITLE>
  </HEAD>
  <BODY>
B,
                  A
C
D
  </BODY>

</HTML>

</source>
   
  


output in template

   <source lang="xml">

File: Data.xml <?xml version="1.0"?> <people>

 <person born="1912" died="1954">
   <name>
     <first_name>A</first_name>
     <last_name>B</last_name>
   </name>
   <profession>C</profession>
   <profession>D</profession>
   <profession>E</profession>
 </person>
 <person born="2008" died="2008">
   <name>
     <first_name>F</first_name>
     <middle_initial>G</middle_initial>
     <last_name>H</last_name>
   </name>
   <profession>I</profession>
   <hobby>J</hobby>
 </person>

</people>

File: Transform.xslt <?xml version="1.0"?> <xsl:stylesheet version="1.0"

               xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:template match="person">

A Person

 </xsl:template>

</xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?>

A Person

A Person

</source>
   
  


Select value from an element with value-of

   <source lang="xml">

File: Data.xml <?xml version="1.0"?> <people>

 <person born="1912" died="1954">
   <name>
     <first_name>A</first_name>
     <last_name>B</last_name>
   </name>
   <profession>C</profession>
   <profession>D</profession>
   <profession>E</profession>
 </person>
 <person born="2008" died="2008">
   <name>
     <first_name>F</first_name>
     <middle_initial>G</middle_initial>
     <last_name>H</last_name>
   </name>
   <profession>I</profession>
   <hobby>J</hobby>
 </person>

</people>

File: Transform.xslt <?xml version="1.0"?> <xsl:stylesheet version="1.0"

               xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:template match="person">

<xsl:value-of select="name"/>

 </xsl:template>

</xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?>

A B

F G H

</source>
   
  


set match mode to fulltext

   <source lang="xml">

File: Data.xml <?xml version="1.0"?> <Book>

 <Title>this is the title</Title>
 <Authors>
   <Author>A</Author>
   <Author>B</Author>
   <Author>C</Author>
 </Authors>
 <Year>2007</Year>
 <Chapters>
   <Chapter number="1" title="title 1">chapter 1</Chapter>
   <Chapter number="2" title="title 2">chapter 2</Chapter>
   <Chapter number="3" title="title 3">chapter 3</Chapter>
   <Chapter number="4" title="title 4">chapter 4</Chapter>
   <Chapter number="5" title="title 5">chapter 5</Chapter>
 </Chapters>

</Book>

File: Transform.xslt

<?xml version="1.0"?> <xsl:stylesheet

    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="1.0">
   
   <xsl:template match="Chapter" mode="fulltext">

<xsl:value-of select="@number" />. <xsl:value-of select="@title" />

       <paragraph>
          <xsl:value-of select="." />
       </paragraph>
   </xsl:template>

</xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?>

 this is the title
 
   A
   B
   C
 
 2007
 
   chapter 1
   chapter 2
   chapter 3
   chapter 4
   chapter 5
 
</source>
   
  


template mode="index"

   <source lang="xml">

File: Data.xml <?xml version="1.0"?> <cv>

 <para>
   <performance>
     <publication>H</publication>
     G
     <venue>F</venue>
     E
     <group>D</group>
     C
     <date>1998</date>
     B
     <quote>
       A
     </quote>
   </performance>
 </para>

</cv>

File: Transform.xslt <?xml version="1.0"?> <xsl:stylesheet version="1.0"

 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:template match="/">
   <html>
     <body>
       <xsl:apply-templates />
<xsl:apply-templates mode="index" />
             Date
             Venue
             Composer
             Work
             Role
     </body>
   </html>
 </xsl:template>
 <xsl:template match="performance" mode="index">
   <tr>
     <td>
       <xsl:value-of select="date" />
        
     </td>
     <td>
       <xsl:value-of select="venue" />
        
     </td>
     <td>
       <xsl:value-of select="composer" />
        
     </td>
     <td>
       <xsl:value-of select="work" />
        
     </td>
     <td>
       <xsl:value-of select="role" />
        
     </td>
   </tr>
 </xsl:template>

</xsl:stylesheet> Output: <html>

  <body>
       
         
           H
           G
           F
           E
           D
           C
           1998
           B
           
             A
           
         
       
     
Date Venue Composer Work Role
1998
                       
                    
F
                       
                    
                       
                    
                       
                    
                       
                    
  </body>

</html>

</source>
   
  


template with parameters

   <source lang="xml">

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="get-min-and-max">
   <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="$nodes">
       <xsl:call-template name="get-min-and-max">
         <xsl:with-param name="nodes"
           select="remove($nodes, 1)" />
         <xsl:with-param name="min-so-far"
           select="if (number($nodes[1]) lt $min-so-far) 
                                 then $nodes[1]
                                 else $min-so-far" />
         <xsl:with-param name="max-so-far"
           select="if (number($nodes[1]) gt $max-so-far) 
                                 then $nodes[1]
                                 else $max-so-far" />
       </xsl:call-template>
     </xsl:when>
     <xsl:otherwise>
       <xsl:document>
         <min>
           <xsl:value-of select="$min-so-far" />
         </min>
         <max>
           <xsl:value-of select="$max-so-far" />
         </max>
       </xsl:document>
     </xsl:otherwise>
   </xsl:choose>
 </xsl:template>
 <xsl:template match="/">
   <result>
     <xsl:call-template name="get-min-and-max">
       <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>

</source>