XML/XSLT stylesheet/for each
Содержание
- 1 for-each loop and table rows
- 2 for-each select="child::parentTagName[@attributeName="your value"]"
- 3 for-each select="child::parentTagName", value-of select="childTagName"
- 4 for-each select="node()", value-of select="."
- 5 for-each select="*", value-of="."
- 6 Nested for-each statement
- 7 Use for-each loop
- 8 Use for-each to loop through each tags
for-each loop and table rows
<source lang="xml">
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 <html xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xsl:version="1.0">
<head> <title>Email Listing</title> </head> <body>
Name | E-mail Address |
---|---|
<xsl:value-of select="name"/> | <xsl:value-of select="email"/> |
</body>
</html> Output: <html>
<head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Email Listing</title> </head> <body>
Name | E-mail Address |
---|---|
name 1 | g@gmail.ru |
name 2 | n@hotmail.ru |
</body>
</html>
</source>
for-each select="child::parentTagName[@attributeName="your value"]"
<source lang="xml">
File: Data.xml <winelist>
<wine grape="Chardonnay"> <winery>shop 2</winery> <product>product 2</product> <year>1997</year> <prices> <list>10.99</list> <discounted>9.50</discounted> </prices> </wine> <wine grape="Cabernet"> <winery>shop 1</winery> <product>product 1</product> <year>1998</year> <prices> <list>6.99</list> <discounted>5.99</discounted> </prices> </wine>
</winelist> 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="winelist"> <xsl:for-each select="child::wine[@grape="Cabernet"]"> <xsl:value-of select="winery" /> </xsl:for-each> </xsl:template>
</xsl:stylesheet>
Output: shop 1
</source>
for-each select="child::parentTagName", value-of select="childTagName"
<source lang="xml">
File: Data.xml <winelist>
<wine grape="Chardonnay"> <winery>shop 2</winery> <product>product 2</product> <year>1997</year> <prices> <list>10.99</list> <discounted>9.50</discounted> </prices> </wine> <wine grape="Chardonnay"> <winery>shop 1</winery> <product>product 1</product> <year>1998</year> <prices> <list>6.99</list> <discounted>5.99</discounted> </prices> </wine>
</winelist> 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="winelist"> <xsl:for-each select="child::wine"> <xsl:value-of select="winery" /> </xsl:for-each> </xsl:template>
</xsl:stylesheet> Output: shop 2shop 1
</source>
for-each select="node()", value-of select="."
<source lang="xml">
File: Data.xml
<book author="jm">
<chapter>this is a test</chapter> <?xml-stylesheet href="ss.css" type="text/css" ?>
</book> 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="book"> <xsl:for-each select="node()"> <xsl:value-of select="." /> </xsl:for-each> </xsl:template>
</xsl:stylesheet> Output:
this is a test href="ss.css" type="text/css" </source>
for-each select="*", value-of="."
<source lang="xml">
File: Data.xml <book author="jm">
<chapter>this is a test</chapter> <?xml-stylesheet href="ss.css" type="text/css" ?>
</book> 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="book"> <xsl:for-each select="*"> <xsl:value-of select="." /> </xsl:for-each> </xsl:template>
</xsl:stylesheet> Output: this is a test
</source>
Nested for-each statement
<source lang="xml">
File: Data.xml <?xml version="1.0" ?> <customer-list>
<customer> <name> <first>Peter</first> <last>Whimsey</last> </name> <order>25 cases Earl Grey tea</order> <order>12 bottles brandy</order> </customer> <customer> <name> <first>Charlie</first> <last>Chan</last> </name> <order>1 Chinese-English dictionary</order> <order>5 cases mustache wax</order> </customer> <customer> <name> <first>Nora</first> <last>Charles</last> </name> <order>5 bottles wine</order> <order>24 sandwiches</order> </customer> <customer> <name> <first>Nick</first> <last>Charles</last> </name> <order>12 cases gin</order> <order>4 cartons dog food</order> </customer>
</customer-list> File: Transform.xslt <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/"> <HTML> <HEAD><TITLE>Customers</TITLE></HEAD> <BODY>
<xsl:apply-templates select="name"/> |
---|
<xsl:apply-templates/> |
</BODY> </HTML> </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>
Whimsey,
Peter |
---|
25 cases Earl Grey tea |
12 bottles brandy |
Chan,
Charlie |
1 Chinese-English dictionary |
5 cases mustache wax |
Charles,
Nora |
5 bottles wine |
24 sandwiches |
Charles,
Nick |
12 cases gin |
4 cartons dog food |
</BODY>
</HTML>
</source>
Use for-each loop
<source lang="xml">
File: Data.xml <?xml version="1.0"?> <Objects>
<Object name="Car"> <Characteristic>A</Characteristic> <Characteristic>B</Characteristic> <Characteristic>C</Characteristic> <Characteristic>D</Characteristic> </Object> <Object name="Orange"> <Characteristic>1</Characteristic> <Characteristic>2</Characteristic> <Characteristic>3</Characteristic> <Characteristic>4</Characteristic> </Object>
</Objects>
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>title</title> </head> <body>
Characteristics of <xsl:value-of select="Objects/Object/@name" />
<xsl:apply-templates select="/Objects/Object" /> </body> </html> </xsl:template> <xsl:template match="Object">
-
<xsl:for-each select="Characteristic">
- <xsl:value-of select="." />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet> Output: <html>
<head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>title</title> </head> <body>
Characteristics of Car
- A
- B
- C
- D
- 1
- 2
- 3
- 4
</body>
</html>
</source>
Use for-each to loop through each tags
<source lang="xml">
File: Data.xml <?xml version="1.0" encoding="utf-8"?> <?xml-stylesheet type="text/xsl" href="Transform.xslt"?> <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="html"/>
<xsl:template match="/"> <html> <head> <title>Email Listing</title> </head> <body>
Name | E-mail Address |
---|---|
<xsl:value-of select="name"/> | <xsl:value-of select="email"/> |
</body> </html> </xsl:template>
</xsl:stylesheet>
Output: <html>
<head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Email Listing</title> </head> <body>
Name | E-mail Address |
---|---|
person1 | p@hotmail.ru |
person2 | p@hotmail.ru |
person3 | p3@hotmail.ru |
</body>
</html>
</source>