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

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

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

apply-templates select="county" mode="county"

   <source lang="xml">

File: Data.xml <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet href="Transform.xslt" type="text/xsl"?> <us>

<state name="Hawaii">
 <county name="Hawaii">
  <city class="largest">Hilo</city>
 </county>
</state>

</us>

File: Transform.xslt <?xml version="1.0" encoding="US-ASCII"?> <xsl:stylesheet version="1.0"

 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="html" />
 <xsl:template match="us/state">
   <html>
     <head>
       <title>
         State:
         <xsl:value-of select="@name" />
       </title>
     </head>
     <body>

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

All Counties

    <xsl:apply-templates select="county" mode="county" />

Largest Cities (by County)

    <xsl:apply-templates select="county" mode="city" />
     </body>
   </html>
 </xsl:template>
 <xsl:template match="county" mode="county">
  • <xsl:value-of select="@name" />
  •  </xsl:template>
     <xsl:template match="county" mode="city">
    
  • <xsl:value-of select="city" /> ( <xsl:value-of select="@name" /> )
  •  </xsl:template>
    

    </xsl:stylesheet> Output:

    <html>

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

    State: Hawaii

    All Counties

    • Hawaii

    Largest Cities (by County)

    • Hilo ( Hawaii )
      </body>
    

    </html></source>


    Creating and Applying Template Rules

       <source lang="xml">
    

    File: Data.xml

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

     <animal>
       <name language="English">T1</name>
       <name language="Latin">T2</name>
       <projects>
         <project>project1</project>
       </projects>
     </animal>
    

    </employees> 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>this is the title</title>
         </head>
         <body bgcolor="white">
           <xsl:apply-templates select="employees/animal" />
         </body>
       </html>
     </xsl:template>
     <xsl:template match="animal">
    


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

       <paragraph>
         <xsl:value-of select="name[@language="English"]" />
         <a href="http://www.wbex.ru">pages</a>
       </p>
    

     </xsl:template>
     <xsl:template match="name[@language="English"]">
       <nobr>
         
           <xsl:value-of select="." />
           :
         
       </nobr>
     </xsl:template>
     <xsl:template match="name[@language="Latin"]">
       <nobr>
         
           <xsl:value-of select="." />
         
       </nobr>
     </xsl:template>
    

    </xsl:stylesheet> Output: <html>

      <head>
         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
         <title>this is the title</title>
      </head>
      <body bgcolor="white">
    


    <nobr>T1  : </nobr> <nobr>T2</nobr>

         <paragraph>T1<a href="http://www.wbex.ru">pages</a></p>
    

      </body>
    

    </html></source>


    Template rules are modules that describe how a particular part of your source XML should be output

       <source lang="xml">
    

    A template rule has three parts:

       the opening tag describes which part(s) of your XML document the template should be applied to, 
       the middle bit describes what should happen once a match is found, 
       and the closing tag completes the template.
    

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

     <keyword>while</keyword>
     <keyword>continue</keyword>
     <keyword>def</keyword>
     <keyword>elif</keyword>
     <keyword>except</keyword>
     <keyword>from</keyword>
    

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

     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:saxon="http://icl.ru/saxon"
     extension-element-prefixes="saxon">
     <xsl:output method="text" encoding="ISO-8859-1" />
     <xsl:strip-space elements="*" />
     <xsl:template match="python">
       
       <xsl:text>Python 2.3 Keywords
    
    </xsl:text>
       <xsl:apply-templates select="keyword" mode="text">
         <xsl:sort />
       </xsl:apply-templates>
       
       <saxon:output href="keywords.html" method="html" indent="yes"
         saxon:indent-spaces="1">
         <xsl:fallback />
         <html>
           <body>
    

    Python 2.3 Keywords

      <xsl:apply-templates select="keyword" mode="html"> <xsl:sort /> </xsl:apply-templates>
           </body>
         </html>
       </saxon:output>
     </xsl:template>
     <xsl:template match="keyword" mode="html">
    
  • <xsl:value-of select="." />
  •  </xsl:template>
     <xsl:template match="keyword" mode="text">
       <xsl:value-of select="." />
       <xsl:choose>
         <xsl:when
           test="not((position() mod 5)=0) and not(position()=last())">
           <xsl:text>	</xsl:text>
         </xsl:when>
         <xsl:otherwise>
           <xsl:text>
    </xsl:text>
         </xsl:otherwise>
       </xsl:choose>
     </xsl:template>
    

    </xsl:stylesheet> Output: Python 2.3 Keywords continue def elif except from while</source>