XML/XSLT stylesheet/select
Содержание
- 1 child
- 2 context position and context size
- 3 Get value from tag with {}
- 4 Node selection by level
- 5 Parent and attribute
- 6 select="@*" (at)
- 7 select="../@attribute"
- 8 Select attribute value and output to a list
- 9 select distinct values
- 10 select="document("")/*/book:category[@code=current()/@category]/@desc"
- 11 select="employee[@dept="programming"]"
- 12 select="employees/employee[2]/following::contact/name/firstName"
- 13 select="employees/employee[2]/preceding::contact/name/firstName"
- 14 select="employees/head:header/namespace::head"
- 15 Select Node by index
- 16 Select one from the target value list
- 17 Select one tag from a list of tags
- 18 select with if then else
- 19 value-of select="person[position()=3]/name"
child
File: Data.xml
<wine grape="A">
<winery>B</winery>
<year>1998</year>
<prices>
<list>13.99</list>
<discounted>11.99</discounted>
<case>143.50</case>
</prices>
</wine>
File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="wine">
<wine vintage="{child::year}">
<xsl:apply-templates select="product" />
<category>
<xsl:value-of select="@grape" />
</category>
<xsl:apply-templates select="price" />
</wine>
</xsl:template>
<xsl:template
match="@*|node()|processing-instruction()|comment()">
<xsl:copy>
<xsl:apply-templates
select="@*|node()|processing-instruction()|comment()" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><wine vintage="1998"><category>A</category></wine>
context position and context size
File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<Book>
<Chapter number="c1">the first chapter</Chapter>
<Chapter number="c2">the second chapter</Chapter>
<Chapter number="c3">the third chapter</Chapter>
<Chapter number="c4">the fourth chapter</Chapter>
<Chapter number="c5">the fifth chapter</Chapter>
</Book>
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>context position and context size.</title>
</head>
<body>
<h3>Context position and context size.</h3>
<xsl:apply-templates select="/Book/Chapter" />
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Output:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>context position and context size.</title>
</head>
<body>
<h3>Context position and context size.</h3>the first chapterthe second chapterthe third chapterthe fourth chapterthe fifth chapter
</body>
</html>
Get value from tag with {}
File: Data.xml
<wine grape="A">
<winery>B</winery>
<year>1998</year>
<prices>
<list>13.99</list>
<discounted>11.99</discounted>
<case>143.50</case>
</prices>
</wine>
File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="wine">
<wine vintage="{year}">
<xsl:apply-templates select="product" />
<category>
<xsl:value-of select="@grape" />
</category>
<xsl:apply-templates select="price" />
</wine>
</xsl:template>
<xsl:template
match="@*|node()|processing-instruction()|comment()">
<xsl:copy>
<xsl:apply-templates
select="@*|node()|processing-instruction()|comment()" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><wine vintage="1998"><category>A</category></wine>
Node selection by level
File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<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="/">
<html>
<head>
<title>
<xsl:value-of select="/Book/Title" />
</title>
</head>
<body>
<h3>
<xsl:value-of select="/Book/Title" />
</h3>
<p>
by
<xsl:apply-templates select="/Book/Authors/Author" />
</p>
<h3>Table of Contents</h3>
<xsl:apply-templates select="/Book/Chapters/Chapter"
mode="TOC" />
<xsl:apply-templates select="/Book/Chapters/Chapter"
mode="fulltext" />
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Output:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>this is the title</title>
</head>
<body>
<h3>this is the title</h3>
<p>
by
ABC
</p>
<h3>Table of Contents</h3>chapter 1chapter 2chapter 3chapter 4chapter 5chapter 1chapter 2chapter 3chapter 4chapter 5
</body>
</html>
Parent and attribute
File: Data.xml
<wine grape="A">
<winery>B</winery>
<year>1998</year>
<prices>
<list>13.99</list>
<discounted>11.99</discounted>
<case>143.50</case>
</prices>
</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="prices">
parent element"s grape:
<xsl:value-of select="parent::wine/attribute::grape" />
</xsl:template>
</xsl:stylesheet>
Output:
B
1998
parent element"s grape:
A
select="@*" (at)
File: Data.xml
<HTML>
<Head>
<TITLE>This is a mixed-case HTML-like XML document</TITLE>
</Head>
<Body>
<p>
Some
<I>HTML</I>
paragraph
</p>
<data>
<record>one</record>
<record>two</record>
</data>
<UL>
<LI>alpha</LI>
<LI>beta</LI>
<Li>gamma</Li>
</UL>
</Body>
</HTML>
File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:strip-space elements="head body" />
<xsl:template
match="html | head | title | body | p | ul | li | b | i"
priority="2">
<xsl:copy>
<xsl:copy-of select="@*" />
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
<xsl:template match="*" priority="1" />
</xsl:stylesheet>
select="../@attribute"
File: Data.xml
<wine grape="Cabernet Sauvignon">
<winery>Los Vascos</winery>
<year>1998</year>
<prices>
<list>13.99</list>
<discounted>11.99</discounted>
<case>143.50</case>
</prices>
</wine>
File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="prices">
parent element"s grape:
<xsl:value-of select="../@grape" />
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
Los Vascos
1998
parent element"s grape:
Cabernet Sauvignon
Select attribute value and output to a list
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>
<dl>
<xsl:apply-templates/>
</dl>
</body>
</html>
</xsl:template>
<xsl:template match="person">
<dt><xsl:apply-templates select="name"/></dt>
<dd><ul>
<li>Born: <xsl:apply-templates select="@born"/></li>
<li>Died: <xsl:apply-templates select="@died"/></li>
</ul></dd>
</xsl:template>
</xsl:stylesheet>
Output:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Famous Scientists</title>
</head>
<body>
<dl>
<dt>
A
B
</dt>
<dd>
<ul>
<li>Born: 1912</li>
<li>Died: 1954</li>
</ul>
</dd>
<dt>
F
G
H
</dt>
<dd>
<ul>
<li>Born: 2008</li>
<li>Died: 2008</li>
</ul>
</dd>
</dl>
</body>
</html>
select distinct values
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:key name="pub" match="book" use="publisher" />
<xsl:variable name="in" select="/" />
<xsl:variable name="publishers" as="xs:string*"
select="distinct-values(/booklist/book/publisher)" />
<xsl:template match="/">
<html>
<head>
<title>Sales volume by publisher</title>
</head>
<body>
<h1>Sales volume by publisher</h1>
<table id="{generate-id(.)}">
<tr>
<th>Publisher</th>
<th>Total Sales Value</th>
</tr>
<xsl:for-each select="$publishers">
<tr>
<td>
<xsl:value-of select="." />
</td>
<td>
<xsl:call-template name="total-sales" />
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
<xsl:template name="total-sales">
<xsl:param name="publisher" select="." />
<xsl:value-of select="sum($in/key("pub",$publisher)/sales)" />
</xsl:template>
</xsl:stylesheet>
Output:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Sales volume by publisher</title>
</head>
<body>
<h1>Sales volume by publisher</h1>
<table id="d2">
<tr>
<th>Publisher</th>
<th>Total Sales Value</th>
</tr>
<tr>
<td>publisher 1</td>
<td>235</td>
</tr>
<tr>
<td>publisher 2</td>
<td>12</td>
</tr>
</table>
</body>
</html>
select="document("")/*/book:category[@code=current()/@category]/@desc"
File: Data.xml
<?xml version="1.0"?>
<booklist>
<book category="S">
<title>title 1</title>
<author>author 1</author>
</book>
<book category="FC">
<title>author 1</title>
<author>author 1</author>
</book>
<book category="FC">
<title>title 3</title>
<author>author 1</author>
</book>
<book category="CS">
<title>title 4</title>
<author>author 1</author>
<author>author 2</author>
<author>author 3</author>
<author>author 4</author>
</book>
</booklist>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0" xmlns:book="books.uri" exclude-result-prefixes="book">
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="//book">
<h1>
<xsl:value-of select="title" />
</h1>
<p>
Category:
<xsl:value-of select="document("")/*/book:category[@code=current()/@category]/@desc" />
</p>
</xsl:for-each>
</body>
</html>
</xsl:template>
<book:category code="S" desc="Science" />
<book:category code="CS" desc="Computing" />
<book:category code="FC" desc="Children"s Fiction" />
</xsl:transform>
Output:
<html>
<body>
<h1>title 1</h1>
<p>
Category:
Science
</p>
<h1>author 1</h1>
<p>
Category:
Children"s Fiction
</p>
<h1>title 3</h1>
<p>
Category:
Children"s Fiction
</p>
<h1>title 4</h1>
<p>
Category:
Computing
</p>
</body>
</html>
select="employee[@dept="programming"]"
File: Data.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="Transform.xslt" type="text/xsl"?>
<employees xmlns="http://www.domain.ru/namespace/employee">
<title>Employee Data File</title>
<employee eid="1" dept="programming">
<contact addInfo="info1">
<name>
<firstName>Joe</firstName>
<middleName int="B">Brian</middleName>
<lastName>Smith</lastName>
</name>
<address>
<street>1 Drive</street>
<city>Vancouver</city>
<state>BC</state>
<zipcode>80210</zipcode>
</address>
<phone>
<tel type="wk">111-1111111</tel>
<tel type="hm">222-222222</tel>
<fax>303-4667357</fax>
</phone>
<email>a@a.ru</email>
</contact>
<hireDate>2008-10-29</hireDate>
</employee>
</employees>
File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="employees">
<html>
<head>
<title>Employee Email List</title>
</head>
<body>
<table>
<tr>
<th>Number</th>
<th>
<xsl:value-of
select="employee/contact/phone/tel/attribute::type" />
</th>
</tr>
<tr>
<xsl:for-each
select="employee[@dept="programming"]">
<td>
<xsl:number />
</td>
<td>
<xsl:value-of
select="contact/phone/tel[2]" />
</td>
</xsl:for-each>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
Employee Data File
Joe
Brian
Smith
1 Drive
Vancouver
BC
80210
111-1111111
222-222222
303-4667357
a@a.ru
2008-10-29
select="employees/employee[2]/following::contact/name/firstName"
File: Data.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="Transform.xslt" type="text/xsl"?>
<employees xmlns="http://www.domain.ru/namespace/employee">
<title>Employee Data File</title>
<employee eid="1" dept="programming">
<contact addInfo="info1">
<name>
<firstName>Joe</firstName>
<middleName int="B">Brian</middleName>
<lastName>Smith</lastName>
</name>
<address>
<street>1 Drive</street>
<city>Vancouver</city>
<state>BC</state>
<zipcode>80210</zipcode>
</address>
<phone>
<tel type="wk">111-1111111</tel>
<tel type="hm">222-222222</tel>
<fax>303-4667357</fax>
</phone>
<email>a@a.ru</email>
</contact>
<hireDate>2008-10-29</hireDate>
</employee>
</employees>
File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html" indent="yes" />
<xsl:template match="/">
<html>
<head>
<title>Employee Output</title>
</head>
<body>
<p>
The employee element nodes that are defined after
developer 1 are:
</p>
<ul>
<li>
<xsl:value-of
select="employees/employee[2]/following::contact/name/firstName" />
</li>
</ul>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Output:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Employee Output</title>
</head>
<body>
<p>
The employee element nodes that are defined after
developer 1 are:
</p>
<ul>
<li></li>
</ul>
</body>
</html>
select="employees/employee[2]/preceding::contact/name/firstName"
File: Data.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="Transform.xslt" type="text/xsl"?>
<employees xmlns="http://www.domain.ru/namespace/employee">
<title>Employee Data File</title>
<employee eid="1" dept="programming">
<contact addInfo="info1">
<name>
<firstName>Joe</firstName>
<middleName int="B">Brian</middleName>
<lastName>Smith</lastName>
</name>
<address>
<street>1 Drive</street>
<city>Vancouver</city>
<state>BC</state>
<zipcode>80210</zipcode>
</address>
<phone>
<tel type="wk">111-1111111</tel>
<tel type="hm">222-222222</tel>
<fax>303-4667357</fax>
</phone>
<email>a@a.ru</email>
</contact>
<hireDate>2008-10-29</hireDate>
</employee>
</employees>
File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html" indent="yes" />
<xsl:template match="/">
<html>
<head>
<title>Employee Output</title>
</head>
<body>
<p>
The employee element nodes that are defined before
developer 1 are:
</p>
<ul>
<li>
<xsl:value-of
select="employees/employee[2]/preceding::contact/name/firstName" />
</li>
</ul>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Output:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Employee Output</title>
</head>
<body>
<p>
The employee element nodes that are defined before
developer 1 are:
</p>
<ul>
<li></li>
</ul>
</body>
</html>
select="employees/head:header/namespace::head"
File: Data.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="Transform.xslt" type="text/xsl"?>
<employees>
<head:header xmlns:head="http://www.domain.ru/namespace/header">
<title>Employee Data File</title>
<maintainer>developer 1</maintainer>
</head:header>
<employee eid="1" dept="programming">
<contact addInfo="info1">
<name>
<firstName>Joe</firstName>
<middleName int="B">Brian</middleName>
<lastName>Smith</lastName>
</name>
<address>
<street>1 Drive</street>
<city>Vancouver</city>
<state>BC</state>
<zipcode>80210</zipcode>
</address>
<phone>
<tel type="wk">111-1111111</tel>
<tel type="hm">222-222222</tel>
<fax>303-4667357</fax>
</phone>
<email>a@a.ru</email>
</contact>
<hireDate>2008-10-29</hireDate>
</employee>
</employees>
File: Transform.xslt
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:head="http://www.domain.ru/namespace/header"
version="1.0">
<xsl:template match="/">
<xsl:value-of select="employees/head:header/namespace::head"/>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>http://www.domain.ru/namespace/header
Select Node by index
File: Data.xml
<Employees>
<Person>
<FirstName>A</FirstName>
<LastName>B</LastName>
<DateOfBirth>2008-12-12</DateOfBirth>
</Person>
<Person>
<FirstName>C</FirstName>
<LastName>D</LastName>
<DateOfBirth>2008-11-11</DateOfBirth>
</Person>
<Person>
<FirstName>E</FirstName>
<LastName>F</LastName>
<DateOfBirth>2008-10-10</DateOfBirth>
</Person>
</Employees>
File: Transform.xslt
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head>
<title>this is the title</title>
</head>
<body>
<h3>header 3</h3>
<xsl:apply-templates
select="/Employees/Person[1]/FirstName" />
</body>
</html>
</xsl:template>
<xsl:template match="FirstName">
<xsl:for-each select="following::*">
<paragraph>
<xsl:value-of select="name(.)" />
which contains the text "
<xsl:value-of select="." />
".
</paragraph>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Output:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>this is the title</title>
</head>
<body>
<h3>header 3</h3>
<paragraph>LastName
which contains the text "
B
".
</paragraph>
<paragraph>DateOfBirth
which contains the text "
2008-12-12
".
</paragraph>
<paragraph>Person
which contains the text "
C
D
2008-11-11
".
</paragraph>
<paragraph>FirstName
which contains the text "
C
".
</paragraph>
<paragraph>LastName
which contains the text "
D
".
</paragraph>
<paragraph>DateOfBirth
which contains the text "
2008-11-11
".
</paragraph>
<paragraph>Person
which contains the text "
E
F
2008-10-10
".
</paragraph>
<paragraph>FirstName
which contains the text "
E
".
</paragraph>
<paragraph>LastName
which contains the text "
F
".
</paragraph>
<paragraph>DateOfBirth
which contains the text "
2008-10-10
".
</paragraph>
</body>
</html>
Select one from the target value list
File: Data.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<SCENE REF="4.3">
<SPEECH>
<SPEAKER>A</SPEAKER>
I
<NL />
</SPEECH>
<SPEECH>
<SPEAKER>B</SPEAKER>
O
<NL />
</SPEECH>
</SCENE>
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="backcolor" select=""#FFFFCC"" />
<xsl:template match="SCENE|PROLOGUE|EPILOGUE">
<HTML>
<HEAD>
<TITLE>
<xsl:value-of select="TITLE" />
</TITLE>
</HEAD>
<BODY BGCOLOR="{$backcolor}">
<xsl:apply-templates />
</BODY>
</HTML>
</xsl:template>
<xsl:template match="SPEECH">
<TABLE>
<TR>
<TD WIDTH="160" VALIGN="TOP">
<xsl:apply-templates select="SPEAKER" />
</TD>
<TD VALIGN="TOP">
<xsl:apply-templates select="STAGEDIR|LINE" />
</TD>
</TR>
</TABLE>
</xsl:template>
<xsl:template match="TITLE">
<H1>
<CENTER>
<xsl:apply-templates />
</CENTER>
</H1>
<HR />
</xsl:template>
<xsl:template match="SPEAKER">
<B>
<xsl:apply-templates />
<xsl:if test="not(position()=last())">
<BR />
</xsl:if>
</B>
</xsl:template>
<xsl:template match="SCENE/STAGEDIR">
<CENTER>
<H3>
<xsl:apply-templates />
</H3>
</CENTER>
</xsl:template>
<xsl:template match="SPEECH/STAGEDIR">
<P>
<I>
<xsl:apply-templates />
</I>
</P>
</xsl:template>
<xsl:template match="LINE/STAGEDIR">
[
<I>
<xsl:apply-templates />
</I>
]
</xsl:template>
<xsl:template match="SCENE/SUBHEAD">
<CENTER>
<H3>
<xsl:apply-templates />
</H3>
</CENTER>
</xsl:template>
<xsl:template match="SPEECH/SUBHEAD">
<P>
<B>
<xsl:apply-templates />
</B>
</P>
</xsl:template>
<xsl:template match="LINE">
<xsl:apply-templates />
<BR />
</xsl:template>
</xsl:stylesheet>
Output:
<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<TITLE></TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFCC">
<TABLE>
<TR>
<TD WIDTH="160" VALIGN="TOP"><B>A</B></TD>
<TD VALIGN="TOP"></TD>
</TR>
</TABLE>
<TABLE>
<TR>
<TD WIDTH="160" VALIGN="TOP"><B>B</B></TD>
<TD VALIGN="TOP"></TD>
</TR>
</TABLE>
</BODY>
</HTML>
Select one tag from a list of tags
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:template match="wine">
<xsl:value-of select="price" />
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>10.99
select with if then else
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"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="schema-version" select="4.0" />
<xsl:template match="/">
<promotion>
<xsl:variable name="attname"
select="if ($schema-version lt 3.0)
then "code"
else "reason-code"" />
<xsl:attribute name="{$attname}" select="17" />
</promotion>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><promotion reason-code="17"/>
value-of select="person[position()=3]/name"
File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<emailList>
<head:header xmlns:head="http://www.domain.ru/namespace/header">
<title>Email List</title>
<maintainer>Joe</maintainer>
</head:header>
<person type="personal" id="p001">
<name>person1</name>
<email>p@hotmail.ru</email>
</person>
<person type="work" id="p002">
<name>person2</name>
<email>p@hotmail.ru</email>
</person>
<person type="personal" id="p003">
<name>person3</name>
<email>p3@hotmail.ru</email>
</person>
</emailList>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:head="http://www.domain.ru/namespace/header">
<xsl:output method="xml" indent="yes" />
<xsl:template match="/">
<xsl:apply-templates select="emailList" />
</xsl:template>
<xsl:template match="emailList">
<xsl:value-of select="person[position()=3]/name" />
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>person3