XML Tutorial/XSLT stylesheet/output

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

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

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


in Cp1250

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<html>
  <head>
    <title>HTML</title>
  </head>
  <body>
    <h1>HTML output</h1>
    ?  </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>


in ISO-8859-1

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<html>
  <head>
    <title>HTML</title>
  </head>
  <body>
    <h1>HTML output</h1>
    ?  </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>


in UTF-16

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<html>
  <head>
    <title>HTML</title>
  </head>
  <body>
    <h1>HTML output</h1>
    ?  </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>


output indent="yes"

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>


output method="text"

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


output method="text" indent="no"

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


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

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>&#10;&#10;</xsl:text>
      <t:template match="{name()}">
        <t:apply-templates select="scandinavia" />
      </t:template>
      <xsl:text>&#10;</xsl:text>
      <xsl:apply-templates select="scandinavia" />
      <xsl:apply-templates select="scandinavia/state[1]" />
      <xsl:text>&#10;</xsl:text>
    </t:stylesheet>
  </xsl:template>
  <xsl:template match="scandinavia">
    <xsl:text>&#10;</xsl:text>
    <t:template match="{name()}">
      <sc:scandinavia>
        <t:apply-templates select="state">
          <t:sort />
        </t:apply-templates>
      </sc:scandinavia>
    </t:template>
    <xsl:text>&#10;</xsl:text>
  </xsl:template>
  <xsl:template match="state">
    <xsl:text>&#10;</xsl:text>
    <t:template match="{name()}">
      <sc:country>
        <t:value-of select="." />
      </sc:country>
    </t:template>
    <xsl:text>&#10;</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>


outputs in UTF-8

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<html>
  <head>
    <title>HTML</title>
  </head>
  <body>
    <h1>HTML output</h1>
    ?  </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>


Set output encoding, indent, standalone, doctype

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>
        <h1><xsl:value-of select="/list/title"/></h1>
        <paragraph>
          <xsl:for-each select="/list/listitem">
            <xsl:number format="1. "/>
            <xsl:value-of select="."/>
            <br/>
          </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


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

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


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

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>&lt;!ELEMENT </xsl:text>
      <xsl:value-of select="name()"/>
      <xsl:text> ANY></xsl:text>
      <xsl:text>&lt;!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>


The XSLT stylesheet used to generate a CSV file

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


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

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


<xsl:output> Element

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