XML Tutorial/XSLT stylesheet/name
Версия от 18:22, 25 мая 2010; (обсуждение)
Содержание
An example of use of function name()
File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>
<date year="1999" month="11" day="23"/>
<weight kg="24" g="314"/>
</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>
<xsl:for-each select="//*[@*]">
<xsl:call-template name="elementTemplate"/>
</xsl:for-each>
</TABLE>
</xsl:template>
<xsl:template name="elementTemplate">
<TR>
<td>
<xsl:value-of select="name(.)"/>
</TH>
<TD>
<xsl:call-template name="attributeTemplate"/>
</TD>
</TR>
</xsl:template>
<xsl:template name="attributeTemplate">
<xsl:for-each select="@*">
<xsl:value-of select="name()"/>
<xsl:text>=</xsl:text>
<xsl:value-of select="."/>
<xsl:text/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><TABLE><TR><td>date</TH><TD>year=1999month=11day=23</TD></TR><TR><td>weight</TH><TD>kg=24g=314</TD></TR></TABLE>
get the name of currently selected element with name function
File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>
<contact>
<name>John</name>
<street>Long street</street>
<number>7</number>
<tel>
<home>25252511</home>
<work>88888888</work>
</tel>
</contact>
<contact>
<name>Joe</name>
<street>Short avenue</street>
<number>75</number>
<tel>
<home>21111111</home>
<work>111111111</work>
</tel>
</contact>
</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>
<xsl:for-each select="//*">
<xsl:call-template name="generalTemplate"/>
</xsl:for-each>
</TABLE>
</xsl:template>
<xsl:template name="generalTemplate">
<TR>
<TD>
<xsl:value-of select="name(.)"/>
</TD>
</TR>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><TABLE><TR><TD>data</TD></TR><TR><TD>contact</TD></TR><TR><TD>name</TD></TR><TR><TD>street</TD></TR><TR><TD>number</TD></TR><TR><TD>tel</TD></TR><TR><TD>home</TD></TR><TR><TD>work</TD></TR><TR><TD>contact</TD></TR><TR><TD>name</TD></TR><TR><TD>street</TD></TR><TR><TD>number</TD></TR><TR><TD>tel</TD></TR><TR><TD>home</TD></TR><TR><TD>work</TD></TR></TABLE>
name() function
File: Data.xml
<book>
<chapter>
<title>From Book I</title>
<para>para1</para>
<para>para2</para>
</chapter>
<afterword>
<para>para 3</para>
</afterword>
<appendix>
<title>Glossary</title>
<para>A</para>
<para>B</para>
</appendix>
</book>
File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html" />
<xsl:template match="chapter | appendix | afterword">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="*">
<h1>
<xsl:value-of select="name()" />
ELEMENT UNACCOUNTED FOR BY STYLESHEET:
<xsl:apply-templates />
</h1>
</xsl:template>
<xsl:template match="book">
<html>
<body>
<xsl:apply-templates />
</body>
</html>
</xsl:template>
<xsl:template match="title">
<h1>
<xsl:apply-templates />
</h1>
</xsl:template>
<xsl:template match="chapter/para">
<paragraph>
<font face="times">
<xsl:apply-templates />
</font>
</p>
</xsl:template>
<xsl:template match="appendix/para">
<paragraph>
<font face="arial">
<xsl:apply-templates />
</font>
</p>
</xsl:template>
</xsl:stylesheet>
Output:
<html>
<body>
<h1>From Book I</h1>
<paragraph><font face="times">para1</font></p>
<paragraph><font face="times">para2</font></p>
<h1>para
ELEMENT UNACCOUNTED FOR BY STYLESHEET:
para 3
</h1>
<h1>Glossary</h1>
<paragraph><font face="arial">A</font></p>
<paragraph><font face="arial">B</font></p>
</body>
</html>
name()- Takes zero or one node-set arguments and returns the name of the node in prefix:localpart format
File: Data.xml
<list>
<freezer>
<element>peas</element>
<element>green beans</element>
<element>pot pie</element>
<element>ice cream</element>
</freezer>
<bakery>
<element>rolls</element>
<element>jelly doughnuts</element>
<element>bread</element>
</bakery>
<produce>
<element>bananas</element>
<element>kumquats</element>
<element>apples</element>
</produce>
</list>
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="xml" indent="yes" />
<xsl:template match="list">
<xsl:copy>
<xsl:apply-templates select="*">
<xsl:sort select="name()" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates select="element">
<xsl:sort />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="element">
<xsl:copy-of select="." />
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<list>
<bakery>
<element>bread</element>
<element>jelly doughnuts</element>
<element>rolls</element>
</bakery>
<freezer>
<element>green beans</element>
<element>ice cream</element>
<element>peas</element>
<element>pot pie</element>
</freezer>
<produce>
<element>apples</element>
<element>bananas</element>
<element>kumquats</element>
</produce>
</list>
prints names of all elements used in the document
File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>
<contact>
<name>John</name>
<street>Long street </street>
<number>7</number>
<tel>
<home>25252511</home>
<work>88888888</work>
</tel>
</contact>
<contact>
<name>Joe</name>
<street>Short avenue </street>
<number>75</number>
<tel>
<home>21111111</home>
<work>111111111</work>
</tel>
</contact>
</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>
<xsl:for-each select="//*">
<xsl:call-template name="generalTemplate"/>
</xsl:for-each>
</TABLE>
</xsl:template>
<xsl:template name="generalTemplate">
<TR>
<TD>
<xsl:value-of select="name(.)"/>
</TD>
</TR>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><TABLE><TR><TD>data</TD></TR><TR><TD>contact</TD></TR><TR><TD>name</TD></TR><TR><TD>street</TD></TR><TR><TD>number</TD></TR><TR><TD>tel</TD></TR><TR><TD>home</TD></TR><TR><TD>work</TD></TR><TR><TD>contact</TD></TR><TR><TD>name</TD></TR><TR><TD>street</TD></TR><TR><TD>number</TD></TR><TR><TD>tel</TD></TR><TR><TD>home</TD></TR><TR><TD>work</TD></TR></TABLE>