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

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

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

Match among a list of target values

   <source lang="xml">

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

 <book category="reference">
     <author>author1</author>
     <title>title 1</title>
     <price>8.95</price>
  </book>
  <book category="fiction">
     <author>author 2</author>
     <title>title 2</title>
     <price>12.99</price>
  </book>
  <book category="fiction">
     <author>author 3</author>
     <title>title 3</title>
     <price>8.99</price>
  </book>

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

 version="2.0">
 <xsl:template match="books">
   <html>
     <body>

A list of books

<xsl:apply-templates />
     </body>
   </html>
 </xsl:template>
 <xsl:template match="book">
   <tr>
     <td>
       <xsl:number />
     </td>
     <xsl:apply-templates />
   </tr>
 </xsl:template>
 <xsl:template match="author | title | price">
   <td>
     <xsl:value-of select="." />
   </td>
 </xsl:template>

</xsl:stylesheet> Output: <html>

  <body>

A list of books

1 author1 title 1 8.95
2 author 2 title 2 12.99
3 author 3 title 3 8.99
  </body>

</html>

</source>
   
  


match: mode="cast-list"

   <source lang="xml">

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

 <TITLE>title 1</TITLE>
 <STAGEDIR>A</STAGEDIR>
 <SPEECH>
   <SPEAKER>B</SPEAKER>
   <LINE>line 1</LINE>
   <LINE>line 2</LINE>
   <LINE>line 3</LINE>
   <LINE>line 4</LINE>
   <LINE>line 5</LINE>
   <LINE>line 6</LINE>
 </SPEECH>

</SCENE> File: Transform.xsl <?xml version="1.0"?> <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

 version="2.0">
 <xsl:template match="SCENE">
   <html>
     <body>
       <xsl:apply-templates select="TITLE" />
       <xsl:variable name="speakers" as="element()*">
         <xsl:for-each-group select="//SPEAKER"
           group-by=".">
           <xsl:sequence select="current-group()[1]" />
         </xsl:for-each-group>
       </xsl:variable>

Cast: <xsl:apply-templates select="$speakers" mode="cast-list" />

       <xsl:apply-templates select="* except TITLE" />
     </body>
   </html>
 </xsl:template>
 <xsl:template match="SPEAKER" mode="cast-list">
   <xsl:value-of select="." />
   <xsl:if test="not(position()=last())">,</xsl:if>
 </xsl:template>
 <xsl:template match="TITLE">

<xsl:apply-templates />

 </xsl:template>
 <xsl:template match="STAGEDIR">
   
     <xsl:apply-templates />
   
 </xsl:template>
 <xsl:template match="SPEECH">

<xsl:apply-templates />

 </xsl:template>
 <xsl:template match="SPEAKER">
   
     <xsl:apply-templates />
   
   
</xsl:template> <xsl:template match="LINE"> <xsl:apply-templates />
</xsl:template>

</xsl:transform> Output: <html>

  <body>

title 1

Cast: B

A

            B
line 1
line 2
line 3
line 4
line 5
line 6

  </body>

</html>

</source>
   
  


match more than one value

   <source lang="xml">

<?xml version="1.0"?> <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

 version="1.0">
 <xsl:template match="/">
   <html>
     <body>

Stylesheet Module Structure

    <xsl:apply-templates select="*/xsl:include | */xsl:import" />
     </body>
   </html>
 </xsl:template>
 <xsl:template match="xsl:include | xsl:import">
  • <xsl:value-of select="concat(local-name(),"s ",@href)" /> <xsl:variable name="module" select="document(@href)" />
      <xsl:apply-templates select="$module/*/xsl:include | $module/*/xsl:import" />
  •  </xsl:template>
    

    </xsl:transform> Output: <html>

      <body>
    

    Stylesheet Module Structure

        </body>
      

      </html>

      </source>
         
        
      


      Match root

         <source lang="xml">
      

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

         <Name DOB="2008/11/11">
              <FirstName>first name</FirstName>
              <LastName>last name</LastName>
         </Name>
      

      </PersonData>

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

       version="1.0">
       <xsl:template match="/">
         <html>
           <head>
             <title>
               Title:
               <xsl:value-of select="/PersonData/Name/FirstName" />
               <xsl:text></xsl:text>
               <xsl:value-of select="/PersonData/Name/LastName" />
             </title>
           </head>
           <body>
             <paragraph>
               <xsl:value-of select="/PersonData/Name/FirstName" />
               <xsl:text> </xsl:text>
               <xsl:value-of select="/PersonData/Name/LastName" />
               was born on
               <xsl:value-of select="/PersonData/Name/@DOB" />
             </paragraph>
           </body>
         </html>
       </xsl:template>
      

      </xsl:stylesheet>

      Output: <html>

        <head>
           <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
           <title>
                        Title:
                        first namelast name
           </title>
        </head>
        <body>
           <paragraph>first name last name
                        was born on
                        2008/11/11
           </paragraph>
        </body>
      

      </html>

      </source>
         
        
      


      | (or) with level

         <source lang="xml">
      

      File: Data.xml <chapter>

       <title>The Chapter</title>
       <sect1>
         <title>First Section</title>
         <figure>
           <title>First picture in book</title>
           <graphic fileref="pic1.jpg" />
         </figure>
       </sect1>
       <sect1>
         <title>Second Section</title>
         <sect2>
           <title>Second Section, First Subsection</title>
           <figure>
             <title>Second picture in book</title>
             <graphic fileref="pic2.jpg" />
           </figure>
         </sect2>
         <sect2>
           <title>Second Section, Second Subsection</title>
           <para>This one has no figure.</para>
         </sect2>
         <sect2>
           <title>Second Section, Third Subsection</title>
           <figure>
             <title>Fourth picture in book</title>
             <graphic fileref="pic3.jpg" />
           </figure>
         </sect2>
       </sect1>
      

      </chapter>

      File: Transform.xslt <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

       version="1.0">
       <xsl:output method="text" />
       <xsl:strip-space elements="*" />
       <xsl:template match="figure">
         [
         <xsl:apply-templates />
         ]
       </xsl:template>
       <xsl:template
         match="para | chapter/title | sect1/title | sect2/title " />
      

      </xsl:stylesheet> Output:

         [
         First picture in book
         ]
       
         [
         Second picture in book
         ]
       
         [
         Fourth picture in book
         ]
       
      </source>