XML Tutorial/XSLT stylesheet/concat — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 11:26, 26 мая 2010
Содержание
concat function
<source lang="xml">
File: Data.xml <?xml version = "1.0"?> <stocks>
<stock symbol = "INTC"> <name>Intel Corporation</name> </stock> <stock symbol = "CSCO"> <name>Cisco Systems, Inc.</name> </stock>
</stocks>
File: Transform.xslt <?xml version = "1.0"?> <xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/stocks"> <html> <body>
-
<xsl:for-each select="stock">
<xsl:if test="starts-with(@symbol, "C")">
- <xsl:value-of select="concat(@symbol," - ", name)" />
</xsl:if> </xsl:for-each>
</body> </html> </xsl:template>
</xsl:stylesheet> Output: <html>
<body>
- CSCO - Cisco Systems, Inc.
</body>
</html></source>
Concat string together
<source lang="xml">
File: Data.xml <?xml version="1.0" encoding="UTF-8"?> <poem>
<line>line 1</line> <line>line 2</line> <line>line 3</line> <attribution>A</attribution>
</poem> 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="text" /> <xsl:template match="poem"> <xsl:value-of select="concat(line[1], " ", line[2], " ", " ", line[3], " ", " ", line[4], " ", line[5], " ", " -", attribution)" /> </xsl:template>
</xsl:stylesheet> Output: line 1 line 2
line 3 -A</source>
Function concat() can stick several strings together
<source lang="xml">
File: Data.xml <?xml version="1.0" encoding="utf-8"?>
<user firstName="John" surname="Smith"/> <user firstName="Joe" surname="Smith"/> <user firstName="Charles" surname="Smith"/>
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="//user"> <Paragraph> <xsl:value-of select="concat(@firstName," ",@surname)"/> </Paragraph> </xsl:template>
</xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?>
<Paragraph>John Smith</Paragraph> <Paragraph>Joe Smith</Paragraph> <Paragraph>Charles Smith</Paragraph></source>
select=concat(The second album is , list/listitem[2])
<source lang="xml">
File: Data.xml <?xml version="1.0"?>
<list xml:lang="en">
<title>title 1</title> <listitem>item 1</listitem> <listitem>item 2</listitem> <listitem>item 3</listitem> <listitem xml:lang="sw">item 4</listitem> <listitem xml:lang="en-gb">item 5</listitem> <listitem xml:lang="zu">item 6</listitem> <listitem xml:lang="jz">item 7</listitem>
</list> File: Transform.xslt <?xml version="1.0"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html"/> <xsl:template match="/"> <html> <head> <title>XSLT and CSS Demo</title> <style> <xsl:comment> p.big {font-size: 125%; font-weight: bold;} p.odd {color: purple; font-weight: bold;} p.even {color: blue; font-style: italic; font-weight: bold;} </xsl:comment> </style> </head> <body style="font-family: sans-serif;"> <xsl:apply-templates select="list/title"/> <xsl:comment select="concat("The second album is ", list/listitem[2])"/> <xsl:apply-templates select="list/listitem"/> </body> </html> </xsl:template> <xsl:template match="title">
<xsl:value-of select="."/>
</xsl:template> <xsl:template match="listitem"> <xsl:choose> <xsl:when test="position() mod 2">
<xsl:value-of select="."/>
</xsl:when> <xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise> </xsl:choose> </xsl:template>
</xsl:stylesheet> Output: <html>
<head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>XSLT and CSS Demo</title><style> </style></head> <body style="font-family: sans-serif;">
title 1
item 1
item 2
item 3
item 4
item 5
item 6
item 7
</body>
</html></source>
The concat function returns the concatenation of its arguments.
<source lang="xml">
File: Data.xml <?xml version="1.0" encoding="utf-8"?>
<text>Start</text> <text>Body</text> <text>Finish</text>
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:variable name="T" select="concat(//text[1]," - ",//text[2]," - ",//text[3])"/> <xsl:template match="/"> <Paragraph> <xsl:value-of select="$T"/> </Paragraph> </xsl:template>
</xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?><Paragraph>Start - Body - Finish</Paragraph></source>