XML/XSLT stylesheet/list

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

Create index number

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>
        <h1><xsl:value-of select="/list/title"/></h1>
        <p>
          <xsl:for-each select="/list/listitem">
            <xsl:number format="1. "/>
            <xsl:value-of select="."/>
            <br/>
          </xsl:for-each>
        </p>
      </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>
      <h1>title 1</h1>
      <p>1. item 1
         <br></br>2. item 2
         <br></br>3. item 3
         <br></br>4. item 4
         <br></br>5. item 5
         <br></br>6. item 6
         <br></br>7. item 7
         <br></br>
      </p>
   </body>
</html>



Create list in transformation

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>
        <h1>Our neighbours</h1>
        <xsl:apply-templates/>
        <hr/>
        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>
    <h2>
      <xsl:value-of select="@name"/>
    </h2>
    <xsl:value-of select="description/text()"/>
    <ul>
      <xsl:apply-templates/>
    </ul>
  </xsl:template>
  <xsl:template match="diameter">
    <li>
      <strong>Diameter: </strong>
      <xsl:value-of select="text()"/>
    </li>
  </xsl:template>
  <xsl:template match="moons">
    <li>
      <strong>Moons: </strong>
      <xsl:value-of select="text()"/>
    </li>
  </xsl:template>
  <xsl:template match="meanTemp">
    <li>
      <strong>Mean temperature: </strong>
      <xsl:value-of select="text()"/>
    </li>
  </xsl:template>
  <xsl:template match="oneDay">
    <li>
      <strong>Length of one day: </strong>
      <xsl:value-of select="text()"/>
    </li>
  </xsl:template>
  <xsl:template match="oneYear">
    <li>
      <strong>Length of one year: </strong>
      <xsl:value-of select="text()"/>
    </li>
  </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>
      <h1>Our neighbours</h1><img width="100" height="100" src="Venus.jpg%0A      "><h2>Venus</h2>
      description
      
      <ul>
         <li><strong>Diameter: </strong> 12104 km (7505 miles)
         </li>
         <li><strong>Moons: </strong> 0
         </li>
         <li><strong>Mean temperature: </strong> 482C (900F)
         </li>
         <li><strong>Length of one day: </strong> 243.01 earth days
         </li>
         <li><strong>Length of one year: </strong> 224.7 earth days
         </li>
      </ul>
      <hr>
      2006.
      
   </body>
</html>



Create ordered list

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>
        <h1>Instructions Guide</h1>
        <xsl:apply-templates />
      </body>
    </html>
  </xsl:template>
  <xsl:template match="parts-list">
    <h2>Parts</h2>
    <dl>
      <xsl:apply-templates />
    </dl>
  </xsl:template>
  <xsl:template match="part[@label]">
    <dt>
      <xsl:value-of select="@label" />
    </dt>
    <dd>
      <xsl:apply-templates />
    </dd>
  </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">
    <h2>Steps</h2>
    <ol>
      <xsl:apply-templates />
    </ol>
  </xsl:template>
  <xsl:template match="step">
    <li>
      <xsl:apply-templates />
    </li>
  </xsl:template>
</xsl:stylesheet>

Output:
<?xml version="1.0" encoding="ISO-8859-1"?><html><head><title>Instructions Guide</title></head><body><h1>Instructions Guide</h1>
  <h2>Parts</h2><dl>
    <dt>A</dt><dd>part 1</dd>
    <dt>B</dt><dd>part 2</dd>
    <dt>F</dt><dd>part 3</dd>
    <dt>N</dt><dd>part 4</dd>
    <dt>C</dt><dd>part 5</dd>
  </dl>
  <h2>Steps</h2><ol>
    <li>
      Glue
      part 1 (Part A)
      and
      part 2 (Part B)
      together.
    </li>
    <li>
      For each
      part 3 (Part F)
      , .
    </li>
    <li>
      Connect
      part 5 (Part C)
    </li>
  </ol>
</body></html>