XML Tutorial/XSLT stylesheet/starts with
Версия от 18:22, 25 мая 2010; (обсуждение)
Содержание
for-each select=addressbook/address[starts-with(state, M)]
File: Data.xml
<?xml version="1.0"?>
<addressbook>
<address>
<name>
<title>Mr.</title>
<first-name>Jack</first-name>
<last-name>Smith</last-name>
</name>
<street>1234 Main Street</street>
<city>New York</city>
<state>WI</state>
<zip>48392</zip>
</address>
</addressbook>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="no"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:for-each select="addressbook/address[starts-with(state, "M")]">
<xsl:sort select="name/last-name"/>
<xsl:sort select="name/first-name"/>
<xsl:if test="name/title">
<xsl:value-of select="name/title"/>
<xsl:text> </xsl:text>
</xsl:if>
<xsl:value-of select="name/first-name"/>
<xsl:text> </xsl:text>
<xsl:value-of select="name/last-name"/>
<xsl:text>
</xsl:text>
<xsl:value-of select="street"/>
<xsl:text>
</xsl:text>
<xsl:value-of select="city"/>
<xsl:text>, </xsl:text>
<xsl:value-of select="state"/>
<xsl:text> </xsl:text>
<xsl:value-of select="zip"/>
<xsl:text>
</xsl:text>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
select element starting with K
File: Data.xml
<?xml version="1.0" encoding="US-ASCII"?>
<state name="New York">
<county>Abc</county>
<county>Kent</county>
<county>Newport</county>
<county>World</county>
<county>Washington</county>
</state>
File: Transform.xslt
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:template match="/">
<county state="{state/@name}">
<xsl:apply-templates select="state" />
</county>
</xsl:template>
<xsl:template match="state">
<xsl:apply-templates select="county" />
</xsl:template>
<xsl:template match="state">
<xsl:apply-templates select="county[starts-with(.,"K")]" />
</xsl:template>
<xsl:template match="county">
<name>
<xsl:apply-templates />
</name>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<county state="New York">
<name>Kent</name>
</county>
starts-with function
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>
<ul>
<xsl:for-each select="stock">
<xsl:if test="starts-with(@symbol, "C")">
<li>
<xsl:value-of
select="concat(@symbol," - ", name)" />
</li>
</xsl:if>
</xsl:for-each>
</ul>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Output:
<html>
<body>
<ul>
<li>CSCO - Cisco Systems, Inc.</li>
</ul>
</body>
</html>
starts-with() returns true if the first argument string starts with the second argument string
File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>
<text>Welcome to XSL world.</text>
<string>Welcome</string>
<string>XSL</string>
<string>XML</string>
</data>
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 border="1">
<TR>
<TH colspan="3">
<xsl:value-of select="//text"/>
</TH>
</TR>
<TR>
<td>string</TH>
<td>starts-with</TH>
<td>contains</TH>
</TR>
<xsl:for-each select="//string">
<TR>
<TD>
<xsl:value-of select="."/>
</TD>
<TD>
<xsl:value-of select="starts-with(//text,.)"/>
</TD>
<TD>
<xsl:value-of select="contains(//text,.)"/>
</TD>
</TR>
</xsl:for-each>
</TABLE>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><TABLE border="1"><TR><TH colspan="3">Welcome to XSL world.</TH></TR><TR><td>string</TH><td>starts-with</TH><td>contains</TH></TR><TR><TD>Welcome</TD><TD>true</TD><TD>true</TD></TR><TR><TD>XSL</TD><TD>false</TD><TD>true</TD></TR><TR><TD>XML</TD><TD>false</TD><TD>false</TD></TR></TABLE>