XML/XSLT stylesheet/if — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 11:26, 26 мая 2010
Содержание
- 1 An example of if-then-else logic in XSLT 1.0
- 2 Branching
- 3 Compare value of attribute with if statement
- 4 if statement and value compare
- 5 if statement in for-each loop
- 6 if statement with and operator
- 7 Use boolean operator in if statement
- 8 <xsl:value-of select="title" />
- 9 title 1
- 10 author 1
- 11 title 3
- 12 title 4
An example of if-then-else logic in XSLT 1.0
<source lang="xml">
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:param name="x" select=""10""/> <xsl:output method="text"/> <xsl:template match="/"> <xsl:text> An example of if-then-else logic in XSLT 1.0:</xsl:text> <xsl:text> If $x is larger than 10, print "Big", </xsl:text> <xsl:text> otherwise print "Little"</xsl:text> <xsl:text> </xsl:text> <xsl:choose> <xsl:when test="$x > 10"> <xsl:text>Big</xsl:text> </xsl:when> <xsl:otherwise> <xsl:text>Little</xsl:text> </xsl:otherwise> </xsl:choose> <xsl:text> </xsl:text> </xsl:template>
</xsl:stylesheet> Output:
An example of if-then-else logic in XSLT 1.0:
If $x is larger than 10, print "Big", otherwise print "Little" Little </source>
Branching
<source lang="xml">
File: Data.xml <?xml version="1.0" standalone="no" ?> <transcript>
<student id="STU12345" name="name 1" status="active"> <home_address>35 Wall Street, Wonderland, NJ</home_address> <interests> <interest>interest 1</interest> <interest>interest 2</interest> <interest>interest 3</interest> </interests> </student> <term> <heading name="Winter 1999" /> <course> <course-name>course 1</course-name> <grade>A-</grade> <credits>4</credits> </course> <course> <course-name>course 2</course-name> <grade>A</grade> <credits>3</credits> </course> </term> <summary>summary</summary> <comments> comments </comments>
</transcript> File: Transform.xslt
<?xml version="1.0" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="transcript"> Student received A"s in the following courses: <xsl:apply-templates select="term/course[starts-with(grade,"A")]" /> </xsl:template> <xsl:template match="course"> <xsl:value-of select="course-name" /> <xsl:if test="not(position()=last())">,</xsl:if> </xsl:template>
</xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?>
Student received A"s in the following courses: course 1,course 2 </source>
Compare value of attribute with if statement
<source lang="xml">
File: Data.xml <?xml version="1.0"?> <Characters>
<Character age="1">Character 1</Character> <Character age="2">Character 2</Character> <Character age="3">Character 3</Character> <Character age="4">Character 4</Character> <Character age="5">Character 5</Character>
</Characters>
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>select</title> </head> <body>
header 3.
<xsl:apply-templates select="/Characters/Character" /> </body> </html> </xsl:template> <xsl:template match="Character"> <xsl:if test="@age > 2 "> <paragraph> <xsl:value-of select="." /> is older than expected. <xsl:value-of select="@age" /> , is correct. </paragraph> </xsl:if> </xsl:template>
</xsl:stylesheet> Output: <html>
<head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>select</title> </head> <body>
header 3.
<paragraph>Character 3 is older than expected. 3 , is correct. </paragraph> <paragraph>Character 4 is older than expected. 4 , is correct. </paragraph> <paragraph>Character 5 is older than expected. 5 , is correct. </paragraph> </body>
</html>
</source>
if statement and value compare
<source lang="xml">
File: Data.xml <poem>
<a>line 1</a> line 1 <c>line 1</c> <d> line 1 </d>
</poem> 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="a"> <xsl:if test=". = "line 1""> 1. a = "line 1" </xsl:if> <xsl:if test=". = ../b">2. a = ../b</xsl:if> <xsl:if test=". = ../c">3. a = ../c</xsl:if> <xsl:if test=". != ../c">4. a != ../c</xsl:if> <xsl:if test="translate(.,"abcdefghijklmnopqrstuvwxyz","ABCDEFGHIJKLMNOPQRSTUVWXYZ") = translate(../c,"abcdefghijklmnopqrstuvwxyz","ABCDEFGHIJKLMNOPQRSTUVWXYZ")"> 5. a = ../c (ignoring case) </xsl:if> <xsl:if test=". = ../d">6. a = ../d</xsl:if> <xsl:if test=". = normalize-space(../d)"> 7. a = normalize-space(../d) </xsl:if> </xsl:template> <xsl:template match="b|c|d" />
</xsl:stylesheet> Output:
1. a = "line 1" 2. a = ../b3. a = ../c 5. a = ../c (ignoring case) 7. a = normalize-space(../d) </source>
if statement in for-each loop
<source lang="xml">
File: Data.xml <?xml version="1.0"?> <book>
<title>title 4</title> <author>author 1</author> <author>author 2</author> <author>author 3</author> <author>author 4</author>
</book> File: Transform.xslt <?xml version="1.0"?> <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"> <xsl:template match="book"> <xsl:value-of select="title" /> by <xsl:for-each select="author"> <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:transform>
Output: <?xml version="1.0" encoding="UTF-8"?>title 4
by author 1,author 2,author 3,andauthor 4 </source>
if statement with and operator
<source lang="xml">
File: Data.xml <?xml version="1.0"?> <Characters>
<Character age="1">Character 1</Character> <Character age="2">Character 2</Character> <Character age="3">Character 3</Character> <Character age="4">Character 4</Character> <Character age="5">Character 5</Character>
</Characters> File: Transform.xslt <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.wbex.ru" xmlns:xsd="http://www.w3.org/2000/10/XMLSchema" version="1.0"> <xsl:output method="xml"/> <xsl:template match="*"> <xsl:element name="{name(.)}"> <xsl:for-each select="@*"> <xsl:if test="(name(.) != "minOccurs") and (name(.) != "maxOccurs")"> <xsl:attribute name="{name(.)}"> <xsl:value-of select="."/> </xsl:attribute> </xsl:if> </xsl:for-each> <xsl:apply-templates/> </xsl:element> </xsl:template>
</xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?><Characters xmlns="http://www.wbex.ru">
<Character age="1">Character 1</Character> <Character age="2">Character 2</Character> <Character age="3">Character 3</Character> <Character age="4">Character 4</Character> <Character age="5">Character 5</Character>
</Characters>
</source>
Use boolean operator in if statement
<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="2.0"> <xsl:template match="/"> <html> <body> <xsl:variable name="all-books" select="//book" /> <xsl:for-each select="$all-books">
<xsl:value-of select="title" />
by <xsl:value-of select="author[1]" /> <xsl:if test="count(author)!=1"> and others </xsl:if>
<xsl:variable name="others" select="$all-books[./@category=current()/@category and not(. is current())]" /> <xsl:if test="$others">
Other books in this category:
-
<xsl:for-each select="$others">
- <xsl:value-of select="title" />
</xsl:for-each>
</xsl:if> </xsl:for-each> </body> </html> </xsl:template>
</xsl:transform> Output: <html>
<body>
title 1
byauthor 1
author 1
byauthor 1
Other books in this category:
- title 3
title 3
byauthor 1
Other books in this category:
- author 1
title 4
byauthor 1 and others
</body>
</html>
</source>