XML/XSLT stylesheet/list

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

Create index number

   <source lang="xml">

File: Data.xml <?xml version="1.0"?> <list xml:lang="en">

 <title>title 1</title>
 <listitem>item 1</listitem>
 <listitem>item 2</listitem>
 <listitem>item 3</listitem>
 <listitem xml:lang="sw">item 4</listitem>
 <listitem xml:lang="en-gb">item 5</listitem>
 <listitem xml:lang="zu">item 6</listitem>
 <listitem xml:lang="jz">item 7</listitem>

</list>

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

 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output 
   method="xhtml" 
   encoding="ISO-8859-3"
   doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
   doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"/>
 <xsl:template match="/">
   <html>
     <head>
       <title><xsl:value-of select="/list/title"/></title>
     </head>
     <body>

<xsl:value-of select="/list/title"/>

<xsl:for-each select="/list/listitem"> <xsl:number format="1. "/> <xsl:value-of select="."/>
</xsl:for-each>

     </body>
   </html>
 </xsl:template>

</xsl:stylesheet> Output: <?xml version="1.0" encoding="ISO-8859-3"?> <!DOCTYPE html

 PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>

  <head>
     <title>title 1</title>
  </head>
  <body>

title 1

1. item 1
</br>2. item 2
</br>3. item 3
</br>4. item 4
</br>5. item 5
</br>6. item 6
</br>7. item 7
</br>

  </body>

</html>

</source>
   
  


Create list in transformation

   <source lang="xml">

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

 <planet name="Venus">
   <description>
   description
   </description>
   <diameter> 12104 km (7505 miles)</diameter>
   <moons> 0</moons>
   <meanTemp> 482C (900F)</meanTemp>
   <oneDay> 243.01 earth days</oneDay>
   <oneYear> 224.7 earth days</oneYear>
 </planet>

</neighbours> 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" version="4.0" indent="yes"/>
 <xsl:template match="/">
   <xsl:apply-templates/>
 </xsl:template>
 <xsl:template match="text()"/>
 <xsl:template match="neighbours">
   <html>
     <head>
       <title>A simple HTML page</title>
     </head>
     <body>

Our neighbours

       <xsl:apply-templates/>

       2006.
   </body>
   </html>
 </xsl:template>
 <xsl:template match="planet">
   <img width="100" height="100">
     <xsl:attribute name="src"><xsl:value-of select="@name"/>.jpg
     </xsl:attribute>
   </img>

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

   <xsl:value-of select="description/text()"/>
    <xsl:apply-templates/>
 </xsl:template>
 <xsl:template match="diameter">
  • Diameter: <xsl:value-of select="text()"/>
  •  </xsl:template>
     <xsl:template match="moons">
    
  • Moons: <xsl:value-of select="text()"/>
  •  </xsl:template>
     <xsl:template match="meanTemp">
    
  • Mean temperature: <xsl:value-of select="text()"/>
  •  </xsl:template>
     <xsl:template match="oneDay">
    
  • Length of one day: <xsl:value-of select="text()"/>
  •  </xsl:template>
     <xsl:template match="oneYear">
    
  • Length of one year: <xsl:value-of select="text()"/>
  •  </xsl:template>
    

    </xsl:stylesheet> Output: <html>

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

    Our neighbours

    <img width="100" height="100" src="Venus.jpg%0A ">

    Venus

         description
         
    
    • Diameter: 12104 km (7505 miles)
    • Moons: 0
    • Mean temperature: 482C (900F)
    • Length of one day: 243.01 earth days
    • Length of one year: 224.7 earth days

         2006.
         
      </body>
    

    </html>

    </source>
       
      
    


    Create ordered list

       <source lang="xml">
    

    File: Data.xml <?xml version="1.0"?> <manual type="assembly" id="model-rocket">

     <parts-list>
       <part label="A" count="1">part 1</part>
       <part label="B" count="1">part 2</part>
       <part label="F" count="4">part 3</part>
       <part label="N" count="3">part 4</part>
       <part label="C" count="1">part 5</part>
     </parts-list>
     <instructions>
       <step>
         Glue
         <part ref="A" />
         and
         <part ref="B" />
         together.
       </step>
       <step>
         For each
         <part ref="F" />
         , .
       </step>
       <step>
         Connect
         <part ref="C" />
       </step>
     </instructions>
    

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

     version="1.0">
     <xsl:output method="xml" encoding="ISO-8859-1" />
     <xsl:template match="manual">
       <html>
         <head>
           <title>Instructions Guide</title>
         </head>
         <body>
    

    Instructions Guide

           <xsl:apply-templates />
         </body>
       </html>
     </xsl:template>
     <xsl:template match="parts-list">
    

    Parts

    <xsl:apply-templates />
     </xsl:template>
     <xsl:template match="part[@label]">
       
    <xsl:value-of select="@label" />
    <xsl:apply-templates />
    </xsl:template> <xsl:template match="part[@ref]"> <xsl:variable name="label"> <xsl:value-of select="@ref" /> </xsl:variable> <xsl:value-of select="//part[@label=$label]"></xsl:value-of> <xsl:text> (Part </xsl:text> <xsl:value-of select="@ref" /> <xsl:text>)</xsl:text> </xsl:template> <xsl:template match="instructions">

    Steps

      <xsl:apply-templates />
     </xsl:template>
     <xsl:template match="step">
    
  • <xsl:apply-templates />
  •  </xsl:template>
    

    </xsl:stylesheet>

    Output:

    <?xml version="1.0" encoding="ISO-8859-1"?><html><head><title>Instructions Guide</title></head><body>

    Instructions Guide

    Parts

       
    A
    part 1
    B
    part 2
    F
    part 3
    N
    part 4
    C
    part 5

    Steps

    1. Glue part 1 (Part A) and part 2 (Part B) together.
    2. For each part 3 (Part F) , .
    3. Connect part 5 (Part C)

    </body></html>

    </source>