XML Tutorial/XSLT stylesheet/output

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

html: "disable-output-escaping xsl:output"

   <source lang="xml">

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"/>
   <xsl:template match="/">
     <xsl:text>&</xsl:text>
     <xsl:text disable-output-escaping="yes">&</xsl:text>
   </xsl:template>

</xsl:stylesheet></source>


in Cp1250

   <source lang="xml">

File: Data.xml <?xml version="1.0" encoding="utf-8"?> <html>

 <head>
   <title>HTML</title>
 </head>
 <body>

HTML output

   ?  </body>

</html> 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" encoding="Cp1250"/>
   <xsl:template match="/">
     <xsl:copy-of select="/source/*"/>
   </xsl:template>

</xsl:stylesheet></source>


in ISO-8859-1

   <source lang="xml">

File: Data.xml <?xml version="1.0" encoding="utf-8"?> <html>

 <head>
   <title>HTML</title>
 </head>
 <body>

HTML output

   ?  </body>

</html> 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" encoding="ISO-8859-1"/>
   <xsl:template match="/">
     <xsl:copy-of select="/source/*"/>
   </xsl:template>

</xsl:stylesheet></source>


in UTF-16

   <source lang="xml">

File: Data.xml <?xml version="1.0" encoding="utf-8"?> <html>

 <head>
   <title>HTML</title>
 </head>
 <body>

HTML output

   ?  </body>

</html> 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" encoding="UTF-16"/>
   <xsl:template match="/">
     <xsl:copy-of select="/source/*"/>
   </xsl:template>

</xsl:stylesheet></source>


output indent="yes"

   <source lang="xml">

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

 version="1.0">
 <xsl:output indent="yes" />
 <xsl:template match="/">
   <html>
     <head>
       <title>Contact Info</title>
     </head>
     <body
       style="font-size:12px; font-family: Verdana, Arial, Helvetica;">
       this is a test
     </body>
   </html>
 </xsl:template>

</xsl:stylesheet> Output: <html>

  <head>
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     <title>Contact Info</title>
  </head>
  <body style="font-size:12px; font-family: Verdana, Arial, Helvetica;">
             this is a test
           
  </body>

</html></source>


output method="text"

   <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>

</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="sect2[figure]">
   <xsl:value-of select="title" />
   [
   <xsl:apply-templates />
   ]
 </xsl:template>

</xsl:stylesheet> Output: The ChapterFirst SectionFirst picture in book</source>


output method="text" indent="no"

   <source lang="xml">

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

 <address>
   <name>
     <title>Ms.</title>
     <first-name>Joe</first-name>
     <last-name>Smith</last-name>
   </name>
   <street>707 First Way</street>
   <city>New York</city>
   <state>ME</state>
   <zip>00218</zip>
 </address>

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

 <xsl:output method="text" indent="no"/>
 <xsl:strip-space elements="*"/>
 <xsl:variable name="newline">
 <xsl:text></xsl:text>
 </xsl:variable>
 <xsl:template match="/">
   <xsl:for-each select="addressbook/address">
     <xsl:sort select="name/last-name"/>
     <xsl:sort select="name/first-name"/>
     <xsl:if test="name/title">
       <xsl:value-of select="name/title"/>
       <xsl:text> </xsl:text>
     </xsl:if>
     <xsl:value-of select="name/first-name"/>
     <xsl:text> </xsl:text>
     <xsl:value-of select="name/last-name"/>
     <xsl:value-of select="$newline"/>
     <xsl:value-of select="street"/>
     <xsl:value-of select="$newline"/>
     <xsl:value-of select="city"/>
     <xsl:text>, </xsl:text>
     <xsl:value-of select="state"/>
     <xsl:text>  </xsl:text>
     <xsl:value-of select="zip"/>
     <xsl:value-of select="$newline"/>
     <xsl:value-of select="$newline"/>
   </xsl:for-each>
 </xsl:template>

</xsl:stylesheet> Output: Ms. Joe Smith707 First WayNew York, ME 00218</source>


output method="xml" indent="yes" encoding="ISO-8859-1"

   <source lang="xml">

File: Data.xml <?xml version="1.0" encoding="UTF-8"?> <europe>

<scandinavia>
 <state>Finland</state>
 <state>Sweden</state>
 <state>Iceland</state>
 <state>Norway</state>
 <state>Denmark</state>
</scandinavia>

</europe> File: Transform.xslt <?xml version="1.0" encoding="UTF-8" ?> <xsl:stylesheet version="1.0"

 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns="http://www.wbex.ru"
 xmlns:sc="http://www.wbex.ru/scand"
 xmlns:t="http://www.wbex.ru/temp">
 <xsl:output method="xml" indent="yes" encoding="ISO-8859-1" />
 <xsl:namespace-alias stylesheet-prefix="t" result-prefix="xsl" />
 <xsl:template match="europe">
   <t:stylesheet version="1.0">
     <t:output method="xml" indent="yes" encoding="ISO-8859-1" />
     <t:namespace-alias stylesheet-prefix="sc" result-prefix="#default" />
     <xsl:text>

</xsl:text>
     <t:template match="{name()}">
       <t:apply-templates select="scandinavia" />
     </t:template>
     <xsl:text>
</xsl:text>
     <xsl:apply-templates select="scandinavia" />
     <xsl:apply-templates select="scandinavia/state[1]" />
     <xsl:text>
</xsl:text>
   </t:stylesheet>
 </xsl:template>
 <xsl:template match="scandinavia">
   <xsl:text>
</xsl:text>
   <t:template match="{name()}">
     <sc:scandinavia>
       <t:apply-templates select="state">
         <t:sort />
       </t:apply-templates>
     </sc:scandinavia>
   </t:template>
   <xsl:text>
</xsl:text>
 </xsl:template>
 <xsl:template match="state">
   <xsl:text>
</xsl:text>
   <t:template match="{name()}">
     <sc:country>
       <t:value-of select="." />
     </sc:country>
   </t:template>
   <xsl:text>
</xsl:text>
 </xsl:template>

</xsl:stylesheet> Output: <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet xmlns:sc="http://www.wbex.ru/scand"

               xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
               xmlns="http://www.wbex.ru"
               version="1.0">
  <xsl:output method="xml" indent="yes" encoding="ISO-8859-1"/>
  
  <xsl:namespace-alias stylesheet-prefix="sc" result-prefix="#default"/>
  <xsl:template match="europe">
     <xsl:apply-templates select="scandinavia"/>
  </xsl:template>
  <xsl:template match="scandinavia">
     <sc:scandinavia>
        <xsl:apply-templates select="state">
           <xsl:sort/>
        </xsl:apply-templates>
     </sc:scandinavia>
  </xsl:template>
  <xsl:template match="state">
     <sc:country>
        <xsl:value-of select="."/>
     </sc:country>
  </xsl:template>

</xsl:stylesheet></source>


outputs in UTF-8

   <source lang="xml">

File: Data.xml <?xml version="1.0" encoding="utf-8"?> <html>

 <head>
   <title>HTML</title>
 </head>
 <body>

HTML output

   ?  </body>

</html> 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" encoding="UTF-8"/>
   <xsl:template match="/">
     <xsl:copy-of select="/source/*"/>
   </xsl:template>

</xsl:stylesheet></source>


Set output encoding, indent, standalone, doctype

   <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="1.0"

 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"
   encoding="ISO-8859-3"
   indent="yes"
   omit-xml-declaration="no"
   standalone="yes"
   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"/>

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

</xsl:stylesheet> Output: title 1title 11. item 12. item 23. item 34. item 45. item 56. item 67. item 7</source>


text: "disable-output-escaping xsl:output"

   <source lang="xml">

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="text"/>
   <xsl:template match="/">
     <xsl:text>&</xsl:text>
     <xsl:text disable-output-escaping="yes">&</xsl:text>
   </xsl:template>

</xsl:stylesheet></source>


text output method outputs the string-value of every text node

   <source lang="xml">

File: Data.xml <?xml version="1.0" encoding="utf-8"?> <AAA id="12"/> 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="text"/>
   <xsl:template match="AAA">
     <xsl:text><!ELEMENT </xsl:text>
     <xsl:value-of select="name()"/>
     <xsl:text> ANY></xsl:text>
     <xsl:text><!ATTLIST </xsl:text>
     <xsl:value-of select="name()"/>
     <xsl:text/>
     <xsl:value-of select="name(@*)"/>
     <xsl:text> ID #REQUIRED></xsl:text>
     <xsl:text>Look at my source in your browser</xsl:text>
   </xsl:template>

</xsl:stylesheet></source>


The XSLT stylesheet used to generate a CSV file

   <source lang="xml">

File: Data.xml <?xml version="1.0" encoding="utf-8"?> <emailList>

 <person>
   <name>person1</name>
   <email>p@hotmail.ru</email>
 </person>
 <person>
   <name>person2</name>
   <email>p@hotmail.ru</email>
 </person>
 <person>
   <name>person3</name>
   <email>p3@hotmail.ru</email>
 </person>

</emailList> 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="text"/>

 <xsl:template match="/emailList/person">
   <xsl:value-of select="name"/>,<xsl:value-of select="email"/>
 </xsl:template>

</xsl:stylesheet> Output:

 person1,p@hotmail.ru
 person2,p@hotmail.ru
 person3,p3@hotmail.ru</source>
   
  

xml: "disable-output-escaping xsl:output"

   <source lang="xml">

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="xml"/>
   <xsl:template match="/">
     <xsl:text>&</xsl:text>
     <xsl:text disable-output-escaping="yes">&</xsl:text>
   </xsl:template>

</xsl:stylesheet></source>


<xsl:output> Element

   <source lang="xml">

XSLT can be used to produce XML, HTML, or text output. XML output is the default. If you want to do it explicitly, then the following code is used: <xsl:output method="xml" /> The value of the method attribute is case sensitive and must be all lowercase. HTML output is specified like this: <xsl:output method="html" /> Text output is specified like this: <xsl:output method="text" /></source>