XML/XSLT stylesheet/output

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

Add , to the output

File: Data.xml
<employees>
  <emp>
    <lName>A</lName>
    <fName>B</fName>
    <hireDate>19980323</hireDate>
  </emp>
  <emp>
    <lName>C</lName>
    <fName>D</fName>
    <hireDate>19991103</hireDate>
  </emp>
</employees>
File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="text" indent="no" />
  <xsl:template match="emp">
    <xsl:apply-templates select="lName" />
    <xsl:text>,</xsl:text>
    <xsl:apply-templates select="fName" />
    <xsl:text>,</xsl:text>
    <xsl:apply-templates select="hireDate" />
  </xsl:template>

</xsl:stylesheet>
Output:

  A,B,19980323
  C,D,19991103



Add your signs to the output (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:template match="sect1/title">
    [
    <xsl:apply-templates />
    ]
  </xsl:template>
  <xsl:template match="para" />
</xsl:stylesheet>
Output:

  The Chapter
  
    
    [
    First Section
    ]
  
    
      First picture in book



format output with tab

File: Data.xml
<employees>
  <employee hireDate="04/23/1999">
    <last>A</last>
    <first>B</first>
    <salary>100000</salary>
  </employee>
  <employee hireDate="09/01/1998">
    <last>C</last>
    <first>D</first>
    <salary>95000</salary>
  </employee>
  <employee hireDate="08/20/2000">
    <last>E</last>
    <first>F</first>
    <salary>89000</salary>
  </employee>
</employees>
File: Transform.xslt
<!DOCTYPE stylesheet [
  <!ENTITY tab "<xsl:text>&#9;</xsl:text>">
  <!ENTITY cr "<xsl:text>
</xsl:text>">
]>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="text" />
  <xsl:template match="employees">
    Last&tab;First&tab;Salary&tab;Hire Date ----&tab;-----&tab;------&tab;----------
    <xsl:apply-templates />
  </xsl:template>
  <xsl:template match="employee">
    <xsl:apply-templates select="last" />
    &tab;
    <xsl:apply-templates select="first" />
    &tab;
    <xsl:apply-templates select="salary" />
    &tab;
    <xsl:apply-templates select="@hireDate" />
    &cr;
  </xsl:template>
</xsl:stylesheet>
Output:

    Last  First  Salary  Hire Date ----  -----  ------  ----------
    
  A  B  100000  04/23/1999

  C  D  95000  09/01/1998

  E  F  89000  08/20/2000



Output html option list

File: Data.xml
<?xml version="1.0"?>
<countries>
  <country name="France" />
  <country name="Germany" />
  <country name="Israel" />
  <country name="Japan" />
  <country name="Poland" />
  <country name="United States" selected="yes" />
  <country name="Venezuela" />
</countries>
File: Transform.xslt
<?xml version="1.0"?>
<html xsl:version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <body>
    <h1>Please select a country:</h1>
    <select id="country">
      <xsl:for-each select="//country">
        <option value="{@name}">
          <xsl:if test="@selected="yes"">
            <xsl:attribute name="selected">selected</xsl:attribute>
          </xsl:if>
          <xsl:value-of select="@name" />
        </option>
      </xsl:for-each>
    </select>
    <hr />
  </body>
</html>
Output:
<html>
   <body>
      <h1>Please select a country:</h1><select id="country">
         <option value="France">France</option>
         <option value="Germany">Germany</option>
         <option value="Israel">Israel</option>
         <option value="Japan">Japan</option>
         <option value="Poland">Poland</option>
         <option value="United States" selected>United States</option>
         <option value="Venezuela">Venezuela</option></select><hr>
   </body>
</html>



Output HTML tags

File: Data.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="Transform.xslt"?>
<greeting>Hello World!</greeting>

File: Transform.xslt
<?xml version="1.0" encoding="ISO-8859-1"?>
<html xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns="http://www.w3.org/1999/xhtml"
      xsl:version="2.0">
  <head><title>Hello World Example</title></head>
  <body>
    <p>
      <xsl:value-of select="/greeting" />
    </p>
  </body>
</html>
Output:
<?xml version="1.0" encoding="UTF-8"?><html xmlns="http://www.w3.org/1999/xhtml">
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
      <title>Hello World Example</title>
   </head>
   <body>
      <p>Hello World!</p>
   </body>
</html>



output method="html"

File: Data.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="Books.xsl"?>
<Books xmlns="http://www.wbex.ru"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <Book xmlns="http://www.wbex.ru">
                <Title>title 1</Title>
                <Author>author 1</Author>
                <Date>1998</Date>
                <ISBN>1-11111-111-1</ISBN>
                <Publisher>publisher 1</Publisher>
        </Book>
        <Book xmlns="http://www.wbex.ru">
                <Title>title 2</Title>
                <Author>author 2</Author>
                <Date>1977</Date>
                <ISBN>2-222-22222-2</ISBN>
                <Publisher>publisher 2</Publisher>
        </Book>
</Books>

File: Books.xsl
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xlink="http://www.w3.org/1999/xlink/namespace"
                xmlns:bks="http://www.wbex.ru"
                xmlns:bk="http://www.wbex.ru"
                exclude-result-prefixes="bk bks"
                version="1.0">
 
    <xsl:output method="html"/>
    <xsl:include href="Book.xsl"/>
    <xsl:template match="/">
        <HTML>
            <BODY>
                <xsl:apply-templates/>
            </BODY>
        </HTML>
    </xsl:template>
    <xsl:template match="bks:Books">
         <CENTER><H2>My Bookstore</H2></CENTER>
         <TABLE border="1">
             <xsl:apply-templates/>
         </TABLE>
    </xsl:template>
    <xsl:template match="bks:Book">
         <xsl:variable name="book-url" select="document(@xlink:href)"/>
         <xsl:apply-templates select="$book-url//bk:Book"/>
    </xsl:template>
</xsl:stylesheet>

File: Book.xsl
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:bk="http://www.wbex.ru"
                exclude-result-prefixes="bk"
                version="1.0">
 
    <xsl:output method="html"/>
    <xsl:template match="/">
        <HTML>
            <BODY>
                <TABLE border="1">
                    <xsl:apply-templates/>
                </TABLE>
            </BODY>
        </HTML>
    </xsl:template>
    <xsl:template match="bk:Book">
         <TR>
             <xsl:apply-templates/>
         </TR>
    </xsl:template>
    <xsl:template match="*">
         <TD>
             <xsl:value-of select="."/>
         </TD>
    </xsl:template>
</xsl:stylesheet>



output method="xml" omit-xml-declaration="yes" indent="no"

File: Data.xml
<wine grape="chardonnay">
  <product>product 2</product>
  <year>1997</year>
  <price>10.99</price>
</wine>

File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="xml" omit-xml-declaration="yes" indent="no" />
  
  <xsl:template match="wine">
    <wine>
      <price>
        <xsl:apply-templates select="price" />
      </price>
      <product>
        <xsl:apply-templates select="product" />
      </product>
    </wine>
  </xsl:template>
</xsl:stylesheet>
Output:
<wine><price>10.99</price><product>product 2</product></wine>



output rtf in format

File: Data.xml
<article>
  <title author="bd" ver="1.0">My Article</title>
  <p>First paragraph. 3 &lt; 4.</p>
  <p>Second paragraph. AT&amp;T is a big company.</p>
</article>
File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="text" />
  <xsl:template match="article">
    {\rtf1
    <xsl:apply-templates />
    }
  </xsl:template>
  <xsl:template match="title">
    \par {\b
    <xsl:apply-templates />
    }
  </xsl:template>
  <xsl:template match="p">
    \par
    <xsl:apply-templates />
  </xsl:template>
</xsl:stylesheet>
Output:

    {\rtf1
    
  
    \par {\b
    My Article
    }
  
  
    \par
    First paragraph. 3 < 4.
  
    \par
    Second paragraph. AT&T is a big company.
    }



Output text with tag

File: Data.xml
<test>AAA</test>
File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:template match="test">
    <testOut>
      this is a test
      <xsl:apply-templates />
    </testOut>
  </xsl:template>
</xsl:stylesheet>

Output:
<?xml version="1.0" encoding="UTF-8"?><testOut>
      this is a test
      AAA</testOut>



Output text with xsl:text

File: Data.xml 
<?xml version="1.0" encoding="iso-8859-1"?>
<document>
    <author>author</author>
    <title>XSLT 2.0 Programmer"s Reference</title>
    <copyright/>
    <date/>
    <abstract>abstract
    </abstract>
</document>

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:variable name="owner">John Wiley and Sons</xsl:variable>
  <xsl:template name="copyright">
    <xsl:text>Copyright ?</xsl:text>
    <xsl:value-of select="$owner" />
    <xsl:text> 2004</xsl:text>
  </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
    author
    XSLT 2.0 Programmer"s Reference
    
    
    abstract



Select with value-of and output with new tags

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="xml" indent="yes"/>
  <xsl:template match="/">
    <contacts>
      <xsl:for-each select="emailList/person">
        <contact>
          <fullName>
            <xsl:value-of select="name"/>
          </fullName>
          <eMail>
            <xsl:value-of select="email"/>
          </eMail>
        </contact>
      </xsl:for-each>
    </contacts>
  </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<contacts>
   <contact>
      <fullName>person1</fullName>
      <eMail>p@hotmail.ru</eMail>
   </contact>
   <contact>
      <fullName>person2</fullName>
      <eMail>p@hotmail.ru</eMail>
   </contact>
   <contact>
      <fullName>person3</fullName>
      <eMail>p3@hotmail.ru</eMail>
   </contact>
</contacts>