XML Tutorial/XSLT stylesheet/Introduction

Материал из Web эксперт
Версия от 11:26, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Batch-Processing Nodes

   <source lang="xml">

The xsl:for-each element processes all the nodes in the same way, one after the other.

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="animal">
   <paragraph align="center">
     
<xsl:apply-templates select="name" /> </paragraph>
<xsl:for-each select="subspecies"> </xsl:for-each>
Subspecies Region Number As Of
           <xsl:apply-templates select="name" />
           <xsl:value-of select="region" />
           <xsl:value-of select="population" />
           <xsl:value-of select="population/@year" />
 </xsl:template>

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

<paragraph align="center">
T1T2</paragraph></table></source>


Every XSL stylesheet must start with xsl:stylesheet element

   <source lang="xml">

The attribute version="1.0" specifies version of XSL(T) specification. File: Data.xml

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

 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

</xsl:stylesheet></source>


Insert html tags into template

   <source lang="xml">

File: Data.xml

<employee>

 <firstName>Joe</firstName>
 <surname>Smith</surname>

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

 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:template match="employee">
     
       <xsl:apply-templates select="firstName"/>
     
     
       <xsl:apply-templates select="surname"/>
     
   </xsl:template>
   <xsl:template match="surname">
     
       <xsl:value-of select="."/>
     
   </xsl:template>

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


With XSL you can modify any source text and produce different output from the same source file

   <source lang="xml">

"//title" matches any title element anywhere in the document. "//author" matches any author element anywhere in the document. "/" matches the root element.

File: Data.xml

   <title>XSL</title>
   <author>John Smith</author>

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

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

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

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

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

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

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

   </xsl:template>

</xsl:stylesheet> Output:

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

John Smith

XSL

</source>


"xsl:template xsl:value-of"

   <source lang="xml">

An XSL processors parses an XML source and tries to find a matching template rule. If it does, instructions inside matching template are evaluated. File: Data.xml

   <bold>Hello, world.</bold>
   <red>I am </red>
   <italic>fine.</italic>

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

 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:template match="bold">
     <paragraph>
       
         <xsl:value-of select="."/>
       
     </paragraph>
   </xsl:template>
   <xsl:template match="red">
     <paragraph style="color:red">
       <xsl:value-of select="."/>
     </paragraph>
   </xsl:template>
   <xsl:template match="italic">
     <paragraph>
       
         <xsl:value-of select="."/>
       
     </paragraph>
   </xsl:template>

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

   <paragraph>Hello, world.</paragraph>
   <paragraph style="color:red">I am </paragraph>
   <paragraph>fine.</paragraph></source>
   
  

"xsl:value-of xsl:apply-templates"

   <source lang="xml">

File: Data.xml <employee>

 <firstName>Joe</firstName>
 <surname>Smith</surname>

</employee>

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

 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:template match="employee">
     
       <xsl:value-of select="."/>
     
   </xsl:template>
   <xsl:template match="surname">
     
       <xsl:value-of select="."/>
     
   </xsl:template>

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

 Joe
 Smith

</source>

Subspecies</th><td>Region</th><td>Number</th><td>As Of</th>