XML Tutorial/XSLT stylesheet/name
Содержание
- 1 An example of use of function name()
- 2 get the name of currently selected element with name function
- 3 name() function
- 4 <xsl:value-of select="name()" /> ELEMENT UNACCOUNTED FOR BY STYLESHEET: <xsl:apply-templates />
- 5 <xsl:apply-templates />
- 6 From Book I
- 7 para ELEMENT UNACCOUNTED FOR BY STYLESHEET: para 3
- 8 Glossary
An example of use of function name()
<source lang="xml">
File: Data.xml <?xml version="1.0" encoding="utf-8"?>
<date year="1999" month="11" day="23"/> <weight kg="24" g="314"/>
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="/">
</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"?>date</TH><TD>year=1999month=11day=23 |
</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"?>data |
contact |
name |
street |
number |
tel |
home |
work |
contact |
name |
street |
number |
tel |
home |
work |
name() function
<source lang="xml">
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="*">
<xsl:value-of select="name()" /> ELEMENT UNACCOUNTED FOR BY STYLESHEET: <xsl:apply-templates />
</xsl:template> <xsl:template match="book"> <html> <body> <xsl:apply-templates /> </body> </html> </xsl:template> <xsl:template match="title">
<xsl:apply-templates />
</xsl:template> <xsl:template match="chapter/para"> <paragraph> <xsl:apply-templates /> </p> </xsl:template> <xsl:template match="appendix/para"> <paragraph> <xsl:apply-templates /> </p> </xsl:template>
</xsl:stylesheet> Output: <html>
<body>
From Book I
<paragraph>para1</p> <paragraph>para2</p>
para ELEMENT UNACCOUNTED FOR BY STYLESHEET: para 3
Glossary
<paragraph>A</p> <paragraph>B</p> </body>
</html></source>
name()- Takes zero or one node-set arguments and returns the name of the node in prefix:localpart format
<source lang="xml">
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></source>
prints names of all elements used in the document
<source lang="xml">
File: Data.xml <?xml version="1.0" encoding="utf-8"?>
<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>
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="/">
</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"?>data |
contact |
name |
street |
number |
tel |
home |
work |
contact |
name |
street |
number |
tel |
home |
work |