XML/XSLT stylesheet/template match — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 08:26, 26 мая 2010
Содержание
- 1 Call a template with parameter
- 2 Define and use template
- 3 Get two values in one template
- 4 Locate parent tags and get value from children tags
- 5 match and get value operations with namespace
- 6 match an element
- 7 output a table without loop
- 8 output in template
- 9 Select value from an element with value-of
- 10 set match mode to fulltext
- 11 template mode="index"
- 12 template with parameters
Call a template with parameter
File: Data.xml
<?xml version="1.0"?>
<programme>
<opera>
<title>A</title>
<composer>B</composer>
<date>1791</date>
</opera>
<composer name="Mozart">
<fullname>Mozart</fullname>
<born>1756</born>
<died>1791</died>
</composer>
</programme>
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:template match="/">
<xsl:variable name="list">
<xsl:call-template name="make-list">
<xsl:with-param name="names"
select="/programme/composer/fullname" />
</xsl:call-template>
</xsl:variable>
This week"s composers are:
<xsl:value-of select="translate($list, ",", ";")" />
</xsl:template>
<xsl:template name="make-list">
<xsl:param name="names" />
<xsl:for-each select="$names">
<xsl:value-of select="." />
<xsl:if test="position()!=last()">,</xsl:if>
<xsl:if test="position()=last()-1">and</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
This week"s composers are:
Define and use template
File: Data.xml
<poem>
<author>author 1</author>
<date>1912</date>
<title>Song</title>
<stanza>
<line>line 1</line>
<line>line 2</line>
<line>line 3</line>
<line>line 4</line>
</stanza>
<stanza>
<line>line 5</line>
<line>line 6</line>
<line>line 7</line>
<line>line 8</line>
</stanza>
<stanza>
<line>line 9</line>
<line>line 10</line>
<line>line 11</line>
<line>line 12</line>
</stanza>
</poem>
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:template match="poem">
<html>
<head>
<title>
<xsl:value-of select="title" />
</title>
</head>
<body>
<xsl:apply-templates select="title" />
<xsl:apply-templates select="author" />
<xsl:apply-templates select="stanza" />
<xsl:apply-templates select="date" />
</body>
</html>
</xsl:template>
<xsl:template match="title">
<div align="center">
<h1>
<xsl:value-of select="." />
</h1>
</div>
</xsl:template>
<xsl:template match="author">
<div align="center">
<h2>
By
<xsl:value-of select="." />
</h2>
</div>
</xsl:template>
<xsl:template match="date">
<p>
<i>
<xsl:value-of select="." />
</i>
</p>
</xsl:template>
<xsl:template match="stanza">
<p>
<xsl:apply-templates select="line" />
</p>
</xsl:template>
<xsl:template match="line">
<xsl:if test="position() mod 2 = 0">  </xsl:if>
<xsl:value-of select="." />
<br />
</xsl:template>
</xsl:stylesheet>
Output:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Song</title>
</head>
<body>
<div align="center">
<h1>Song</h1>
</div>
<div align="center">
<h2>
By
author 1
</h2>
</div>
<p>line 1<br> line 2<br>line 3<br> line 4<br></p>
<p>line 5<br> line 6<br>line 7<br> line 8<br></p>
<p>line 9<br> line 10<br>line 11<br> line 12<br></p>
<p><i>1912</i></p>
</body>
</html>
Get two values in one template
File: Data.xml
<?xml version="1.0"?>
<people>
<person born="1912" died="1954">
<name>
<first_name>A</first_name>
<last_name>B</last_name>
</name>
<profession>C</profession>
<profession>D</profession>
<profession>E</profession>
</person>
<person born="2008" died="2008">
<name>
<first_name>F</first_name>
<middle_initial>G</middle_initial>
<last_name>H</last_name>
</name>
<profession>I</profession>
<hobby>J</hobby>
</person>
</people>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="people">
<html>
<head><title>Famous Scientists</title></head>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="name">
<p><xsl:value-of select="last_name"/>,
<xsl:value-of select="first_name"/></p>
</xsl:template>
<xsl:template match="person">
<xsl:apply-templates select="name"/>
</xsl:template>
</xsl:stylesheet>
Output:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Famous Scientists</title>
</head>
<body>
<p>B,
A
</p>
<p>H,
F
</p>
</body>
</html>
Locate parent tags and get value from children tags
File: Data.xml
<?xml version="1.0"?>
<emailList>
<person>
<name>name 1</name>
<email>g@gmail.ru</email>
</person>
<person>
<name>name 2</name>
<email>n@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:template match="/">
<html>
<head>
<title>Email Listing</title>
</head>
<body>
<table>
<tr>
<th>Name</th>
<th>E-mail Address</th>
</tr>
<xsl:apply-templates/>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="person">
<tr>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="email"/></td>
</tr>
</xsl:template>
</xsl:stylesheet>
Output:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Email Listing</title>
</head>
<body>
<table>
<tr>
<th>Name</th>
<th>E-mail Address</th>
</tr>
<tr>
<td>name 1</td>
<td>g@gmail.ru</td>
</tr>
<tr>
<td>name 2</td>
<td>n@hotmail.ru</td>
</tr>
</table>
</body>
</html>
match and get value operations with namespace
File: Data.xml
<?xml version="1.0"?>
<people>
<person born="1912" died="1954">
<name>
<first_name>A</first_name>
<last_name>B</last_name>
</name>
<profession>C</profession>
<profession>D</profession>
<profession>E</profession>
</person>
<person born="2008" died="2008">
<name>
<first_name>F</first_name>
<middle_initial>G</middle_initial>
<last_name>H</last_name>
</name>
<profession>I</profession>
<hobby>J</hobby>
</person>
</people>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:pe="http://www.cafeconleche.org/namespaces/people">
<xsl:template match="pe:people">
<html>
<head><title>Famous Scientists</title></head>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="pe:name">
<p><xsl:value-of select="pe:last_name"/>,
<xsl:value-of select="pe:first_name"/></p>
</xsl:template>
<xsl:template match="pe:person">
<xsl:apply-templates select="pe:name"/>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
A
B
C
D
E
F
G
H
I
J
match an element
File: Data.xml
<?xml version="1.0"?>
<people>
<person born="1912" died="1954">
<name>
<first_name>A</first_name>
<last_name>B</last_name>
</name>
<profession>C</profession>
<profession>D</profession>
<profession>E</profession>
</person>
<person born="2008" died="2008">
<name>
<first_name>F</first_name>
<middle_initial>G</middle_initial>
<last_name>H</last_name>
</name>
<profession>I</profession>
<hobby>J</hobby>
</person>
</people>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="person">A Person</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
A Person
A Person
output a table without loop
File: Data.xml
<?xml version="1.0" ?>
<customer-list>
<customer>
<name>
<first>A</first>
<last>B</last>
</name>
<order>C</order>
<order>D</order>
</customer>
</customer-list>
File: Transform.xslt
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<HTML>
<HEAD>
<TITLE>Customers</TITLE>
</HEAD>
<BODY>
<TABLE BORDER="1" >
<xsl:apply-templates select="customer-list/customer" />
</TABLE>
</BODY>
</HTML>
</xsl:template>
<xsl:template match="customer">
<TR><TH ALIGN="left">
<xsl:apply-templates select="name"/>
</TH></TR>
<xsl:apply-templates select="order"/>
</xsl:template>
<xsl:template match="order">
<TR><TD>
<xsl:apply-templates/>
</TD></TR>
</xsl:template>
<xsl:template match="name">
<xsl:value-of select="last" />,
<xsl:value-of select="first" />
</xsl:template>
</xsl:stylesheet>
Output:
<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<TITLE>Customers</TITLE>
</HEAD>
<BODY>
<TABLE BORDER="1">
<TR>
<TH ALIGN="left">B,
A
</TH>
</TR>
<TR>
<TD>C</TD>
</TR>
<TR>
<TD>D</TD>
</TR>
</TABLE>
</BODY>
</HTML>
output in template
File: Data.xml
<?xml version="1.0"?>
<people>
<person born="1912" died="1954">
<name>
<first_name>A</first_name>
<last_name>B</last_name>
</name>
<profession>C</profession>
<profession>D</profession>
<profession>E</profession>
</person>
<person born="2008" died="2008">
<name>
<first_name>F</first_name>
<middle_initial>G</middle_initial>
<last_name>H</last_name>
</name>
<profession>I</profession>
<hobby>J</hobby>
</person>
</people>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="person">
<p>A Person</p>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<p>A Person</p>
<p>A Person</p>
Select value from an element with value-of
File: Data.xml
<?xml version="1.0"?>
<people>
<person born="1912" died="1954">
<name>
<first_name>A</first_name>
<last_name>B</last_name>
</name>
<profession>C</profession>
<profession>D</profession>
<profession>E</profession>
</person>
<person born="2008" died="2008">
<name>
<first_name>F</first_name>
<middle_initial>G</middle_initial>
<last_name>H</last_name>
</name>
<profession>I</profession>
<hobby>J</hobby>
</person>
</people>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="person">
<p>
<xsl:value-of select="name"/>
</p>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<p>
A
B
</p>
<p>
F
G
H
</p>
set match mode to fulltext
File: Data.xml
<?xml version="1.0"?>
<Book>
<Title>this is the title</Title>
<Authors>
<Author>A</Author>
<Author>B</Author>
<Author>C</Author>
</Authors>
<Year>2007</Year>
<Chapters>
<Chapter number="1" title="title 1">chapter 1</Chapter>
<Chapter number="2" title="title 2">chapter 2</Chapter>
<Chapter number="3" title="title 3">chapter 3</Chapter>
<Chapter number="4" title="title 4">chapter 4</Chapter>
<Chapter number="5" title="title 5">chapter 5</Chapter>
</Chapters>
</Book>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="Chapter" mode="fulltext">
<h3>
<xsl:value-of select="@number" />.
<xsl:value-of select="@title" />
</h3>
<paragraph>
<xsl:value-of select="." />
</paragraph>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
this is the title
A
B
C
2007
chapter 1
chapter 2
chapter 3
chapter 4
chapter 5
template mode="index"
File: Data.xml
<?xml version="1.0"?>
<cv>
<para>
<performance>
<publication>H</publication>
G
<venue>F</venue>
E
<group>D</group>
C
<date>1998</date>
B
<quote>
A
</quote>
</performance>
</para>
</cv>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates />
<table bgcolor="#cccccc" border="1" cellpadding="5">
<tr>
<td>
<b>Date</b>
</td>
<td>
<b>Venue</b>
</td>
<td>
<b>Composer</b>
</td>
<td>
<b>Work</b>
</td>
<td>
<b>Role</b>
</td>
</tr>
<xsl:apply-templates mode="index" />
</table>
</body>
</html>
</xsl:template>
<xsl:template match="performance" mode="index">
<tr>
<td>
<xsl:value-of select="date" />
 
</td>
<td>
<xsl:value-of select="venue" />
 
</td>
<td>
<xsl:value-of select="composer" />
 
</td>
<td>
<xsl:value-of select="work" />
 
</td>
<td>
<xsl:value-of select="role" />
 
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
Output:
<html>
<body>
H
G
F
E
D
C
1998
B
A
<table bgcolor="#cccccc" border="1" cellpadding="5">
<tr>
<td><b>Date</b></td>
<td><b>Venue</b></td>
<td><b>Composer</b></td>
<td><b>Work</b></td>
<td><b>Role</b></td>
</tr>
<tr>
<td>1998
</td>
<td>F
</td>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
</table>
</body>
</html>
template with parameters
File: Data.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<booklist>
<book>
<title>title 1</title>
<author>author 1</author>
<publisher>publisher 1</publisher>
<isbn>1-11-11111-1</isbn>
<price>6.99</price>
<sales>235</sales>
</book>
<book>
<title>title 2</title>
<author>author 2</author>
<publisher>publisher 2</publisher>
<isbn>0 14 018967 X</isbn>
<price>12.99</price>
<sales>12</sales>
</book>
</booklist>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs" version="2.0">
<xsl:template name="get-min-and-max">
<xsl:param name="nodes" as="node()*" />
<xsl:param name="min-so-far" select="number("INF")"
as="xs:double" />
<xsl:param name="max-so-far" select="number("-INF")"
as="xs:double" />
<xsl:choose>
<xsl:when test="$nodes">
<xsl:call-template name="get-min-and-max">
<xsl:with-param name="nodes"
select="remove($nodes, 1)" />
<xsl:with-param name="min-so-far"
select="if (number($nodes[1]) lt $min-so-far)
then $nodes[1]
else $min-so-far" />
<xsl:with-param name="max-so-far"
select="if (number($nodes[1]) gt $max-so-far)
then $nodes[1]
else $max-so-far" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:document>
<min>
<xsl:value-of select="$min-so-far" />
</min>
<max>
<xsl:value-of select="$max-so-far" />
</max>
</xsl:document>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="/">
<result>
<xsl:call-template name="get-min-and-max">
<xsl:with-param name="nodes"
select="/booklist/book/price" />
</xsl:call-template>
</result>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><result><min>6.99</min><max>12.99</max></result>