XML/XSLT stylesheet/select — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 11:26, 26 мая 2010
Содержание
- 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 Sales volume by publisher
- 11 Sales volume by publisher
- 12 <xsl:value-of select="title" />
- 13 title 1
- 14 author 1
- 15 title 3
- 16 title 4
- 16.1 select="employee[@dept="programming"]"
- 16.2 select="employees/employee[2]/following::contact/name/firstName"
- 16.3 select="employees/employee[2]/preceding::contact/name/firstName"
- 16.4 select="employees/head:header/namespace::head"
- 16.5 Select Node by index
- 16.6 Select one from the target value list
- 17 <xsl:apply-templates />
child
<source lang="xml">
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>
</source>
context position and context size
<source lang="xml">
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>
Context position and context size.
<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>
Context position and context size.
the first chapterthe second chapterthe third chapterthe fourth chapterthe fifth chapter</body>
</html>
</source>
Get value from tag with {}
<source lang="xml">
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>
</source>
Node selection by level
<source lang="xml">
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>
<xsl:value-of select="/Book/Title" />
by <xsl:apply-templates select="/Book/Authors/Author" />
Table of Contents
<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>
this is the title
by ABC
Table of Contents
chapter 1chapter 2chapter 3chapter 4chapter 5chapter 1chapter 2chapter 3chapter 4chapter 5</body>
</html>
</source>
Parent and attribute
<source lang="xml">
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 </source>
select="@*" (at)
<source lang="xml">
File: Data.xml <HTML>
<Head> <TITLE>This is a mixed-case HTML-like XML document</TITLE> </Head> <Body>
Some HTML paragraph
<record>one</record> <record>two</record>
- alpha
- beta
- gamma
</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>
</source>
select="../@attribute"
<source lang="xml">
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 </source>
Select attribute value and output to a list
<source lang="xml">
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="person">
- Born: <xsl:apply-templates select="@born"/>
- Died: <xsl:apply-templates select="@died"/>
</xsl:template>
</xsl:stylesheet> Output: <html>
<head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Famous Scientists</title> </head> <body>
- A B
-
- Born: 1912
- Died: 1954
- F G H
-
- Born: 2008
- Died: 2008
</body>
</html>
</source>
select distinct values
<source lang="xml">
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>
Sales volume by publisher
Publisher | Total Sales Value |
---|---|
<xsl:value-of select="." /> |
<xsl:call-template name="total-sales" /> |
</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>
Sales volume by publisher
Publisher | Total Sales Value |
---|---|
publisher 1 | 235 |
publisher 2 | 12 |
</body>
</html>
</source>
select="document("")/*/book:category[@code=current()/@category]/@desc"
<source lang="xml">
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">
<xsl:value-of select="title" />
Category: <xsl:value-of select="document("")/*/book:category[@code=current()/@category]/@desc" />
</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>
title 1
Category: Science
author 1
Category: Children"s Fiction
title 3
Category: Children"s Fiction
title 4
Category: Computing
</body>
</html>
</source>
select="employee[@dept="programming"]"
<source lang="xml">
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>
Number |
<xsl:value-of select="employee/contact/phone/tel/attribute::type" /> |
---|---|
<xsl:number /> |
<xsl:value-of select="contact/phone/tel[2]" /> |
</xsl:for-each>
</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 </source>
select="employees/employee[2]/following::contact/name/firstName"
<source lang="xml">
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>
The employee element nodes that are defined after developer 1 are:
- <xsl:value-of select="employees/employee[2]/following::contact/name/firstName" />
</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>
The employee element nodes that are defined after developer 1 are:
</body>
</html>
</source>
select="employees/employee[2]/preceding::contact/name/firstName"
<source lang="xml">
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>
The employee element nodes that are defined before developer 1 are:
- <xsl:value-of select="employees/employee[2]/preceding::contact/name/firstName" />
</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>
The employee element nodes that are defined before developer 1 are:
</body>
</html>
</source>
select="employees/head:header/namespace::head"
<source lang="xml">
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
</source>
Select Node by index
<source lang="xml">
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>
header 3
<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>
header 3
<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>
</source>
Select one from the target value list
<source lang="xml">
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">
<xsl:apply-templates select="SPEAKER" /> |
<xsl:apply-templates select="STAGEDIR|LINE" /> |
</xsl:template> <xsl:template match="TITLE">
<xsl:apply-templates />
<xsl:apply-templates />
</xsl:template> <xsl:template match="SPEAKER"> <xsl:apply-templates /> <xsl:if test="not(position()=last())">
</xsl:if> </xsl:template> <xsl:template match="SCENE/STAGEDIR">
<xsl:apply-templates />
</xsl:template> <xsl:template match="SPEECH/STAGEDIR">
<xsl:apply-templates />
</xsl:template> <xsl:template match="LINE/STAGEDIR"> [ <xsl:apply-templates /> ] </xsl:template> <xsl:template match="SCENE/SUBHEAD">
<xsl:apply-templates />
</xsl:template> <xsl:template match="SPEECH/SUBHEAD">
<xsl:apply-templates />
</xsl:template> <xsl:template match="LINE"> <xsl:apply-templates />
</xsl:template>
</xsl:stylesheet> Output: <HTML>
<HEAD> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <TITLE></TITLE> </HEAD> <BODY BGCOLOR="#FFFFCC">
A |
B |
</BODY>
</HTML>
</source>
Select one tag from a list of tags
<source lang="xml">
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
</source>
select with if then else
<source lang="xml">
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"/>
</source>
value-of select="person[position()=3]/name"
<source lang="xml">
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
</source>