XML Tutorial/XSLT stylesheet/value of
Содержание
output an element only if it has a value.
<source lang="xml">
File: Data.xml <?xml version="1.0" encoding="utf-8"?>
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="table">
</xsl:template> <xsl:template match="element"> <xsl:variable name="tmp"> <xsl:value-of select="."/> </xsl:variable> <xsl:if test="boolean(normalize-space($tmp))"> <TR> <TD> <xsl:value-of select="$tmp"/> </TD> </TR> </xsl:if> </xsl:template>
</xsl:stylesheet> Output:
<?xml version="1.0" encoding="UTF-8"?>element 1 |
element 2 |
element 3 |
Select value from an element
<source lang="xml">
File: Data.xml <?xml version = "1.0"?> <product>
<subject> Visual C++ </subject> <subject>C How to Program</subject>
</product> File: Transform.xslt <?xml version = "1.0"?> <xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/product"> <xsl:value-of select="subject" /> </xsl:template>
</xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?>
Visual C++ </source>
the output without empty elements removal
<source lang="xml">
File: Data.xml <?xml version="1.0" encoding="utf-8"?>
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="table">
</xsl:template> <xsl:template match="element"> <TR> <TD> <xsl:value-of select="."/> </TD> </TR> </xsl:template>
</xsl:stylesheet> Output:
<?xml version="1.0" encoding="UTF-8"?>element 1 |
element 2 |
element 3 |
Use value-of to get element value from different level
<source lang="xml">
File: Data.xml <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="Transform.xslt"?> <BOOK>
<TITLE>Jack</TITLE> <AUTHOR> <FIRSTNAME>Jack</FIRSTNAME> <LASTNAME>Smith</LASTNAME> </AUTHOR> <BINDING>hardcover</BINDING> <PAGES>724</PAGES> <PRICE>$9.95</PRICE>
</BOOK> File: Transform.xslt <?xml version="1.0"?> <xsl:stylesheet
version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <HTML> <HEAD> <TITLE>Book Description</TITLE> </HEAD> <BODY>
Book Description
Author: <xsl:value-of select="BOOK/AUTHOR"/>
Title: <xsl:value-of select="BOOK/TITLE"/>
Price: <xsl:value-of select="BOOK/PRICE"/>
Binding type: <xsl:value-of select="BOOK/BINDING"/>
Number of pages: <xsl:value-of select="BOOK/PAGES"/> </BODY> </HTML> </xsl:template>
</xsl:stylesheet> Output: <HTML>
<HEAD> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <TITLE>Book Description</TITLE> </HEAD> <BODY>
Book Description
Author:Jack Smith
Title: Jack
Price: $9.95
Binding type: hardcover
Number of pages: 724 </BODY>
</HTML></source>
value-of select="."
<source lang="xml">
File: Data.xml <?xml version="1.0"?> <greeting>
Hello, World!
</greeting> File: Transform.xslt
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="html"/> <xsl:template match="/"> <xsl:apply-templates select="greeting"/> </xsl:template> <xsl:template match="greeting"> <html> <body>
<xsl:value-of select="."/>
</body> </html> </xsl:template>
</xsl:stylesheet> Output: <html>
<body>
Hello, World!
</body>
</html></source>
value-of select=cars/manufacturer/@name separator=,
<source lang="xml">
File: Data.xml <?xml version="1.0" encoding="utf-8"?> <cars>
<manufacturer name="Chevrolet"> <car>Cavalier</car> <car>Corvette</car> <car>Impala</car> <car>Malibu</car> </manufacturer>
</cars> File: Transform.xslt <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:template match="/"> <xsl:value-of select="cars/manufacturer/@name" separator=", "/> </xsl:template>
</xsl:stylesheet> Output: Chevrolet</source>
value-of select=format-date($date, [M01]/[D01]/[Y0001])
<source lang="xml">
File: Transform.xslt <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xsl:output method="text"/> <xsl:template match="/"> <xsl:call-template name="date-formatter"> <xsl:with-param name="date" select="current-date()"/> </xsl:call-template> </xsl:template> <xsl:template name="date-formatter"> <xsl:param name="date" as="xs:date" required="yes"/> <xsl:value-of select="format-date($date, "[M01]/[D01]/[Y0001]")"/> </xsl:template>
</xsl:stylesheet> Output: 12/11/2008</source>
value-of select=replace($sub2, ', '')
<source lang="xml">
File: Data.xml <?xml version="1.0" encoding="utf-8"?>
- T
- S
- W
File: Transform.xslt <?xml version="1.0"?> <xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:template match="/"> <xsl:apply-templates select="ul/li"/> </xsl:template> <xsl:template match="li"> <xsl:variable name="sub1" select="replace(., "&", "^&")"/> <xsl:variable name="sub2" select="replace($sub1, "\|", "^|")"/> <xsl:value-of select="replace($sub2, "'", "''")"/> <xsl:text> </xsl:text> </xsl:template>
</xsl:stylesheet> Output: T S W</source>