XML Tutorial/XSLT stylesheet/value of
Содержание
- 1 output an element only if it has a value.
- 2 Select value from an element
- 3 the output without empty elements removal
- 4 Use value-of to get element value from different level
- 5 value-of select="."
- 6 value-of select=cars/manufacturer/@name separator=,
- 7 value-of select=format-date($date, [M01]/[D01]/[Y0001])
- 8 value-of select=replace($sub2, ', '')
output an element only if it has a value.
File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<table>
<element> element 1 </element>
<element> element 2 </element>
<element/>
<element/>
<element> element 3</element>
</table>
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">
<TABLE BORDER="1">
<xsl:for-each select="element">
<xsl:apply-templates select="."/>
</xsl:for-each>
</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"?><TABLE BORDER="1"><TR><TD> element 1 </TD></TR><TR><TD> element 2 </TD></TR><TR><TD> element 3</TD></TR></TABLE>
Select value from an element
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">
<em>
<xsl:value-of select="subject" />
</em>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><em>
Visual C++
</em>
the output without empty elements removal
File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<table>
<element> element 1 </element>
<element> element 2 </element>
<element/>
<element/>
<element> element 3</element>
</table>
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">
<TABLE BORDER="1">
<xsl:for-each select="element">
<xsl:apply-templates select="."/>
</xsl:for-each>
</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"?><TABLE BORDER="1"><TR><TD> element 1 </TD></TR><TR><TD> element 2 </TD></TR><TR><TD/></TR><TR><TD/></TR><TR><TD> element 3</TD></TR></TABLE>
Use value-of to get element value from different level
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="/"> <!-- match the XSLT root node -->
<HTML>
<HEAD>
<TITLE>Book Description</TITLE>
</HEAD>
<BODY>
<H2>Book Description</H2>
<SPAN STYLE="font-style:italic">Author: </SPAN>
<xsl:value-of select="BOOK/AUTHOR"/><BR/>
<SPAN STYLE="font-style:italic">Title: </SPAN>
<xsl:value-of select="BOOK/TITLE"/><BR/>
<SPAN STYLE="font-style:italic">Price: </SPAN>
<xsl:value-of select="BOOK/PRICE"/><BR/>
<SPAN STYLE="font-style:italic">Binding type: </SPAN>
<xsl:value-of select="BOOK/BINDING"/><BR/>
<SPAN STYLE="font-style:italic">Number of pages: </SPAN>
<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>
<H2>Book Description</H2><SPAN STYLE="font-style:italic">Author: </SPAN>
Jack
Smith
<BR><SPAN STYLE="font-style:italic">Title: </SPAN>Jack<BR><SPAN STYLE="font-style:italic">Price: </SPAN>$9.95<BR><SPAN STYLE="font-style:italic">Binding type: </SPAN>hardcover<BR><SPAN STYLE="font-style:italic">Number of pages: </SPAN>724
</BODY>
</HTML>
value-of select="."
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>
<h1>
<xsl:value-of select="."/>
</h1>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Output:
<html>
<body>
<h1>
Hello, World!
</h1>
</body>
</html>
value-of select=cars/manufacturer/@name separator=,
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
value-of select=format-date($date, [M01]/[D01]/[Y0001])
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
value-of select=replace($sub2, ', '')
File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<ul>
<li>T</li>
<li>S</li>
<li>W</li>
</ul>
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